init
This commit is contained in:
@@ -6,141 +6,10 @@ 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("is_default"))
|
||||
.boolean()
|
||||
.not_null()
|
||||
.default(false),
|
||||
)
|
||||
.col(ColumnDef::new(Alias::new("owner_id")).uuid().not_null())
|
||||
.to_owned(),
|
||||
)
|
||||
.await?;
|
||||
// ---------------------------------------------------------------------
|
||||
// Tables principales
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
// 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()),
|
||||
)
|
||||
// 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()
|
||||
@@ -182,7 +51,144 @@ impl MigrationTrait for Migration {
|
||||
)
|
||||
.await?;
|
||||
|
||||
// Create table `message`
|
||||
manager
|
||||
.create_table(
|
||||
Table::create()
|
||||
.table(Alias::new("server"))
|
||||
.if_not_exists()
|
||||
.col(
|
||||
ColumnDef::new(Alias::new("id"))
|
||||
.uuid()
|
||||
.not_null()
|
||||
.primary_key(),
|
||||
)
|
||||
.col(ColumnDef::new(Alias::new("name")).string().not_null())
|
||||
.col(ColumnDef::new(Alias::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("is_default"))
|
||||
.boolean()
|
||||
.not_null()
|
||||
.default(false),
|
||||
)
|
||||
.col(ColumnDef::new(Alias::new("owner_id")).uuid().not_null())
|
||||
.to_owned(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
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()),
|
||||
)
|
||||
.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?;
|
||||
|
||||
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()),
|
||||
)
|
||||
.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?;
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// Contenu et relations utilisateurs
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
manager
|
||||
.create_table(
|
||||
Table::create()
|
||||
@@ -209,7 +215,6 @@ impl MigrationTrait for Migration {
|
||||
.null(),
|
||||
)
|
||||
.col(ColumnDef::new(Alias::new("reply_to_id")).uuid().null())
|
||||
// Indexes créés après
|
||||
.foreign_key(
|
||||
ForeignKey::create()
|
||||
.name("fk_message_channel")
|
||||
@@ -235,7 +240,6 @@ impl MigrationTrait for Migration {
|
||||
)
|
||||
.await?;
|
||||
|
||||
// Create table `attachment`
|
||||
manager
|
||||
.create_table(
|
||||
Table::create()
|
||||
@@ -261,7 +265,6 @@ impl MigrationTrait for Migration {
|
||||
.not_null()
|
||||
.default(Expr::current_timestamp()),
|
||||
)
|
||||
// Index créé après
|
||||
.foreign_key(
|
||||
ForeignKey::create()
|
||||
.name("fk_attachment_message")
|
||||
@@ -273,7 +276,6 @@ impl MigrationTrait for Migration {
|
||||
)
|
||||
.await?;
|
||||
|
||||
// Create M2M table `server_user`
|
||||
manager
|
||||
.create_table(
|
||||
Table::create()
|
||||
@@ -300,7 +302,6 @@ impl MigrationTrait for Migration {
|
||||
.not_null()
|
||||
.default(Expr::current_timestamp()),
|
||||
)
|
||||
// Indexes créés après
|
||||
.foreign_key(
|
||||
ForeignKey::create()
|
||||
.name("fk_server_user_server")
|
||||
@@ -319,7 +320,6 @@ impl MigrationTrait for Migration {
|
||||
)
|
||||
.await?;
|
||||
|
||||
// Create M2M table `channel_user`
|
||||
manager
|
||||
.create_table(
|
||||
Table::create()
|
||||
@@ -333,13 +333,18 @@ 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()
|
||||
.not_null()
|
||||
.default("member"),
|
||||
)
|
||||
.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")
|
||||
@@ -358,202 +363,35 @@ impl MigrationTrait for Migration {
|
||||
)
|
||||
.await?;
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// Création des INDEX après les tables
|
||||
// --------------------------------------------------------------------
|
||||
// ---------------------------------------------------------------------
|
||||
// Groupes et attributions
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
// 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(Alias::new("server_permissions"))
|
||||
.big_integer()
|
||||
ColumnDef::new(Alias::new("id"))
|
||||
.uuid()
|
||||
.not_null()
|
||||
.default(0),
|
||||
.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("channel_permissions"))
|
||||
.big_integer()
|
||||
.not_null()
|
||||
.default(0),
|
||||
)
|
||||
.col(
|
||||
ColumnDef::new(Alias::new("voice_permissions"))
|
||||
.big_integer()
|
||||
.not_null()
|
||||
.default(0),
|
||||
)
|
||||
.col(
|
||||
ColumnDef::new("is_default")
|
||||
ColumnDef::new(Alias::new("is_default"))
|
||||
.boolean()
|
||||
.not_null()
|
||||
.default(false),
|
||||
)
|
||||
.col(
|
||||
ColumnDef::new("created_at")
|
||||
ColumnDef::new(Alias::new("created_at"))
|
||||
.timestamp_with_time_zone()
|
||||
.not_null()
|
||||
.default(Expr::current_timestamp()),
|
||||
)
|
||||
.col(
|
||||
ColumnDef::new("edited_at")
|
||||
.timestamp_with_time_zone()
|
||||
.null()
|
||||
.default(Expr::current_timestamp()),
|
||||
)
|
||||
.foreign_key(
|
||||
ForeignKey::create()
|
||||
.name("fk_group_server")
|
||||
@@ -565,16 +403,13 @@ impl MigrationTrait for Migration {
|
||||
)
|
||||
.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())
|
||||
.col(ColumnDef::new("scope_type").integer().not_null())
|
||||
.col(ColumnDef::new("scope_id").uuid().not_null())
|
||||
.col(ColumnDef::new(Alias::new("group_id")).uuid().not_null())
|
||||
.col(ColumnDef::new(Alias::new("user_id")).uuid().not_null())
|
||||
.primary_key(
|
||||
Index::create()
|
||||
.col(Alias::new("group_id"))
|
||||
@@ -598,39 +433,29 @@ impl MigrationTrait for Migration {
|
||||
)
|
||||
.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?;
|
||||
// ---------------------------------------------------------------------
|
||||
// Permissions sources
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
// 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?;
|
||||
|
||||
// Create table `user_permission`
|
||||
manager
|
||||
.create_table(
|
||||
Table::create()
|
||||
.table(Alias::new("user_permission"))
|
||||
.table(Alias::new("group_permission"))
|
||||
.if_not_exists()
|
||||
.col(ColumnDef::new("id").uuid().primary_key().not_null())
|
||||
.col(ColumnDef::new("user_id").uuid().not_null())
|
||||
.col(ColumnDef::new("server_id").uuid().not_null())
|
||||
.col(ColumnDef::new("scope_type").integer().not_null())
|
||||
.col(ColumnDef::new("scope_id").uuid().not_null())
|
||||
.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()
|
||||
@@ -650,7 +475,68 @@ impl MigrationTrait for Migration {
|
||||
.default(0),
|
||||
)
|
||||
.col(
|
||||
ColumnDef::new("created_at")
|
||||
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()),
|
||||
@@ -673,14 +559,22 @@ impl MigrationTrait for Migration {
|
||||
)
|
||||
.await?;
|
||||
|
||||
// Create CACHE table `computed_permission`
|
||||
// ---------------------------------------------------------------------
|
||||
// Cache des permissions calculées
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
manager
|
||||
.create_table(
|
||||
Table::create()
|
||||
.table(Alias::new("computed_permission"))
|
||||
.if_not_exists()
|
||||
.col(ColumnDef::new("user_id").uuid().not_null())
|
||||
.col(ColumnDef::new("resource_id").uuid().not_null())
|
||||
.col(ColumnDef::new(Alias::new("user_id")).uuid().not_null())
|
||||
.col(
|
||||
ColumnDef::new(Alias::new("scope_type"))
|
||||
.integer()
|
||||
.not_null(),
|
||||
)
|
||||
.col(ColumnDef::new(Alias::new("resource_id")).uuid().not_null())
|
||||
.col(
|
||||
ColumnDef::new(Alias::new("server_permissions"))
|
||||
.big_integer()
|
||||
@@ -702,6 +596,7 @@ impl MigrationTrait for Migration {
|
||||
.primary_key(
|
||||
Index::create()
|
||||
.col(Alias::new("user_id"))
|
||||
.col(Alias::new("scope_type"))
|
||||
.col(Alias::new("resource_id")),
|
||||
)
|
||||
.foreign_key(
|
||||
@@ -715,41 +610,117 @@ impl MigrationTrait for Migration {
|
||||
)
|
||||
.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"))
|
||||
.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?;
|
||||
let tables = [
|
||||
"computed_permission",
|
||||
"user_permission",
|
||||
"group_permission",
|
||||
"group_member",
|
||||
"channel_user",
|
||||
"server_user",
|
||||
"attachment",
|
||||
"message",
|
||||
"channel",
|
||||
"category",
|
||||
"group",
|
||||
"server",
|
||||
"user",
|
||||
];
|
||||
|
||||
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?;
|
||||
for table in tables {
|
||||
manager
|
||||
.drop_table(Table::drop().table(Alias::new(table)).to_owned())
|
||||
.await?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user