Initial commit squashed
This commit is contained in:
23
backend/migration/Cargo.toml
Normal file
23
backend/migration/Cargo.toml
Normal file
@@ -0,0 +1,23 @@
|
||||
[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"] }
|
||||
chrono = "0.4"
|
||||
|
||||
[dependencies.sea-orm-migration]
|
||||
version = "^0.10.6"
|
||||
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-native-tls", # `ASYNC_RUNTIME` feature
|
||||
"sqlx-sqlite", # `DATABASE_DRIVER` feature
|
||||
]
|
||||
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
|
||||
```
|
||||
12
backend/migration/src/lib.rs
Normal file
12
backend/migration/src/lib.rs
Normal file
@@ -0,0 +1,12 @@
|
||||
pub use sea_orm_migration::prelude::*;
|
||||
|
||||
mod m20220101_000001_create_table;
|
||||
|
||||
pub struct Migrator;
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl MigratorTrait for Migrator {
|
||||
fn migrations() -> Vec<Box<dyn MigrationTrait>> {
|
||||
vec![Box::new(m20220101_000001_create_table::Migration)]
|
||||
}
|
||||
}
|
||||
351
backend/migration/src/m20220101_000001_create_table.rs
Normal file
351
backend/migration/src/m20220101_000001_create_table.rs
Normal file
@@ -0,0 +1,351 @@
|
||||
//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,
|
||||
}
|
||||
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