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,75 +1,65 @@
use pagetop::prelude::*;
#[derive(Iden)]
enum UserRole { Table,
Uid,
Rid,
enum UserRole {
Table, // Maps users to roles.
Uid, // Foreign Key: User::Uid for user.
Rid, // Foreign Key: Role::Rid for role.
}
#[derive(Iden)]
enum User { Table,
Uid,
// ...
}
enum User { Table, Uid, /* ... */ }
#[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(
// Maps users to roles.
Table::create()
.table(UserRole::Table)
.if_not_exists()
// Foreign Key: User::Uid for user.
.col(ColumnDef::new(UserRole::Uid)
.unsigned()
.not_null()
)
// Foreign Key: Role::Rid for role.
.col(ColumnDef::new(UserRole::Rid)
.unsigned()
.not_null()
)
// INDEXES.
.primary_key(Index::create()
.col(UserRole::Uid)
.col(UserRole::Rid)
)
.foreign_key(
ForeignKey::create()
.name("fk_user_role-uid")
.from(UserRole::Table, UserRole::Uid)
.to(User::Table, User::Uid)
.on_delete(ForeignKeyAction::Cascade)
.on_update(ForeignKeyAction::Cascade)
)
.foreign_key(
ForeignKey::create()
.name("fk_user_role-rid")
.from(UserRole::Table, UserRole::Rid)
.to(Role::Table, Role::Rid)
.on_delete(ForeignKeyAction::Cascade)
.on_update(ForeignKeyAction::Cascade)
)
.to_owned()
manager.create_table(Table::create()
.table(UserRole::Table)
.if_not_exists()
.col(ColumnDef::new(UserRole::Uid)
.unsigned()
.not_null()
)
.await
.col(ColumnDef::new(UserRole::Rid)
.unsigned()
.not_null()
)
// INDEXES.
.primary_key(Index::create()
.col(UserRole::Uid)
.col(UserRole::Rid)
)
.foreign_key(ForeignKey::create()
.name("fk_user_role-uid")
.from(UserRole::Table, UserRole::Uid)
.to(User::Table, User::Uid)
.on_delete(ForeignKeyAction::Cascade)
.on_update(ForeignKeyAction::Cascade)
)
.foreign_key(ForeignKey::create()
.name("fk_user_role-rid")
.from(UserRole::Table, UserRole::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(UserRole::Table)
.to_owned()
)
.await
manager.drop_table(Table::drop()
.table(UserRole::Table)
.to_owned()
)
.await
}
}