From 9bfe6ded8f58ec65039b80a2304fd99f4d850602 Mon Sep 17 00:00:00 2001 From: Conner Bogen Date: Fri, 5 Jul 2024 11:16:21 +0200 Subject: [PATCH] bootstrap rust backend --- Cargo.lock | 7 +++ Cargo.toml | 2 + crates/backend/Cargo.toml | 4 ++ crates/backend/src/main.rs | 1 + crates/entity/Cargo.toml | 8 ++++ crates/entity/src/lib.rs | 0 crates/migration/Cargo.toml | 16 +++++++ crates/migration/README.md | 41 ++++++++++++++++ crates/migration/src/lib.rs | 12 +++++ .../src/m20220101_000001_create_table.rs | 47 +++++++++++++++++++ crates/migration/src/main.rs | 6 +++ 11 files changed, 144 insertions(+) create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 crates/backend/Cargo.toml create mode 100644 crates/backend/src/main.rs create mode 100644 crates/entity/Cargo.toml create mode 100644 crates/entity/src/lib.rs create mode 100644 crates/migration/Cargo.toml create mode 100644 crates/migration/README.md create mode 100644 crates/migration/src/lib.rs create mode 100644 crates/migration/src/m20220101_000001_create_table.rs create mode 100644 crates/migration/src/main.rs diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..3b3d789 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "backend" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..1c3065d --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,2 @@ +[workspace] +members = ["crates/backend"] diff --git a/crates/backend/Cargo.toml b/crates/backend/Cargo.toml new file mode 100644 index 0000000..96aa7cc --- /dev/null +++ b/crates/backend/Cargo.toml @@ -0,0 +1,4 @@ +[package] +name = "backend" +version = "0.1.0" +edition = "2021" diff --git a/crates/backend/src/main.rs b/crates/backend/src/main.rs new file mode 100644 index 0000000..f328e4d --- /dev/null +++ b/crates/backend/src/main.rs @@ -0,0 +1 @@ +fn main() {} diff --git a/crates/entity/Cargo.toml b/crates/entity/Cargo.toml new file mode 100644 index 0000000..849ca04 --- /dev/null +++ b/crates/entity/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "entity" +version = "0.1.0" +edition = "2021" + + +[dependencies] +sea-orm = { version = "0.12" } diff --git a/crates/entity/src/lib.rs b/crates/entity/src/lib.rs new file mode 100644 index 0000000..e69de29 diff --git a/crates/migration/Cargo.toml b/crates/migration/Cargo.toml new file mode 100644 index 0000000..6230d31 --- /dev/null +++ b/crates/migration/Cargo.toml @@ -0,0 +1,16 @@ +[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.12.0" +features = ["runtime-tokio-rustls", "sqlx-postgres"] diff --git a/crates/migration/README.md b/crates/migration/README.md new file mode 100644 index 0000000..3b438d8 --- /dev/null +++ b/crates/migration/README.md @@ -0,0 +1,41 @@ +# Running Migrator CLI + +- Generate a new migration file + ```sh + cargo run -- 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/crates/migration/src/lib.rs b/crates/migration/src/lib.rs new file mode 100644 index 0000000..2c605af --- /dev/null +++ b/crates/migration/src/lib.rs @@ -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> { + vec![Box::new(m20220101_000001_create_table::Migration)] + } +} diff --git a/crates/migration/src/m20220101_000001_create_table.rs b/crates/migration/src/m20220101_000001_create_table.rs new file mode 100644 index 0000000..c8f2a5d --- /dev/null +++ b/crates/migration/src/m20220101_000001_create_table.rs @@ -0,0 +1,47 @@ +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(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 + } +} + +#[derive(DeriveIden)] +enum Post { + Table, + Id, + Title, + Text, +} diff --git a/crates/migration/src/main.rs b/crates/migration/src/main.rs new file mode 100644 index 0000000..c6b6e48 --- /dev/null +++ b/crates/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; +}