From 074a660e4f91a837e61510cf9f7fd577d5bc4898 Mon Sep 17 00:00:00 2001 From: eason <30045503+Eason0729@users.noreply.github.com> Date: Thu, 7 Dec 2023 20:53:03 +0800 Subject: [PATCH] init idens --- backend/entity/src/test.rs | 2 +- backend/migration/Cargo.toml | 22 +++ backend/migration/README.md | 41 +++++ backend/migration/src/lib.rs | 12 ++ .../src/m20231207_000001_create_table.rs | 144 ++++++++++++++++++ backend/migration/src/main.rs | 6 + 6 files changed, 226 insertions(+), 1 deletion(-) create mode 100644 backend/migration/Cargo.toml create mode 100644 backend/migration/README.md create mode 100644 backend/migration/src/lib.rs create mode 100644 backend/migration/src/m20231207_000001_create_table.rs create mode 100644 backend/migration/src/main.rs diff --git a/backend/entity/src/test.rs b/backend/entity/src/test.rs index 501c8167..79a8ed06 100644 --- a/backend/entity/src/test.rs +++ b/backend/entity/src/test.rs @@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize}; use crate::problem; #[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel, Deserialize, Serialize)] -#[sea_orm(table_name = "testcase")] +#[sea_orm(table_name = "test")] pub struct Model { #[sea_orm(primary_key, auto_increment = true)] pub id: i32, diff --git a/backend/migration/Cargo.toml b/backend/migration/Cargo.toml new file mode 100644 index 00000000..22cdf3ed --- /dev/null +++ b/backend/migration/Cargo.toml @@ -0,0 +1,22 @@ +[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.11.0" +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` feature + # "sqlx-postgres", # `DATABASE_DRIVER` feature +] diff --git a/backend/migration/README.md b/backend/migration/README.md new file mode 100644 index 00000000..b3ea53eb --- /dev/null +++ b/backend/migration/README.md @@ -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 + ``` diff --git a/backend/migration/src/lib.rs b/backend/migration/src/lib.rs new file mode 100644 index 00000000..a047dbf6 --- /dev/null +++ b/backend/migration/src/lib.rs @@ -0,0 +1,12 @@ +pub use sea_orm_migration::prelude::*; + +mod m20231207_000001_create_table; + +pub struct Migrator; + +#[async_trait::async_trait] +impl MigratorTrait for Migrator { + fn migrations() -> Vec> { + vec![Box::new(m20231207_000001_create_table::Migration)] + } +} diff --git a/backend/migration/src/m20231207_000001_create_table.rs b/backend/migration/src/m20231207_000001_create_table.rs new file mode 100644 index 00000000..37ad74b2 --- /dev/null +++ b/backend/migration/src/m20231207_000001_create_table.rs @@ -0,0 +1,144 @@ +use sea_orm_migration::prelude::*; + +#[derive(Iden)] +enum Announcement{ + Id, + Title, + Content, + CreateAt, + UpdateAt, +} +enum Contest { + Id, + Hoster, + Begin, + End, + Title, + Content, + Tags, + Password, + CreateAt, + UpdateAt, + Public, +} +enum Education{ + Id, + ProblemId, + UserId, + Tags, + Title, + Content, +} +#[derive(Iden)] +enum Post { + Table, + Id, + Title, + Text, +} +#[derive(Iden)] +enum Problem + { + id, + user_id, + contest_id, + accept_count, + submit_count, + ac_rate, + memory, + time, + difficulty, + public, + tags, + title, + content, + create_at, + UpdateAt, + MatchRule, + } + +enum Submit{ + id, + user_id, + problem_id, + upload_at, + time, + accuracy, + committed, + lang, + code, + memory, + pass_case, + status, + accept, + score, +} +enum Test{ + id, + user_id, + problem_id, + input, + output, + score, +} +enum Token{ + id, + user_id, + rand, + permission, + expiry, +} +enum User { + id, + permission, + score, + username, + password, + create_at, +} +enum UserContest{ + id, + user_id, + contest_id, + score, +} + +#[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(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> { + // Replace the sample below with your own migration scripts + todo!(); + + manager + .drop_table(Table::drop().table(Post::Table).to_owned()) + .await + } +} + +/// Learn more at https://docs.rs/sea-query#iden diff --git a/backend/migration/src/main.rs b/backend/migration/src/main.rs new file mode 100644 index 00000000..c6b6e48d --- /dev/null +++ b/backend/migration/src/main.rs @@ -0,0 +1,6 @@ +use sea_orm_migration::prelude::*; + +#[async_std::main] +async fn main() { + cli::run_cli(migration::Migrator).await; +}