Añade funciones de acceso básico a base de datos

This commit is contained in:
Manuel Cillero 2022-04-24 18:44:37 +02:00
parent d4c6a3b2f3
commit e7f9bf8778
17 changed files with 99 additions and 75 deletions

View file

@ -23,7 +23,7 @@ impl ModuleTrait for UserModule {
cfg.route("/user/login", app::web::get().to(login));
}
fn migrations(&self) -> Vec<Box<dyn db::migration::MigrationTrait>> {
fn migrations(&self) -> Vec<Box<dyn db::MigrationTrait>> {
vec![
boxed_migration!(m20220312_000001_create_table_role),
boxed_migration!(m20220312_000002_create_table_role_permission),

View file

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

View file

@ -1,4 +1,4 @@
use pagetop::db::migration::*;
use pagetop::prelude::*;
#[derive(Iden)]
enum RolePermission { Table,

View file

@ -1,4 +1,4 @@
use pagetop::db::migration::*;
use pagetop::prelude::*;
#[derive(Iden)]
enum User { Table,

View file

@ -1,4 +1,4 @@
use pagetop::db::migration::*;
use pagetop::prelude::*;
#[derive(Iden)]
enum UserRole { Table,