This commit is contained in:
2026-05-03 16:24:47 +02:00
commit 47c33a3a6c
35 changed files with 7288 additions and 0 deletions
+12
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)]
}
}
@@ -0,0 +1,675 @@
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> {
// 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"))
.timestamp_with_time_zone()
.not_null()
.default(Expr::current_timestamp()),
)
.col(
ColumnDef::new(Alias::new("updated_at"))
.timestamp_with_time_zone()
.not_null()
.default(Expr::current_timestamp()),
)
.col(
ColumnDef::new(Alias::new("default_permissions"))
.big_integer()
.not_null()
.default(0),
)
.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("position"))
.integer()
.not_null()
.default(0),
)
.col(
ColumnDef::new(Alias::new("created_at"))
.timestamp_with_time_zone()
.not_null()
.default(Expr::current_timestamp()),
)
.col(
ColumnDef::new(Alias::new("updated_at"))
.timestamp_with_time_zone()
.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"))
.timestamp_with_time_zone()
.not_null()
.default(Expr::current_timestamp()),
)
.col(
ColumnDef::new(Alias::new("updated_at"))
.timestamp_with_time_zone()
.not_null()
.default(Expr::current_timestamp()),
)
.col(
ColumnDef::new(Alias::new("default_permissions"))
.big_integer()
.null(),
)
// 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("password")).string().not_null())
.col(
ColumnDef::new(Alias::new("pub_key"))
.text()
.not_null()
.unique_key(),
)
.col(
ColumnDef::new(Alias::new("created_at"))
.timestamp_with_time_zone()
.not_null()
.default(Expr::current_timestamp()),
)
.col(
ColumnDef::new(Alias::new("updated_at"))
.timestamp_with_time_zone()
.not_null()
.default(Expr::current_timestamp()),
)
.col(
ColumnDef::new(Alias::new("is_superuser"))
.boolean()
.not_null()
.default(false),
)
.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"))
.timestamp_with_time_zone()
.not_null()
.default(Expr::current_timestamp()),
)
.col(
ColumnDef::new(Alias::new("updated_at"))
.timestamp_with_time_zone()
.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"))
.timestamp_with_time_zone()
.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"))
.timestamp_with_time_zone()
.not_null()
.default(Expr::current_timestamp()),
)
.col(
ColumnDef::new(Alias::new("updated_at"))
.timestamp_with_time_zone()
.not_null()
.default(Expr::current_timestamp()),
)
.col(
ColumnDef::new(Alias::new("is_admin"))
.boolean()
.not_null()
.default(false),
)
.col(
ColumnDef::new(Alias::new("is_owner"))
.boolean()
.not_null()
.default(false),
)
.col(
ColumnDef::new(Alias::new("permissions"))
.big_integer()
.not_null()
.default(0),
)
// 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("permissions"))
.big_integer()
.not_null()
.default(0),
)
.col(
ColumnDef::new(Alias::new("joined_at"))
.timestamp_with_time_zone()
.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?;
// Create table `group`
manager
.create_table(
Table::create()
.table(Alias::new("group"))
.if_not_exists()
.col(ColumnDef::new("id").uuid().primary_key().not_null())
.col(ColumnDef::new("server_id").uuid().not_null())
.col(ColumnDef::new("name").string().not_null())
.col(
ColumnDef::new("permissions")
.big_integer()
.not_null()
.default(0),
)
.col(
ColumnDef::new("is_default")
.boolean()
.not_null()
.default(false),
)
.col(
ColumnDef::new("created_at")
.timestamp_with_time_zone()
.not_null()
.default(Expr::current_timestamp()),
)
.foreign_key(
ForeignKey::create()
.name("fk_group_server")
.from(Alias::new("group"), Alias::new("server_id"))
.to(Alias::new("server"), Alias::new("id"))
.on_delete(ForeignKeyAction::Cascade),
)
.to_owned(),
)
.await?;
// Create table `group_member`
manager
.create_table(
Table::create()
.table(Alias::new("group_member"))
.if_not_exists()
.col(ColumnDef::new("group_id").uuid().not_null())
.col(ColumnDef::new("user_id").uuid().not_null())
.primary_key(
Index::create()
.col(Alias::new("group_id"))
.col(Alias::new("user_id")),
)
.foreign_key(
ForeignKey::create()
.name("fk_group_member_group")
.from(Alias::new("group_member"), Alias::new("group_id"))
.to(Alias::new("group"), Alias::new("id"))
.on_delete(ForeignKeyAction::Cascade),
)
.foreign_key(
ForeignKey::create()
.name("fk_group_member_user")
.from(Alias::new("group_member"), Alias::new("user_id"))
.to(Alias::new("user"), Alias::new("id"))
.on_delete(ForeignKeyAction::Cascade),
)
.to_owned(),
)
.await?;
// Index: idx_group_server_id
manager
.create_index(
Index::create()
.name("idx_group_server_id")
.table(Alias::new("group"))
.col(Alias::new("server_id"))
.to_owned(),
)
.await?;
// Index: idx_group_member_user_id
manager
.create_index(
Index::create()
.name("idx_group_member_user_id")
.table(Alias::new("group_member"))
.col(Alias::new("user_id"))
.to_owned(),
)
.await?;
Ok(())
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(Alias::new("group_member")).to_owned())
.await?;
manager
.drop_table(Table::drop().table(Alias::new("group")).to_owned())
.await?;
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
View File
@@ -0,0 +1,6 @@
use sea_orm_migration::prelude::*;
#[async_std::main]
async fn main() {
cli::run_cli(migration::Migrator).await;
}