This commit is contained in:
2026-07-13 01:38:36 +02:00
parent ede38cc042
commit 09cf5f37fb
16 changed files with 747 additions and 644 deletions
+309 -322
View File
@@ -7,7 +7,7 @@ pub struct Migration;
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
// ---------------------------------------------------------------------
// Tables principales
// Utilisateurs
// ---------------------------------------------------------------------
manager
@@ -51,6 +51,10 @@ impl MigrationTrait for Migration {
)
.await?;
// ---------------------------------------------------------------------
// Serveurs
// ---------------------------------------------------------------------
manager
.create_table(
Table::create()
@@ -83,10 +87,149 @@ impl MigrationTrait for Migration {
.default(false),
)
.col(ColumnDef::new(Alias::new("owner_id")).uuid().null())
.foreign_key(
ForeignKey::create()
.name("fk_server_owner")
.from(Alias::new("server"), Alias::new("owner_id"))
.to(Alias::new("user"), Alias::new("id"))
.on_delete(ForeignKeyAction::SetNull),
)
.to_owned(),
)
.await?;
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()),
)
.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),
)
.index(
Index::create()
.name("uq_server_user")
.col(Alias::new("server_id"))
.col(Alias::new("user_id"))
.unique(),
)
.to_owned(),
)
.await?;
// ---------------------------------------------------------------------
// Rôles
// ---------------------------------------------------------------------
manager
.create_table(
Table::create()
.table(Alias::new("role"))
.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("is_default"))
.boolean()
.not_null()
.default(false),
)
.col(
ColumnDef::new(Alias::new("created_at"))
.timestamp_with_time_zone()
.not_null()
.default(Expr::current_timestamp()),
)
.foreign_key(
ForeignKey::create()
.name("fk_role_server")
.from(Alias::new("role"), Alias::new("server_id"))
.to(Alias::new("server"), Alias::new("id"))
.on_delete(ForeignKeyAction::Cascade),
)
.index(
Index::create()
.name("uq_role_server_name")
.col(Alias::new("server_id"))
.col(Alias::new("name"))
.unique(),
)
.to_owned(),
)
.await?;
manager
.create_table(
Table::create()
.table(Alias::new("role_user"))
.if_not_exists()
.col(ColumnDef::new(Alias::new("role_id")).uuid().not_null())
.col(ColumnDef::new(Alias::new("user_id")).uuid().not_null())
.primary_key(
Index::create()
.col(Alias::new("role_id"))
.col(Alias::new("user_id")),
)
.foreign_key(
ForeignKey::create()
.name("fk_role_user_role")
.from(Alias::new("role_user"), Alias::new("role_id"))
.to(Alias::new("role"), Alias::new("id"))
.on_delete(ForeignKeyAction::Cascade),
)
.foreign_key(
ForeignKey::create()
.name("fk_role_user_user")
.from(Alias::new("role_user"), Alias::new("user_id"))
.to(Alias::new("user"), Alias::new("id"))
.on_delete(ForeignKeyAction::Cascade),
)
.to_owned(),
)
.await?;
// ---------------------------------------------------------------------
// Catégories et canaux
// ---------------------------------------------------------------------
manager
.create_table(
Table::create()
@@ -123,8 +266,7 @@ impl MigrationTrait for Migration {
.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),
.on_delete(ForeignKeyAction::Cascade),
)
.to_owned(),
)
@@ -186,7 +328,61 @@ impl MigrationTrait for Migration {
.await?;
// ---------------------------------------------------------------------
// Contenu et relations utilisateurs
// Membres des canaux
// ---------------------------------------------------------------------
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"),
)
.col(
ColumnDef::new(Alias::new("joined_at"))
.timestamp_with_time_zone()
.not_null()
.default(Expr::current_timestamp()),
)
.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),
)
.index(
Index::create()
.name("uq_channel_user")
.col(Alias::new("channel_id"))
.col(Alias::new("user_id"))
.unique(),
)
.to_owned(),
)
.await?;
// ---------------------------------------------------------------------
// Messages et pièces jointes
// ---------------------------------------------------------------------
manager
@@ -276,10 +472,14 @@ impl MigrationTrait for Migration {
)
.await?;
// ---------------------------------------------------------------------
// Permissions
// ---------------------------------------------------------------------
manager
.create_table(
Table::create()
.table(Alias::new("server_user"))
.table(Alias::new("server_role_permission"))
.if_not_exists()
.col(
ColumnDef::new(Alias::new("id"))
@@ -288,34 +488,37 @@ impl MigrationTrait for Migration {
.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("role_id")).uuid().not_null())
.col(
ColumnDef::new(Alias::new("joined_at"))
.timestamp_with_time_zone()
ColumnDef::new(Alias::new("permission"))
.big_integer()
.not_null()
.default(Expr::current_timestamp()),
)
.col(
ColumnDef::new(Alias::new("updated_at"))
.timestamp_with_time_zone()
.not_null()
.default(Expr::current_timestamp()),
.default(0),
)
.foreign_key(
ForeignKey::create()
.name("fk_server_user_server")
.from(Alias::new("server_user"), Alias::new("server_id"))
.name("fk_server_role_permission_server")
.from(
Alias::new("server_role_permission"),
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"))
.name("fk_server_role_permission_role")
.from(Alias::new("server_role_permission"), Alias::new("role_id"))
.to(Alias::new("role"), Alias::new("id"))
.on_delete(ForeignKeyAction::Cascade),
)
.index(
Index::create()
.name("uq_server_role_permission")
.col(Alias::new("server_id"))
.col(Alias::new("role_id"))
.unique(),
)
.to_owned(),
)
.await?;
@@ -323,7 +526,54 @@ impl MigrationTrait for Migration {
manager
.create_table(
Table::create()
.table(Alias::new("channel_user"))
.table(Alias::new("channel_role_permission"))
.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("role_id")).uuid().not_null())
.col(
ColumnDef::new(Alias::new("permission"))
.big_integer()
.not_null()
.default(0),
)
.foreign_key(
ForeignKey::create()
.name("fk_channel_role_permission_channel")
.from(
Alias::new("channel_role_permission"),
Alias::new("channel_id"),
)
.to(Alias::new("channel"), Alias::new("id"))
.on_delete(ForeignKeyAction::Cascade),
)
.foreign_key(
ForeignKey::create()
.name("fk_channel_role_permission_role")
.from(Alias::new("channel_role_permission"), Alias::new("role_id"))
.to(Alias::new("role"), Alias::new("id"))
.on_delete(ForeignKeyAction::Cascade),
)
.index(
Index::create()
.name("uq_channel_role_permission")
.col(Alias::new("channel_id"))
.col(Alias::new("role_id"))
.unique(),
)
.to_owned(),
)
.await?;
manager
.create_table(
Table::create()
.table(Alias::new("channel_user_permission"))
.if_not_exists()
.col(
ColumnDef::new(Alias::new("id"))
@@ -334,226 +584,34 @@ impl MigrationTrait for Migration {
.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()
ColumnDef::new(Alias::new("permission"))
.big_integer()
.not_null()
.default("member"),
)
.col(
ColumnDef::new(Alias::new("joined_at"))
.timestamp_with_time_zone()
.not_null()
.default(Expr::current_timestamp()),
.default(0),
)
.foreign_key(
ForeignKey::create()
.name("fk_channel_user_channel")
.from(Alias::new("channel_user"), Alias::new("channel_id"))
.name("fk_channel_user_permission_channel")
.from(
Alias::new("channel_user_permission"),
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"))
.name("fk_channel_user_permission_user")
.from(Alias::new("channel_user_permission"), Alias::new("user_id"))
.to(Alias::new("user"), Alias::new("id"))
.on_delete(ForeignKeyAction::Cascade),
)
.to_owned(),
)
.await?;
// ---------------------------------------------------------------------
// Groupes et attributions
// ---------------------------------------------------------------------
manager
.create_table(
Table::create()
.table(Alias::new("group"))
.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("is_default"))
.boolean()
.not_null()
.default(false),
)
.col(
ColumnDef::new(Alias::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?;
manager
.create_table(
Table::create()
.table(Alias::new("group_member"))
.if_not_exists()
.col(ColumnDef::new(Alias::new("group_id")).uuid().not_null())
.col(ColumnDef::new(Alias::new("user_id")).uuid().not_null())
.primary_key(
.index(
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?;
// ---------------------------------------------------------------------
// Permissions sources
// ---------------------------------------------------------------------
manager
.create_table(
Table::create()
.table(Alias::new("group_permission"))
.if_not_exists()
.col(
ColumnDef::new(Alias::new("id"))
.uuid()
.not_null()
.primary_key(),
)
.col(ColumnDef::new(Alias::new("group_id")).uuid().not_null())
.col(ColumnDef::new(Alias::new("server_id")).uuid().not_null())
.col(
ColumnDef::new(Alias::new("scope_type"))
.integer()
.not_null(),
)
.col(ColumnDef::new(Alias::new("scope_id")).uuid().not_null())
.col(
ColumnDef::new(Alias::new("server_permissions"))
.big_integer()
.not_null()
.default(0),
)
.col(
ColumnDef::new(Alias::new("channel_permissions"))
.big_integer()
.not_null()
.default(0),
)
.col(
ColumnDef::new(Alias::new("voice_permissions"))
.big_integer()
.not_null()
.default(0),
)
.col(
ColumnDef::new(Alias::new("created_at"))
.timestamp_with_time_zone()
.not_null()
.default(Expr::current_timestamp()),
)
.foreign_key(
ForeignKey::create()
.name("fk_group_permission_group")
.from(Alias::new("group_permission"), Alias::new("group_id"))
.to(Alias::new("group"), Alias::new("id"))
.on_delete(ForeignKeyAction::Cascade),
)
.foreign_key(
ForeignKey::create()
.name("fk_group_permission_server")
.from(Alias::new("group_permission"), Alias::new("server_id"))
.to(Alias::new("server"), Alias::new("id"))
.on_delete(ForeignKeyAction::Cascade),
)
.to_owned(),
)
.await?;
manager
.create_table(
Table::create()
.table(Alias::new("user_permission"))
.if_not_exists()
.col(
ColumnDef::new(Alias::new("id"))
.uuid()
.not_null()
.primary_key(),
)
.col(ColumnDef::new(Alias::new("user_id")).uuid().not_null())
.col(ColumnDef::new(Alias::new("server_id")).uuid().not_null())
.col(
ColumnDef::new(Alias::new("scope_type"))
.integer()
.not_null(),
)
.col(ColumnDef::new(Alias::new("scope_id")).uuid().not_null())
.col(
ColumnDef::new(Alias::new("server_permissions"))
.big_integer()
.not_null()
.default(0),
)
.col(
ColumnDef::new(Alias::new("channel_permissions"))
.big_integer()
.not_null()
.default(0),
)
.col(
ColumnDef::new(Alias::new("voice_permissions"))
.big_integer()
.not_null()
.default(0),
)
.col(
ColumnDef::new(Alias::new("created_at"))
.timestamp_with_time_zone()
.not_null()
.default(Expr::current_timestamp()),
)
.foreign_key(
ForeignKey::create()
.name("fk_user_permission_user")
.from(Alias::new("user_permission"), Alias::new("user_id"))
.to(Alias::new("user"), Alias::new("id"))
.on_delete(ForeignKeyAction::Cascade),
)
.foreign_key(
ForeignKey::create()
.name("fk_user_permission_server")
.from(Alias::new("user_permission"), Alias::new("server_id"))
.to(Alias::new("server"), Alias::new("id"))
.on_delete(ForeignKeyAction::Cascade),
.name("uq_channel_user_permission")
.col(Alias::new("channel_id"))
.col(Alias::new("user_id"))
.unique(),
)
.to_owned(),
)
@@ -597,6 +655,7 @@ impl MigrationTrait for Migration {
.primary_key(
Index::create()
.col(Alias::new("user_id"))
.col(Alias::new("server_id"))
.col(Alias::new("scope_type"))
.col(Alias::new("resource_id")),
)
@@ -607,92 +666,13 @@ impl MigrationTrait for Migration {
.to(Alias::new("user"), Alias::new("id"))
.on_delete(ForeignKeyAction::Cascade),
)
.to_owned(),
)
.await?;
// ---------------------------------------------------------------------
// Index
// ---------------------------------------------------------------------
let indexes = [
("idx_category_server_id", "category", "server_id"),
("idx_channel_server_id", "channel", "server_id"),
("idx_channel_category_id", "channel", "category_id"),
("idx_message_channel_id", "message", "channel_id"),
("idx_message_user_id", "message", "user_id"),
("idx_attachment_message_id", "attachment", "message_id"),
("idx_server_user_server_id", "server_user", "server_id"),
("idx_server_user_user_id", "server_user", "user_id"),
("idx_channel_user_channel_id", "channel_user", "channel_id"),
("idx_channel_user_user_id", "channel_user", "user_id"),
("idx_group_server_id", "group", "server_id"),
("idx_group_member_user_id", "group_member", "user_id"),
];
for (name, table, column) in indexes {
manager
.create_index(
Index::create()
.name(name)
.table(Alias::new(table))
.col(Alias::new(column))
.to_owned(),
)
.await?;
}
manager
.create_index(
Index::create()
.name("idx_group_permission_group_scope")
.table(Alias::new("group_permission"))
.col(Alias::new("group_id"))
.col(Alias::new("scope_type"))
.col(Alias::new("scope_id"))
.to_owned(),
)
.await?;
manager
.create_index(
Index::create()
.name("idx_group_permission_server")
.table(Alias::new("group_permission"))
.col(Alias::new("server_id"))
.to_owned(),
)
.await?;
manager
.create_index(
Index::create()
.name("idx_user_permission_user_scope")
.table(Alias::new("user_permission"))
.col(Alias::new("user_id"))
.col(Alias::new("scope_type"))
.col(Alias::new("scope_id"))
.to_owned(),
)
.await?;
manager
.create_index(
Index::create()
.name("idx_user_permission_server")
.table(Alias::new("user_permission"))
.col(Alias::new("server_id"))
.to_owned(),
)
.await?;
manager
.create_index(
Index::create()
.name("idx_computed_permission_resource")
.table(Alias::new("computed_permission"))
.col(Alias::new("scope_type"))
.col(Alias::new("resource_id"))
.foreign_key(
ForeignKey::create()
.name("fk_computed_permission_server")
.from(Alias::new("computed_permission"), Alias::new("server_id"))
.to(Alias::new("server"), Alias::new("id"))
.on_delete(ForeignKeyAction::Cascade),
)
.to_owned(),
)
.await?;
@@ -701,25 +681,32 @@ impl MigrationTrait for Migration {
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
// Les tables dépendantes doivent être supprimées avant leurs parents.
let tables = [
"computed_permission",
"user_permission",
"group_permission",
"group_member",
"channel_user",
"server_user",
"channel_user_permission",
"channel_role_permission",
"server_role_permission",
"attachment",
"message",
"channel_user",
"channel",
"category",
"group",
"role_user",
"role",
"server_user",
"server",
"user",
];
for table in tables {
manager
.drop_table(Table::drop().table(Alias::new(table)).to_owned())
.drop_table(
Table::drop()
.table(Alias::new(table))
.if_exists()
.to_owned(),
)
.await?;
}