Files
booksman/backend/migration/src/m20220101_000001_create_table.rs

336 lines
11 KiB
Rust

//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(User::Table)
.if_not_exists()
.col(
ColumnDef::new(User::Id)
.integer()
.not_null()
.auto_increment()
.primary_key(),
)
.col(ColumnDef::new(User::UserName).string().unique_key().not_null())
.col(ColumnDef::new(User::PasswordHash).string().not_null())
.to_owned(),
)
.await.expect("Migration failed");
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())
.col(ColumnDef::new(Book::Title).string().not_null())
.col(ColumnDef::new(Book::EditionCount).integer())
.col(ColumnDef::new(Book::FirstPublishYear).integer())
.col(ColumnDef::new(Book::MedianPageCount).integer())
.col(ColumnDef::new(Book::GoodreadId).string())
.col(ColumnDef::new(Book::Description).string())
.col(ColumnDef::new(Book::Cover).string())
.col(ColumnDef::new(Book::Location).string())
.col(ColumnDef::new(Book::TimeAdded).time())
.col(ColumnDef::new(Book::Rating).integer())
.col(ColumnDef::new(Book::Comments).string())
.col(ColumnDef::new(Book::UserId).integer().not_null())
.foreign_key(
ForeignKey::create()
.name("FK_2e303c3a712662f1fc2a4d0aavc")
.from(Book::Table, Book::UserId)
.to(User::Table, User::Id)
.on_delete(ForeignKeyAction::Cascade)
.on_update(ForeignKeyAction::Cascade),
)
.to_owned(),
)
.await.expect("Migration failed");
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.expect("Migration failed");
manager
.create_table(
Table::create()
.table(BookPerson::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.expect("Migration failed");
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.expect("Migration failed");
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.expect("Migration failed");
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.expect("Migration failed");
manager
.create_table(
Table::create()
.table(BookISBN::Table)
.if_not_exists()
.col(
ColumnDef::new(BookISBN::Id)
.integer()
.not_null()
.auto_increment()
.primary_key(),
)
.col(ColumnDef::new(BookISBN::ISBN).string().not_null())
.col(ColumnDef::new(BookISBN::BookId).integer().not_null())
.foreign_key(
ForeignKey::create()
.name("FK_2e303c3a712662f1fc2a4d0aae0")
.from(BookISBN::Table, BookISBN::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.expect("Drop failed");
manager
.drop_table(Table::drop().table(BookAuthor::Table).to_owned())
.await.expect("Drop failed");
manager
.drop_table(Table::drop().table(BookPerson::Table).to_owned())
.await.expect("Drop failed");
manager
.drop_table(Table::drop().table(BookPlace::Table).to_owned())
.await.expect("Drop failed");
manager
.drop_table(Table::drop().table(BookSubject::Table).to_owned())
.await.expect("Drop failed");
manager
.drop_table(Table::drop().table(BookTime::Table).to_owned())
.await.expect("Drop failed");
manager
.drop_table(Table::drop().table(BookISBN::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,
//ISBN,
GoodreadId,
Description,
Cover,
Location,
TimeAdded,
Rating,
Comments,
UserId
}
#[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,
}
#[derive(Iden)]
enum BookISBN {
Table,
Id,
ISBN,
BookId,
}
#[derive(Iden)]
enum User {
Table,
Id,
UserName,
PasswordHash,
}