Migration shouldn't depends on symbol provided by entity crate (#785)

* Migration shouldn't depends on symbol provided by entity crate

* Add docs
This commit is contained in:
Billy Chan 2022-06-12 22:25:32 +08:00 committed by GitHub
parent 5cf4d6022b
commit 9b41f1c1b1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
20 changed files with 227 additions and 82 deletions

View File

@ -9,7 +9,6 @@ name = "migration"
path = "src/lib.rs" path = "src/lib.rs"
[dependencies] [dependencies]
entity = { path = "../entity" }
async-std = { version = "^1", features = ["attributes", "tokio1"] } async-std = { version = "^1", features = ["attributes", "tokio1"] }
[dependencies.sea-orm-migration] [dependencies.sea-orm-migration]

View File

@ -1,4 +1,3 @@
use entity::post::*;
use sea_orm_migration::prelude::*; use sea_orm_migration::prelude::*;
pub struct Migration; pub struct Migration;
@ -15,17 +14,17 @@ impl MigrationTrait for Migration {
manager manager
.create_table( .create_table(
Table::create() Table::create()
.table(Entity) .table(Post::Table)
.if_not_exists() .if_not_exists()
.col( .col(
ColumnDef::new(Column::Id) ColumnDef::new(Post::Id)
.integer() .integer()
.not_null() .not_null()
.auto_increment() .auto_increment()
.primary_key(), .primary_key(),
) )
.col(ColumnDef::new(Column::Title).string().not_null()) .col(ColumnDef::new(Post::Title).string().not_null())
.col(ColumnDef::new(Column::Text).string().not_null()) .col(ColumnDef::new(Post::Text).string().not_null())
.to_owned(), .to_owned(),
) )
.await .await
@ -33,7 +32,21 @@ impl MigrationTrait for Migration {
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager manager
.drop_table(Table::drop().table(Entity).to_owned()) .drop_table(Table::drop().table(Post::Table).to_owned())
.await .await
} }
} }
/// `Iden` is a trait for identifiers used in any query statement.
///
/// Commonly implemented by Enum where each Enum represents a table found in a database,
/// and its variants include table name and column name.
///
/// Learn more at https://docs.rs/sea-query#iden
#[derive(Iden)]
enum Post {
Table,
Id,
Title,
Text,
}

View File

@ -9,7 +9,6 @@ name = "migration"
path = "src/lib.rs" path = "src/lib.rs"
[dependencies] [dependencies]
entity = { path = "../entity" }
async-std = { version = "^1", features = ["attributes", "tokio1"] } async-std = { version = "^1", features = ["attributes", "tokio1"] }
[dependencies.sea-orm-migration] [dependencies.sea-orm-migration]

View File

@ -1,4 +1,3 @@
use entity::post::*;
use sea_orm_migration::prelude::*; use sea_orm_migration::prelude::*;
pub struct Migration; pub struct Migration;
@ -15,17 +14,17 @@ impl MigrationTrait for Migration {
manager manager
.create_table( .create_table(
Table::create() Table::create()
.table(Entity) .table(Post::Table)
.if_not_exists() .if_not_exists()
.col( .col(
ColumnDef::new(Column::Id) ColumnDef::new(Post::Id)
.integer() .integer()
.not_null() .not_null()
.auto_increment() .auto_increment()
.primary_key(), .primary_key(),
) )
.col(ColumnDef::new(Column::Title).string().not_null()) .col(ColumnDef::new(Post::Title).string().not_null())
.col(ColumnDef::new(Column::Text).string().not_null()) .col(ColumnDef::new(Post::Text).string().not_null())
.to_owned(), .to_owned(),
) )
.await .await
@ -33,7 +32,21 @@ impl MigrationTrait for Migration {
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager manager
.drop_table(Table::drop().table(Entity).to_owned()) .drop_table(Table::drop().table(Post::Table).to_owned())
.await .await
} }
} }
/// `Iden` is a trait for identifiers used in any query statement.
///
/// Commonly implemented by Enum where each Enum represents a table found in a database,
/// and its variants include table name and column name.
///
/// Learn more at https://docs.rs/sea-query#iden
#[derive(Iden)]
enum Post {
Table,
Id,
Title,
Text,
}

View File

@ -9,7 +9,6 @@ name = "migration"
path = "src/lib.rs" path = "src/lib.rs"
[dependencies] [dependencies]
entity = { path = "../entity" }
async-std = { version = "^1", features = ["attributes", "tokio1"] } async-std = { version = "^1", features = ["attributes", "tokio1"] }
[dependencies.sea-orm-migration] [dependencies.sea-orm-migration]

View File

@ -1,4 +1,3 @@
use entity::post::*;
use sea_orm_migration::prelude::*; use sea_orm_migration::prelude::*;
pub struct Migration; pub struct Migration;
@ -15,17 +14,17 @@ impl MigrationTrait for Migration {
manager manager
.create_table( .create_table(
Table::create() Table::create()
.table(Entity) .table(Post::Table)
.if_not_exists() .if_not_exists()
.col( .col(
ColumnDef::new(Column::Id) ColumnDef::new(Post::Id)
.integer() .integer()
.not_null() .not_null()
.auto_increment() .auto_increment()
.primary_key(), .primary_key(),
) )
.col(ColumnDef::new(Column::Title).string().not_null()) .col(ColumnDef::new(Post::Title).string().not_null())
.col(ColumnDef::new(Column::Text).string().not_null()) .col(ColumnDef::new(Post::Text).string().not_null())
.to_owned(), .to_owned(),
) )
.await .await
@ -33,7 +32,21 @@ impl MigrationTrait for Migration {
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager manager
.drop_table(Table::drop().table(Entity).to_owned()) .drop_table(Table::drop().table(Post::Table).to_owned())
.await .await
} }
} }
/// `Iden` is a trait for identifiers used in any query statement.
///
/// Commonly implemented by Enum where each Enum represents a table found in a database,
/// and its variants include table name and column name.
///
/// Learn more at https://docs.rs/sea-query#iden
#[derive(Iden)]
enum Post {
Table,
Id,
Title,
Text,
}

View File

@ -10,7 +10,6 @@ path = "src/lib.rs"
[dependencies] [dependencies]
dotenv = "0.15.0" dotenv = "0.15.0"
entity = { path = "../entity" }
async-std = { version = "^1", features = ["attributes", "tokio1"] } async-std = { version = "^1", features = ["attributes", "tokio1"] }
[dependencies.sea-orm-migration] [dependencies.sea-orm-migration]

View File

@ -1,22 +1,7 @@
use entity::note;
use sea_orm::{DbBackend, EntityTrait, Schema};
use sea_orm_migration::prelude::*; use sea_orm_migration::prelude::*;
pub struct Migration; pub struct Migration;
fn get_seaorm_create_stmt<E: EntityTrait>(e: E) -> TableCreateStatement {
let schema = Schema::new(DbBackend::Sqlite);
schema
.create_table_from_entity(e)
.if_not_exists()
.to_owned()
}
fn get_seaorm_drop_stmt<E: EntityTrait>(e: E) -> TableDropStatement {
Table::drop().table(e).if_exists().to_owned()
}
impl MigrationName for Migration { impl MigrationName for Migration {
fn name(&self) -> &str { fn name(&self) -> &str {
"m20220101_000001_create_table" "m20220101_000001_create_table"
@ -26,22 +11,42 @@ impl MigrationName for Migration {
#[async_trait::async_trait] #[async_trait::async_trait]
impl MigrationTrait for Migration { impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
let stmts = vec![get_seaorm_create_stmt(note::Entity)]; manager
.create_table(
for stmt in stmts { Table::create()
manager.create_table(stmt.to_owned()).await?; .table(Notes::Table)
} .if_not_exists()
.col(
Ok(()) ColumnDef::new(Notes::Id)
.integer()
.not_null()
.auto_increment()
.primary_key(),
)
.col(ColumnDef::new(Notes::Title).string().not_null())
.col(ColumnDef::new(Notes::Text).string().not_null())
.to_owned(),
)
.await
} }
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
let stmts = vec![get_seaorm_drop_stmt(note::Entity)]; manager
.drop_table(Table::drop().table(Notes::Table).to_owned())
for stmt in stmts { .await
manager.drop_table(stmt.to_owned()).await?; }
} }
Ok(()) /// `Iden` is a trait for identifiers used in any query statement.
} ///
/// Commonly implemented by Enum where each Enum represents a table found in a database,
/// and its variants include table name and column name.
///
/// Learn more at https://docs.rs/sea-query#iden
#[derive(Iden)]
enum Notes {
Table,
Id,
Title,
Text,
} }

View File

@ -9,7 +9,6 @@ name = "migration"
path = "src/lib.rs" path = "src/lib.rs"
[dependencies] [dependencies]
entity = { path = "../entity" }
async-std = { version = "^1", features = ["attributes", "tokio1"] } async-std = { version = "^1", features = ["attributes", "tokio1"] }
[dependencies.sea-orm-migration] [dependencies.sea-orm-migration]

View File

@ -1,4 +1,3 @@
use entity::post::*;
use sea_orm_migration::prelude::*; use sea_orm_migration::prelude::*;
pub struct Migration; pub struct Migration;
@ -15,17 +14,17 @@ impl MigrationTrait for Migration {
manager manager
.create_table( .create_table(
Table::create() Table::create()
.table(Entity) .table(Post::Table)
.if_not_exists() .if_not_exists()
.col( .col(
ColumnDef::new(Column::Id) ColumnDef::new(Post::Id)
.integer() .integer()
.not_null() .not_null()
.auto_increment() .auto_increment()
.primary_key(), .primary_key(),
) )
.col(ColumnDef::new(Column::Title).string().not_null()) .col(ColumnDef::new(Post::Title).string().not_null())
.col(ColumnDef::new(Column::Text).string().not_null()) .col(ColumnDef::new(Post::Text).string().not_null())
.to_owned(), .to_owned(),
) )
.await .await
@ -33,7 +32,21 @@ impl MigrationTrait for Migration {
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager manager
.drop_table(Table::drop().table(Entity).to_owned()) .drop_table(Table::drop().table(Post::Table).to_owned())
.await .await
} }
} }
/// `Iden` is a trait for identifiers used in any query statement.
///
/// Commonly implemented by Enum where each Enum represents a table found in a database,
/// and its variants include table name and column name.
///
/// Learn more at https://docs.rs/sea-query#iden
#[derive(Iden)]
enum Post {
Table,
Id,
Title,
Text,
}

View File

@ -9,7 +9,6 @@ name = "migration"
path = "src/lib.rs" path = "src/lib.rs"
[dependencies] [dependencies]
entity = { path = "../entity" }
async-std = { version = "^1", features = ["attributes", "tokio1"] } async-std = { version = "^1", features = ["attributes", "tokio1"] }
[dependencies.sea-orm-migration] [dependencies.sea-orm-migration]

View File

@ -1,4 +1,3 @@
use entity::post::*;
use sea_orm_migration::prelude::*; use sea_orm_migration::prelude::*;
pub struct Migration; pub struct Migration;
@ -15,17 +14,17 @@ impl MigrationTrait for Migration {
manager manager
.create_table( .create_table(
Table::create() Table::create()
.table(Entity) .table(Post::Table)
.if_not_exists() .if_not_exists()
.col( .col(
ColumnDef::new(Column::Id) ColumnDef::new(Post::Id)
.integer() .integer()
.not_null() .not_null()
.auto_increment() .auto_increment()
.primary_key(), .primary_key(),
) )
.col(ColumnDef::new(Column::Title).string().not_null()) .col(ColumnDef::new(Post::Title).string().not_null())
.col(ColumnDef::new(Column::Text).string().not_null()) .col(ColumnDef::new(Post::Text).string().not_null())
.to_owned(), .to_owned(),
) )
.await .await
@ -33,7 +32,21 @@ impl MigrationTrait for Migration {
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager manager
.drop_table(Table::drop().table(Entity).to_owned()) .drop_table(Table::drop().table(Post::Table).to_owned())
.await .await
} }
} }
/// `Iden` is a trait for identifiers used in any query statement.
///
/// Commonly implemented by Enum where each Enum represents a table found in a database,
/// and its variants include table name and column name.
///
/// Learn more at https://docs.rs/sea-query#iden
#[derive(Iden)]
enum Post {
Table,
Id,
Title,
Text,
}

View File

@ -9,7 +9,6 @@ name = "migration"
path = "src/lib.rs" path = "src/lib.rs"
[dependencies] [dependencies]
entity = { path = "../entity" }
rocket = { version = "0.5.0-rc.1" } rocket = { version = "0.5.0-rc.1" }
async-std = { version = "^1", features = ["attributes", "tokio1"] } async-std = { version = "^1", features = ["attributes", "tokio1"] }

View File

@ -1,4 +1,3 @@
use entity::post::*;
use sea_orm_migration::prelude::*; use sea_orm_migration::prelude::*;
pub struct Migration; pub struct Migration;
@ -15,17 +14,17 @@ impl MigrationTrait for Migration {
manager manager
.create_table( .create_table(
Table::create() Table::create()
.table(Entity) .table(Post::Table)
.if_not_exists() .if_not_exists()
.col( .col(
ColumnDef::new(Column::Id) ColumnDef::new(Post::Id)
.integer() .integer()
.not_null() .not_null()
.auto_increment() .auto_increment()
.primary_key(), .primary_key(),
) )
.col(ColumnDef::new(Column::Title).string().not_null()) .col(ColumnDef::new(Post::Title).string().not_null())
.col(ColumnDef::new(Column::Text).string().not_null()) .col(ColumnDef::new(Post::Text).string().not_null())
.to_owned(), .to_owned(),
) )
.await .await
@ -33,7 +32,21 @@ impl MigrationTrait for Migration {
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager manager
.drop_table(Table::drop().table(Entity).to_owned()) .drop_table(Table::drop().table(Post::Table).to_owned())
.await .await
} }
} }
/// `Iden` is a trait for identifiers used in any query statement.
///
/// Commonly implemented by Enum where each Enum represents a table found in a database,
/// and its variants include table name and column name.
///
/// Learn more at https://docs.rs/sea-query#iden
#[derive(Iden)]
enum Post {
Table,
Id,
Title,
Text,
}

View File

@ -9,7 +9,6 @@ name = "migration"
path = "src/lib.rs" path = "src/lib.rs"
[dependencies] [dependencies]
entity = { path = "../entity" }
async-std = { version = "^1", features = ["attributes", "tokio1"] } async-std = { version = "^1", features = ["attributes", "tokio1"] }
[dependencies.sea-orm-migration] [dependencies.sea-orm-migration]

View File

@ -1,4 +1,3 @@
use entity::post::*;
use sea_orm_migration::prelude::*; use sea_orm_migration::prelude::*;
pub struct Migration; pub struct Migration;
@ -15,17 +14,17 @@ impl MigrationTrait for Migration {
manager manager
.create_table( .create_table(
Table::create() Table::create()
.table(Entity) .table(Post::Table)
.if_not_exists() .if_not_exists()
.col( .col(
ColumnDef::new(Column::Id) ColumnDef::new(Post::Id)
.integer() .integer()
.not_null() .not_null()
.auto_increment() .auto_increment()
.primary_key(), .primary_key(),
) )
.col(ColumnDef::new(Column::Title).string().not_null()) .col(ColumnDef::new(Post::Title).string().not_null())
.col(ColumnDef::new(Column::Text).string().not_null()) .col(ColumnDef::new(Post::Text).string().not_null())
.to_owned(), .to_owned(),
) )
.await .await
@ -33,7 +32,21 @@ impl MigrationTrait for Migration {
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager manager
.drop_table(Table::drop().table(Entity).to_owned()) .drop_table(Table::drop().table(Post::Table).to_owned())
.await .await
} }
} }
/// `Iden` is a trait for identifiers used in any query statement.
///
/// Commonly implemented by Enum where each Enum represents a table found in a database,
/// and its variants include table name and column name.
///
/// Learn more at https://docs.rs/sea-query#iden
#[derive(Iden)]
enum Post {
Table,
Id,
Title,
Text,
}

View File

@ -9,7 +9,14 @@ name = "migration"
path = "src/lib.rs" path = "src/lib.rs"
[dependencies] [dependencies]
entity = { path = "../entity" } async-std = { version = "^1", features = ["attributes", "tokio1"] }
[dependencies.sea-orm-migration] [dependencies.sea-orm-migration]
version = "<sea-orm-migration-version>" version = "<sea-orm-migration-version>"
features = [
# Enable at least one `ASYNC_RUNTIME` and `DATABASE_DRIVER` feature if you want to run migration via CLI.
# View the list of supported features at https://www.sea-ql.org/SeaORM/docs/install-and-config/database-and-async-runtime.
# e.g.
# "runtime-tokio-rustls", # `ASYNC_RUNTIME` featrure
# "sqlx-postgres", # `DATABASE_DRIVER` feature
]

View File

@ -11,10 +11,48 @@ impl MigrationName for Migration {
#[async_trait::async_trait] #[async_trait::async_trait]
impl MigrationTrait for Migration { impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
todo!() // Replace the sample below with your own migration scripts
todo!();
manager
.create_table(
Table::create()
.table(Post::Table)
.if_not_exists()
.col(
ColumnDef::new(Post::Id)
.integer()
.not_null()
.auto_increment()
.primary_key(),
)
.col(ColumnDef::new(Post::Title).string().not_null())
.col(ColumnDef::new(Post::Text).string().not_null())
.to_owned(),
)
.await
} }
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
todo!() // Replace the sample below with your own migration scripts
todo!();
manager
.drop_table(Table::drop().table(Post::Table).to_owned())
.await
} }
} }
/// `Iden` is a trait for identifiers used in any query statement.
///
/// Commonly implemented by Enum where each Enum represents a table found in a database,
/// and its variants include table name and column name.
///
/// Learn more at https://docs.rs/sea-query#iden
#[derive(Iden)]
enum Post {
Table,
Id,
Title,
Text,
}

View File

@ -35,6 +35,12 @@ impl MigrationTrait for Migration {
} }
} }
/// `Iden` is a trait for identifiers used in any query statement.
///
/// Commonly implemented by Enum where each Enum represents a table found in a database,
/// and its variants include table name and column name.
///
/// Learn more at https://docs.rs/sea-query#iden
#[derive(Iden)] #[derive(Iden)]
pub enum Cake { pub enum Cake {
Table, Table,

View File

@ -54,6 +54,12 @@ impl MigrationTrait for Migration {
} }
} }
/// `Iden` is a trait for identifiers used in any query statement.
///
/// Commonly implemented by Enum where each Enum represents a table found in a database,
/// and its variants include table name and column name.
///
/// Learn more at https://docs.rs/sea-query#iden
#[derive(Iden)] #[derive(Iden)]
pub enum Fruit { pub enum Fruit {
Table, Table,