This commit is contained in:
2025-12-13 02:15:28 +01:00
commit dbec2e9a74
58 changed files with 6177 additions and 0 deletions

22
migration/Cargo.toml Normal file
View File

@@ -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 = "2.0.0-rc"
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
]

41
migration/README.md Normal file
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
```

12
migration/src/lib.rs Normal file
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,555 @@
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> {
// Create table `server`
manager.create_table(
Table::create()
.table(Alias::new("server"))
.if_not_exists()
.col(
ColumnDef::new("id")
.uuid()
.primary_key()
.not_null()
)
.col(
ColumnDef::new("name")
.string()
.not_null()
)
.col(
ColumnDef::new("password")
.string()
.null()
)
.col(
ColumnDef::new(Alias::new("created_at"))
.date_time()
.not_null()
.default(Expr::current_timestamp())
)
.col(
ColumnDef::new(Alias::new("updated_at"))
.date_time()
.not_null()
.default(Expr::current_timestamp())
)
.to_owned(),
).await?;
// Create table `category`
manager.create_table(
Table::create()
.table(Alias::new("category"))
.if_not_exists()
.col(
ColumnDef::new(Alias::new("id"))
.uuid()
.not_null()
.primary_key()
)
.col(
ColumnDef::new(Alias::new("server_id"))
.uuid()
.not_null()
)
.col(
ColumnDef::new(Alias::new("name"))
.string()
.not_null()
)
.col(
ColumnDef::new(Alias::new("created_at"))
.date_time()
.not_null()
.default(Expr::current_timestamp())
)
.col(
ColumnDef::new(Alias::new("updated_at"))
.date_time()
.not_null()
.default(Expr::current_timestamp())
)
// L'index sera créé après via manager.create_index
.foreign_key(
ForeignKey::create()
.name("fk_category_server")
.from(Alias::new("category"), Alias::new("server_id"))
.to(Alias::new("server"), Alias::new("id"))
.on_delete(ForeignKeyAction::Cascade)
.on_update(ForeignKeyAction::Cascade)
)
.to_owned(),
).await?;
// Create table `channel`
manager.create_table(
Table::create()
.table(Alias::new("channel"))
.if_not_exists()
.col(
ColumnDef::new(Alias::new("id"))
.uuid()
.not_null()
.primary_key()
)
.col(
ColumnDef::new(Alias::new("server_id"))
.uuid()
.null()
)
.col(
ColumnDef::new(Alias::new("category_id"))
.uuid()
.null()
)
.col(
ColumnDef::new(Alias::new("position"))
.integer()
.not_null()
.default(0)
)
.col(
ColumnDef::new(Alias::new("channel_type"))
.integer()
.not_null()
)
.col(
ColumnDef::new(Alias::new("name"))
.string()
.null()
)
.col(
ColumnDef::new(Alias::new("created_at"))
.date_time()
.not_null()
.default(Expr::current_timestamp())
)
.col(
ColumnDef::new(Alias::new("updated_at"))
.date_time()
.not_null()
.default(Expr::current_timestamp())
)
// Indexes créés après
.foreign_key(
ForeignKey::create()
.name("fk_channel_server")
.from(Alias::new("channel"), Alias::new("server_id"))
.to(Alias::new("server"), Alias::new("id"))
.on_delete(ForeignKeyAction::Cascade)
)
.foreign_key(
ForeignKey::create()
.name("fk_channel_category")
.from(Alias::new("channel"), Alias::new("category_id"))
.to(Alias::new("category"), Alias::new("id"))
.on_delete(ForeignKeyAction::SetNull)
)
.to_owned()
).await?;
// Create table `user`
manager.create_table(
Table::create()
.table(Alias::new("user"))
.if_not_exists()
.col(
ColumnDef::new(Alias::new("id"))
.uuid()
.not_null()
.primary_key()
)
.col(
ColumnDef::new(Alias::new("username"))
.string()
.not_null()
)
.col(
ColumnDef::new(Alias::new("pub_key"))
.text()
.not_null()
.unique_key()
)
.col(
ColumnDef::new(Alias::new("created_at"))
.date_time()
.not_null()
.default(Expr::current_timestamp())
)
.col(
ColumnDef::new(Alias::new("updated_at"))
.date_time()
.not_null()
.default(Expr::current_timestamp())
)
.to_owned()
).await?;
// Create table `message`
manager.create_table(
Table::create()
.table(Alias::new("message"))
.if_not_exists()
.col(
ColumnDef::new(Alias::new("id"))
.uuid()
.not_null()
.primary_key()
)
.col(
ColumnDef::new(Alias::new("channel_id"))
.uuid()
.not_null()
)
.col(
ColumnDef::new(Alias::new("user_id"))
.uuid()
.not_null()
)
.col(
ColumnDef::new(Alias::new("content"))
.text()
.not_null()
)
.col(
ColumnDef::new(Alias::new("created_at"))
.date_time()
.not_null()
.default(Expr::current_timestamp())
)
.col(
ColumnDef::new(Alias::new("edited_at"))
.date_time()
.null()
)
.col(
ColumnDef::new(Alias::new("reply_to_id"))
.uuid()
.null()
)
// Indexes créés après
.foreign_key(
ForeignKey::create()
.name("fk_message_channel")
.from(Alias::new("message"), Alias::new("channel_id"))
.to(Alias::new("channel"), Alias::new("id"))
.on_delete(ForeignKeyAction::Cascade)
)
.foreign_key(
ForeignKey::create()
.name("fk_message_user")
.from(Alias::new("message"), Alias::new("user_id"))
.to(Alias::new("user"), Alias::new("id"))
.on_delete(ForeignKeyAction::Cascade)
)
.foreign_key(
ForeignKey::create()
.name("fk_message_reply_to")
.from(Alias::new("message"), Alias::new("reply_to_id"))
.to(Alias::new("message"), Alias::new("id"))
.on_delete(ForeignKeyAction::SetNull)
)
.to_owned()
).await?;
// Create table `attachment`
manager.create_table(
Table::create()
.table(Alias::new("attachment"))
.if_not_exists()
.col(
ColumnDef::new(Alias::new("id"))
.uuid()
.not_null()
.primary_key()
)
.col(
ColumnDef::new(Alias::new("message_id"))
.uuid()
.not_null()
)
.col(
ColumnDef::new(Alias::new("filename"))
.string()
.not_null()
)
.col(
ColumnDef::new(Alias::new("file_size"))
.big_integer()
.not_null()
)
.col(
ColumnDef::new(Alias::new("mime_type"))
.string()
.not_null()
)
.col(
ColumnDef::new(Alias::new("created_at"))
.date_time()
.not_null()
.default(Expr::current_timestamp())
)
// Index créé après
.foreign_key(
ForeignKey::create()
.name("fk_attachment_message")
.from(Alias::new("attachment"), Alias::new("message_id"))
.to(Alias::new("message"), Alias::new("id"))
.on_delete(ForeignKeyAction::Cascade)
)
.to_owned()
).await?;
// Create M2M table `server_user`
manager.create_table(
Table::create()
.table(Alias::new("server_user"))
.if_not_exists()
.col(
ColumnDef::new(Alias::new("id"))
.uuid()
.not_null()
.primary_key()
)
.col(
ColumnDef::new(Alias::new("server_id"))
.uuid()
.not_null()
)
.col(
ColumnDef::new(Alias::new("user_id"))
.uuid()
.not_null()
)
.col(
ColumnDef::new(Alias::new("username"))
.string()
.null()
)
.col(
ColumnDef::new(Alias::new("joined_at"))
.date_time()
.not_null()
.default(Expr::current_timestamp())
)
.col(
ColumnDef::new(Alias::new("updated_at"))
.date_time()
.not_null()
)
// Indexes créés après
.foreign_key(
ForeignKey::create()
.name("fk_server_user_server")
.from(Alias::new("server_user"), Alias::new("server_id"))
.to(Alias::new("server"), Alias::new("id"))
.on_delete(ForeignKeyAction::Cascade)
)
.foreign_key(
ForeignKey::create()
.name("fk_server_user_user")
.from(Alias::new("server_user"), Alias::new("user_id"))
.to(Alias::new("user"), Alias::new("id"))
.on_delete(ForeignKeyAction::Cascade)
)
.to_owned()
).await?;
// Create M2M table `channel_user`
manager.create_table(
Table::create()
.table(Alias::new("channel_user"))
.if_not_exists()
.col(
ColumnDef::new(Alias::new("id"))
.uuid()
.not_null()
.primary_key()
)
.col(
ColumnDef::new(Alias::new("channel_id"))
.uuid()
.not_null()
)
.col(
ColumnDef::new(Alias::new("user_id"))
.uuid()
.not_null()
)
.col(
ColumnDef::new(Alias::new("role"))
.string()
.not_null()
.default("member".to_owned())
)
.col(
ColumnDef::new(Alias::new("joined_at"))
.date_time()
.not_null()
.default(Expr::current_timestamp())
)
// Indexes créés après
.foreign_key(
ForeignKey::create()
.name("fk_channel_user_channel")
.from(Alias::new("channel_user"), Alias::new("channel_id"))
.to(Alias::new("channel"), Alias::new("id"))
.on_delete(ForeignKeyAction::Cascade)
)
.foreign_key(
ForeignKey::create()
.name("fk_channel_user_user")
.from(Alias::new("channel_user"), Alias::new("user_id"))
.to(Alias::new("user"), Alias::new("id"))
.on_delete(ForeignKeyAction::Cascade)
)
.to_owned()
).await?;
// --------------------------------------------------------------------
// Création des INDEX après les tables
// --------------------------------------------------------------------
// category(server_id)
manager.create_index(
Index::create()
.name("idx_category_server_id")
.table(Alias::new("category"))
.col(Alias::new("server_id"))
.to_owned()
).await?;
// channel(server_id)
manager.create_index(
Index::create()
.name("idx_channel_server_id")
.table(Alias::new("channel"))
.col(Alias::new("server_id"))
.to_owned()
).await?;
// channel(category_id)
manager.create_index(
Index::create()
.name("idx_channel_category_id")
.table(Alias::new("channel"))
.col(Alias::new("category_id"))
.to_owned()
).await?;
// message(channel_id)
manager.create_index(
Index::create()
.name("idx_message_channel_id")
.table(Alias::new("message"))
.col(Alias::new("channel_id"))
.to_owned()
).await?;
// message(user_id)
manager.create_index(
Index::create()
.name("idx_message_user_id")
.table(Alias::new("message"))
.col(Alias::new("user_id"))
.to_owned()
).await?;
// message(reply_to_id)
manager.create_index(
Index::create()
.name("idx_message_reply_to_id")
.table(Alias::new("message"))
.col(Alias::new("reply_to_id"))
.to_owned()
).await?;
// attachment(message_id)
manager.create_index(
Index::create()
.name("idx_attachment_message_id")
.table(Alias::new("attachment"))
.col(Alias::new("message_id"))
.to_owned()
).await?;
// server_user(server_id)
manager.create_index(
Index::create()
.name("idx_server_user_server_id")
.table(Alias::new("server_user"))
.col(Alias::new("server_id"))
.to_owned()
).await?;
// server_user(user_id)
manager.create_index(
Index::create()
.name("idx_server_user_user_id")
.table(Alias::new("server_user"))
.col(Alias::new("user_id"))
.to_owned()
).await?;
// unique (server_id, user_id)
manager.create_index(
Index::create()
.name("uk_server_user_server_user")
.table(Alias::new("server_user"))
.col(Alias::new("server_id"))
.col(Alias::new("user_id"))
.unique()
.to_owned()
).await?;
// channel_user(channel_id)
manager.create_index(
Index::create()
.name("idx_channel_user_channel_id")
.table(Alias::new("channel_user"))
.col(Alias::new("channel_id"))
.to_owned()
).await?;
// channel_user(user_id)
manager.create_index(
Index::create()
.name("idx_channel_user_user_id")
.table(Alias::new("channel_user"))
.col(Alias::new("user_id"))
.to_owned()
).await?;
// unique (channel_id, user_id)
manager.create_index(
Index::create()
.name("uk_channel_user_channel_user")
.table(Alias::new("channel_user"))
.col(Alias::new("channel_id"))
.col(Alias::new("user_id"))
.unique()
.to_owned()
).await?;
Ok(())
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager.drop_table(Table::drop().table(Alias::new("channel_user")).to_owned()).await?;
manager.drop_table(Table::drop().table(Alias::new("server_user")).to_owned()).await?;
manager.drop_table(Table::drop().table(Alias::new("attachment")).to_owned()).await?;
manager.drop_table(Table::drop().table(Alias::new("message")).to_owned()).await?;
manager.drop_table(Table::drop().table(Alias::new("channel")).to_owned()).await?;
manager.drop_table(Table::drop().table(Alias::new("category")).to_owned()).await?;
manager.drop_table(Table::drop().table(Alias::new("user")).to_owned()).await?;
manager.drop_table(Table::drop().table(Alias::new("server")).to_owned()).await?;
Ok(())
}
}

6
migration/src/main.rs Normal file
View File

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