Add migrations

This commit is contained in:
fluo10 2025-05-20 08:34:15 +09:00
parent a42065fad3
commit 8c8d01deb9
16 changed files with 353 additions and 2 deletions

6
Cargo.toml Normal file
View file

@ -0,0 +1,6 @@
[workspace]
members = ["bullet-cards-*"]
[workspace.dependencies]
async-std = { version = "1", features = ["attributes", "tokio1"] }
sea-orm-migration = { version = "1.1.0", features = ["runtime-tokio-rustls","sqlx-sqlite"] }

View file

@ -1,3 +1,3 @@
# porgana # Bullet Cards
My personal organization application My personal organization application inspired by bullet journal and index card

View file

@ -0,0 +1,9 @@
[package]
name = "bullet-cards-migration-client"
version = "0.1.0"
edition = "2021"
publish = false
[dependencies]
async-std.workspace = true
sea-orm-migration.workspace = true

View file

@ -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
```

View 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)]
}
}

View file

@ -0,0 +1,41 @@
use sea_orm_migration::{prelude::*, schema::*};
#[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(pk_auto(Post::Id))
.col(string(Post::Title))
.col(string(Post::Text))
.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,
}

View file

@ -0,0 +1,6 @@
use sea_orm_migration::prelude::*;
#[async_std::main]
async fn main() {
cli::run_cli(bullet_cards_migration_client::Migrator).await;
}

View file

@ -0,0 +1,9 @@
[package]
name = "bullet-cards-migration-core"
version = "0.1.0"
edition = "2021"
publish = false
[dependencies]
async-std.workspace = true
sea-orm-migration.workspace = true

View file

@ -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
```

View file

@ -0,0 +1 @@
pub mod m20220101_000001_create_table;

View file

@ -0,0 +1,76 @@
use sea_orm_migration::{prelude::*, schema::*};
pub trait TableMigration {
async fn up<'a>(manager: &'a SchemaManager<'a>) -> Result<(), DbErr> {
manager.create_table(Self::table_create_statement()).await?;
for statement in Self::index_create_statements().into_iter() {
manager.create_index(statement).await?
}
Ok(())
}
async fn down<'a>(manager: &'a SchemaManager<'a>) -> Result<(), DbErr> {
manager.drop_table(Self::table_drop_statement()).await?;
Ok(())
}
fn table_create_statement() -> TableCreateStatement;
fn index_create_statements() -> Vec<IndexCreateStatement>;
fn table_drop_statement() -> TableDropStatement;
}
#[derive(DeriveIden)]
pub enum BulletCard {
Table,
Id,
CreatedAt,
UpdatedAt,
DeletedAt,
Title,
Content,
}
static IDX_BULLET_CARD_CREATED_AT: &str = "idx_bullet_card_created_at";
static IDX_BULLET_CARD_UPDATED_AT: &str = "idx_bullet_card_updated_at";
static IDX_BULLET_CARD_DELETED_AT: &str = "idx_bullet_card_deleted_at";
static IDX_BULLET_CARD_TITLE: &str = "idx_bullet_card_title";
impl TableMigration for BulletCard {
fn table_create_statement() -> TableCreateStatement {
Table::create()
.table(Self::Table)
.if_not_exists()
.col(uuid(Self::Id))
.col(timestamp_with_time_zone(Self::CreatedAt))
.col(timestamp_with_time_zone(Self::UpdatedAt))
.col(timestamp_with_time_zone_null(Self::DeletedAt))
.col(string(Self::Title))
.col(string(Self::Content))
.to_owned()
}
fn index_create_statements() -> Vec<IndexCreateStatement> {
vec![
Index::create().name(IDX_BULLET_CARD_CREATED_AT)
.table(Self::Table)
.col(Self::CreatedAt)
.to_owned(),
Index::create().name(IDX_BULLET_CARD_DELETED_AT)
.table(Self::Table)
.col(Self::DeletedAt)
.to_owned(),
Index::create().name(IDX_BULLET_CARD_TITLE)
.table(Self::Table)
.col(Self::Title)
.to_owned(),
Index::create().name(IDX_BULLET_CARD_UPDATED_AT)
.table(Self::Table)
.col(Self::UpdatedAt)
.to_owned(),
]
}
fn table_drop_statement() -> TableDropStatement {
Table::drop().table(Self::Table).to_owned()
}
}

View file

@ -0,0 +1,9 @@
[package]
name = "bullet-cards-migration-server"
version = "0.1.0"
edition = "2021"
publish = false
[dependencies]
async-std.workspace = true
sea-orm-migration = { workspace = true, features = ["sqlx-postgres"]}

View file

@ -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
```

View 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)]
}
}

View file

@ -0,0 +1,41 @@
use sea_orm_migration::{prelude::*, schema::*};
#[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(pk_auto(Post::Id))
.col(string(Post::Title))
.col(string(Post::Text))
.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,
}

View file

@ -0,0 +1,6 @@
use sea_orm_migration::prelude::*;
#[async_std::main]
async fn main() {
cli::run_cli(bullet_cards_migration_server::Migrator).await;
}