pagetop/packages/pagetop-user/src/migration/m20220312_000001_create_table_role.rs
2024-02-09 14:05:38 +01:00

71 lines
2.3 KiB
Rust

use pagetop::prelude::*;
#[rustfmt::skip]
#[derive(Iden)]
enum Role {
Table, // role: Store user roles.
Rid, // Primary Key: Unique role ID.
Name, // Unique role name.
Weight, // The weight of this role in listings and the user interface.
}
new_migration!(Migration);
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.create_table(
Table::create()
.table(Role::Table)
.if_not_exists()
.col(
ColumnDef::new(Role::Rid)
.unsigned()
.not_null()
.auto_increment()
.primary_key(),
)
.col(
ColumnDef::new(Role::Name)
.string_len(64)
.not_null()
.unique_key(),
)
.col(
ColumnDef::new(Role::Weight)
.integer()
.not_null()
.default(10),
)
// INDEXES.
.index(
Index::create()
.name("weight-name")
.col(Role::Weight)
.col(Role::Name),
)
.to_owned(),
)
.await?;
// Built-in roles.
db::exec::<InsertStatement>(
Query::insert()
.into_table(Role::Table)
.columns(vec![Role::Name, Role::Weight])
.values_panic(vec!["anonymous".into(), "1".into()])
.values_panic(vec!["authenticated".into(), "2".into()])
.values_panic(vec!["administrator".into(), "3".into()]),
)
.await
.map(|_| ())
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(Role::Table).to_owned())
.await
}
}