initial commit
This commit is contained in:
19
backend/migration/Cargo.toml
Normal file
19
backend/migration/Cargo.toml
Normal file
@@ -0,0 +1,19 @@
|
||||
[package]
|
||||
name = "migration"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
publish = false
|
||||
|
||||
[lib]
|
||||
name = "migration"
|
||||
path = "src/lib.rs"
|
||||
|
||||
[dependencies]
|
||||
async-std = { version = "^1", features = ["attributes", "tokio1"] }
|
||||
|
||||
[dependencies.sea-orm-migration]
|
||||
version = "^0.9.0"
|
||||
features = [
|
||||
"runtime-actix-rustls",
|
||||
"sqlx-postgres",
|
||||
]
|
||||
41
backend/migration/README.md
Normal file
41
backend/migration/README.md
Normal file
@@ -0,0 +1,41 @@
|
||||
# Running Migrator CLI
|
||||
|
||||
- Generate a new migration file
|
||||
```sh
|
||||
cargo run -- migrate generate MIGRATION_NAME
|
||||
```
|
||||
- Apply all pending migrations
|
||||
```sh
|
||||
cargo run
|
||||
```
|
||||
```sh
|
||||
cargo run -- up
|
||||
```
|
||||
- Apply first 10 pending migrations
|
||||
```sh
|
||||
cargo run -- up -n 10
|
||||
```
|
||||
- Rollback last applied migrations
|
||||
```sh
|
||||
cargo run -- down
|
||||
```
|
||||
- Rollback last 10 applied migrations
|
||||
```sh
|
||||
cargo run -- down -n 10
|
||||
```
|
||||
- Drop all tables from the database, then reapply all migrations
|
||||
```sh
|
||||
cargo run -- fresh
|
||||
```
|
||||
- Rollback all applied migrations, then reapply all migrations
|
||||
```sh
|
||||
cargo run -- refresh
|
||||
```
|
||||
- Rollback all applied migrations
|
||||
```sh
|
||||
cargo run -- reset
|
||||
```
|
||||
- Check the status of all migrations
|
||||
```sh
|
||||
cargo run -- status
|
||||
```
|
||||
16
backend/migration/src/lib.rs
Normal file
16
backend/migration/src/lib.rs
Normal file
@@ -0,0 +1,16 @@
|
||||
pub use sea_orm_migration::prelude::*;
|
||||
|
||||
//❗ After creating a new migration file, remove the sample migration below 👇
|
||||
mod m20220101_000001_create_todo_table;
|
||||
|
||||
pub struct Migrator;
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl MigratorTrait for Migrator {
|
||||
fn migrations() -> Vec<Box<dyn MigrationTrait>> {
|
||||
vec![
|
||||
//❗ After creating a new migration file, remove the sample migration below 👇
|
||||
Box::new(m20220101_000001_create_todo_table::Migration)
|
||||
]
|
||||
}
|
||||
}
|
||||
32
backend/migration/src/m20220101_000001_create_todo_table.rs
Normal file
32
backend/migration/src/m20220101_000001_create_todo_table.rs
Normal file
@@ -0,0 +1,32 @@
|
||||
use crate::sea_orm::{Statement, ConnectionTrait};
|
||||
use sea_orm_migration::prelude::*;
|
||||
|
||||
#[derive(DeriveMigrationName)]
|
||||
pub struct Migration;
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl MigrationTrait for Migration {
|
||||
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||
//❗ Do not replace the sample below 👇
|
||||
//❗ Instead, create a new migration file and add your own migration code there.
|
||||
//❗ See the README for more information.
|
||||
let sql = r#"
|
||||
CREATE TABLE IF NOT EXISTS todo (
|
||||
id SERIAL PRIMARY KEY,
|
||||
title VARCHAR(255) UNIQUE NOT NULL,
|
||||
description VARCHAR(255) NOT NULL,
|
||||
done BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
|
||||
updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW()
|
||||
);
|
||||
"#;
|
||||
let stmt = Statement::from_string(manager.get_database_backend(), sql.to_owned());
|
||||
manager.get_connection().execute(stmt).await.map(|_| ())
|
||||
}
|
||||
|
||||
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||
let sql = "DROP TABLE todo";
|
||||
let stmt = Statement::from_string(manager.get_database_backend(), sql.to_owned());
|
||||
manager.get_connection().execute(stmt).await.map(|_| ())
|
||||
}
|
||||
}
|
||||
6
backend/migration/src/main.rs
Normal file
6
backend/migration/src/main.rs
Normal file
@@ -0,0 +1,6 @@
|
||||
use sea_orm_migration::prelude::*;
|
||||
|
||||
#[async_std::main]
|
||||
async fn main() {
|
||||
cli::run_cli(migration::Migrator).await;
|
||||
}
|
||||
Reference in New Issue
Block a user