diff --git a/backend/migration/Cargo.toml b/backend/migration/Cargo.toml index 0b07816..f568183 100644 --- a/backend/migration/Cargo.toml +++ b/backend/migration/Cargo.toml @@ -10,11 +10,14 @@ path = "src/lib.rs" [dependencies] async-std = { version = "^1", features = ["attributes", "tokio1"] } +chrono = "0.4" [dependencies.sea-orm-migration] -version = "^0.9.2" # sea-orm-migration version +version = "^0.9.2" features = [ - # Enable following runtime and db backend features if you want to run migration via CLI - # "runtime-tokio-native-tls", - # "sqlx-postgres", + # 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` feature + "sqlx-sqlite", # `DATABASE_DRIVER` feature ] diff --git a/backend/migration/README.md b/backend/migration/README.md index 963caae..b3ea53e 100644 --- a/backend/migration/README.md +++ b/backend/migration/README.md @@ -1,5 +1,9 @@ # Running Migrator CLI +- Generate a new migration file + ```sh + cargo run -- migrate generate MIGRATION_NAME + ``` - Apply all pending migrations ```sh cargo run diff --git a/backend/migration/src/lib.rs b/backend/migration/src/lib.rs index af8d9b2..2c605af 100644 --- a/backend/migration/src/lib.rs +++ b/backend/migration/src/lib.rs @@ -1,12 +1,12 @@ pub use sea_orm_migration::prelude::*; -mod m20220120_000001_create_post_table; +mod m20220101_000001_create_table; pub struct Migrator; #[async_trait::async_trait] impl MigratorTrait for Migrator { fn migrations() -> Vec> { - vec![Box::new(m20220120_000001_create_post_table::Migration)] + vec![Box::new(m20220101_000001_create_table::Migration)] } } diff --git a/backend/migration/src/m20220101_000001_create_table.rs b/backend/migration/src/m20220101_000001_create_table.rs new file mode 100644 index 0000000..6fdc479 --- /dev/null +++ b/backend/migration/src/m20220101_000001_create_table.rs @@ -0,0 +1,285 @@ +use chrono::DateTime; +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> { + // Replace the sample below with your own migration scripts + //todo!(); + + manager + .create_table( + Table::create() + .table(Book::Table) + .if_not_exists() + .col( + ColumnDef::new(Book::Id) + .integer() + .not_null() + .auto_increment() + .primary_key(), + ) + .col(ColumnDef::new(Book::OpenLibraryKey).string().not_null()) + .col(ColumnDef::new(Book::Title).string().not_null()) + .col(ColumnDef::new(Book::EditionCount).integer().not_null()) + .col(ColumnDef::new(Book::FirstPublishYear).integer().not_null()) + .col(ColumnDef::new(Book::MedianPageCount).integer().not_null()) + .col(ColumnDef::new(Book::GoodreadId).string().not_null()) + .col(ColumnDef::new(Book::Description).string().not_null()) + .col(ColumnDef::new(Book::Cover).string().not_null()) + .col(ColumnDef::new(Book::Location).string().not_null()) + .col(ColumnDef::new(Book::TimeAdded).time().not_null()) + .col(ColumnDef::new(Book::Rating).integer().not_null()) + .col(ColumnDef::new(Book::Comments).string().not_null()) + .to_owned(), + ) + .await; + + manager + .create_table( + Table::create() + .table(BookAuthor::Table) + .if_not_exists() + .col( + ColumnDef::new(BookAuthor::Id) + .integer() + .not_null() + .auto_increment() + .primary_key(), + ) + .col(ColumnDef::new(BookAuthor::AuthorName).string().not_null()) + .col(ColumnDef::new(BookAuthor::BookId).integer().not_null()) + .foreign_key( + ForeignKey::create() + .name("FK_2e303c3a712662f1fc2a4d0aad6") + .from(BookAuthor::Table, BookAuthor::BookId) + .to(Book::Table, Book::Id) + .on_delete(ForeignKeyAction::Cascade) + .on_update(ForeignKeyAction::Cascade), + ) + .to_owned(), + ) + .await; + + manager + .create_table( + Table::create() + .table(BookAuthor::Table) + .if_not_exists() + .col( + ColumnDef::new(BookAuthor::Id) + .integer() + .not_null() + .auto_increment() + .primary_key(), + ) + .col(ColumnDef::new(BookAuthor::AuthorName).string().not_null()) + .col(ColumnDef::new(BookAuthor::BookId).integer().not_null()) + .foreign_key( + ForeignKey::create() + .name("FK_2e303c3a712662f1fc2a4d0aad5") + .from(BookAuthor::Table, BookAuthor::BookId) + .to(Book::Table, Book::Id) + .on_delete(ForeignKeyAction::Cascade) + .on_update(ForeignKeyAction::Cascade), + ) + .to_owned(), + ) + .await; + + manager + .create_table( + Table::create() + .table(BookAuthor::Table) + .if_not_exists() + .col( + ColumnDef::new(BookPerson::Id) + .integer() + .not_null() + .auto_increment() + .primary_key(), + ) + .col(ColumnDef::new(BookPerson::Person).string().not_null()) + .col(ColumnDef::new(BookPerson::BookId).integer().not_null()) + .foreign_key( + ForeignKey::create() + .name("FK_2e303c3a712662f1fc2a4d0aad6") + .from(BookPerson::Table, BookPerson::BookId) + .to(Book::Table, Book::Id) + .on_delete(ForeignKeyAction::Cascade) + .on_update(ForeignKeyAction::Cascade), + ) + .to_owned(), + ) + .await; + + manager + .create_table( + Table::create() + .table(BookPlace::Table) + .if_not_exists() + .col( + ColumnDef::new(BookPlace::Id) + .integer() + .not_null() + .auto_increment() + .primary_key(), + ) + .col(ColumnDef::new(BookPlace::Place).string().not_null()) + .col(ColumnDef::new(BookPlace::BookId).integer().not_null()) + .foreign_key( + ForeignKey::create() + .name("FK_2e303c3a712662f1fc2a4d0aad7") + .from(BookPlace::Table, BookPlace::BookId) + .to(Book::Table, Book::Id) + .on_delete(ForeignKeyAction::Cascade) + .on_update(ForeignKeyAction::Cascade), + ) + .to_owned(), + ) + .await; + + manager + .create_table( + Table::create() + .table(BookSubject::Table) + .if_not_exists() + .col( + ColumnDef::new(BookSubject::Id) + .integer() + .not_null() + .auto_increment() + .primary_key(), + ) + .col(ColumnDef::new(BookSubject::Subject).string().not_null()) + .col(ColumnDef::new(BookSubject::BookId).integer().not_null()) + .foreign_key( + ForeignKey::create() + .name("FK_2e303c3a712662f1fc2a4d0aad8") + .from(BookSubject::Table, BookSubject::BookId) + .to(Book::Table, Book::Id) + .on_delete(ForeignKeyAction::Cascade) + .on_update(ForeignKeyAction::Cascade), + ) + .to_owned(), + ) + .await; + + manager + .create_table( + Table::create() + .table(BookTime::Table) + .if_not_exists() + .col( + ColumnDef::new(BookTime::Id) + .integer() + .not_null() + .auto_increment() + .primary_key(), + ) + .col(ColumnDef::new(BookTime::Time).string().not_null()) + .col(ColumnDef::new(BookTime::BookId).integer().not_null()) + .foreign_key( + ForeignKey::create() + .name("FK_2e303c3a712662f1fc2a4d0aad9") + .from(BookTime::Table, BookTime::BookId) + .to(Book::Table, Book::Id) + .on_delete(ForeignKeyAction::Cascade) + .on_update(ForeignKeyAction::Cascade), + ) + .to_owned(), + ) + .await + } + + async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { + // Replace the sample below with your own migration scripts + //todo!(); + + manager + .drop_table(Table::drop().table(Book::Table).to_owned()) + .await; + manager + .drop_table(Table::drop().table(BookAuthor::Table).to_owned()) + .await; + manager + .drop_table(Table::drop().table(BookPerson::Table).to_owned()) + .await; + manager + .drop_table(Table::drop().table(BookPlace::Table).to_owned()) + .await; + manager + .drop_table(Table::drop().table(BookSubject::Table).to_owned()) + .await; + manager + .drop_table(Table::drop().table(BookTime::Table).to_owned()) + .await + } +} + +/// Learn more at https://docs.rs/sea-query#iden +#[derive(Iden)] +enum Book { + Table, + Id, + OpenLibraryKey, + Title, + EditionCount, + FirstPublishYear, + MedianPageCount, + //AuthorName, + //Person, + //Place, + //Subject, + //Time, + GoodreadId, + Description, + Cover, + Location, + TimeAdded, + Rating, + Comments, +} + +#[derive(Iden)] +enum BookAuthor { + Table, + Id, + AuthorName, + BookId, +} + +#[derive(Iden)] +enum BookPerson { + Table, + Id, + Person, + BookId, +} + +#[derive(Iden)] +enum BookPlace { + Table, + Id, + Place, + BookId, +} + +#[derive(Iden)] +enum BookSubject { + Table, + Id, + Subject, + BookId, +} + +#[derive(Iden)] +enum BookTime { + Table, + Id, + Time, + BookId, +} diff --git a/backend/migration/src/m20220120_000001_create_post_table.rs b/backend/migration/src/m20220120_000001_create_post_table.rs deleted file mode 100644 index a2fa021..0000000 --- a/backend/migration/src/m20220120_000001_create_post_table.rs +++ /dev/null @@ -1,42 +0,0 @@ -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> { - manager - .create_table( - Table::create() - .table(Posts::Table) - .if_not_exists() - .col( - ColumnDef::new(Posts::Id) - .integer() - .not_null() - .auto_increment() - .primary_key(), - ) - .col(ColumnDef::new(Posts::Title).string().not_null()) - .col(ColumnDef::new(Posts::Text).string().not_null()) - .to_owned(), - ) - .await - } - - async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { - manager - .drop_table(Table::drop().table(Posts::Table).to_owned()) - .await - } -} - -/// Learn more at https://docs.rs/sea-query#iden -#[derive(Iden)] -enum Posts { - Table, - Id, - Title, - Text, -}