Actualiza formato del código de las migraciones

This commit is contained in:
Manuel Cillero 2022-04-24 21:57:07 +02:00
parent e7f9bf8778
commit f30f84e993
8 changed files with 486 additions and 512 deletions

View file

@ -1,72 +1,64 @@
use pagetop::prelude::*;
#[derive(Iden)]
enum RolePermission { Table,
Rid,
Permission,
Module,
enum RolePermission {
Table, // Stores the permissions assigned to user roles.
Rid, // Foreign Key: Role::Rid.
Permission, // A single permission granted to the role identified by Rid.
Module, // The module declaring the permission.
}
#[derive(Iden)]
enum Role { Table,
Rid,
// ...
}
enum Role { Table, Rid, /* ... */ }
pub struct Migration;
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.create_table(
// Stores the permissions assigned to user roles.
Table::create()
.table(RolePermission::Table)
.if_not_exists()
// Foreign Key: Role::Rid.
.col(ColumnDef::new(RolePermission::Rid)
.unsigned()
.not_null()
)
// A single permission granted to the role identified by Rid.
.col(ColumnDef::new(RolePermission::Permission)
.string_len(128)
.not_null()
)
// The module declaring the permission.
.col(ColumnDef::new(RolePermission::Module)
.string_len(255)
.not_null()
)
// INDEXES.
.primary_key(Index::create()
.col(RolePermission::Rid)
.col(RolePermission::Permission)
)
.index(Index::create()
.name("permission")
.col(RolePermission::Permission)
)
.foreign_key(
ForeignKey::create()
.name("fk_role_permission-rid")
.from(RolePermission::Table, RolePermission::Rid)
.to(Role::Table, Role::Rid)
.on_delete(ForeignKeyAction::Cascade)
.on_update(ForeignKeyAction::Cascade)
)
.to_owned()
manager.create_table(Table::create()
.table(RolePermission::Table)
.if_not_exists()
.col(ColumnDef::new(RolePermission::Rid)
.unsigned()
.not_null()
)
.await
.col(ColumnDef::new(RolePermission::Permission)
.string_len(128)
.not_null()
)
.col(ColumnDef::new(RolePermission::Module)
.string_len(255)
.not_null()
)
// INDEXES.
.primary_key(Index::create()
.col(RolePermission::Rid)
.col(RolePermission::Permission)
)
.index(Index::create()
.name("permission")
.col(RolePermission::Permission)
)
.foreign_key(ForeignKey::create()
.name("fk_role_permission-rid")
.from(RolePermission::Table, RolePermission::Rid)
.to(Role::Table, Role::Rid)
.on_delete(ForeignKeyAction::Cascade)
.on_update(ForeignKeyAction::Cascade)
)
.to_owned()
)
.await
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop()
.table(RolePermission::Table)
.to_owned()
)
.await
manager.drop_table(Table::drop()
.table(RolePermission::Table)
.to_owned()
)
.await
}
}