Añade la creación de tablas asociadas a usuarios
This commit is contained in:
parent
d37b2e09ab
commit
4af6586003
7 changed files with 327 additions and 56 deletions
|
|
@ -26,7 +26,10 @@ impl ModuleTrait for UserModule {
|
|||
|
||||
fn migrations(&self) -> Vec<Box<dyn db::migration::MigrationTrait>> {
|
||||
vec![
|
||||
boxed_migration!(m20220312_000001_create_table_user)
|
||||
boxed_migration!(m20220312_000001_create_table_role),
|
||||
boxed_migration!(m20220312_000002_create_table_role_permission),
|
||||
boxed_migration!(m20220312_000003_create_table_user),
|
||||
boxed_migration!(m20220312_000004_create_table_user_role),
|
||||
]
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,65 @@
|
|||
use pagetop::db::migration::*;
|
||||
|
||||
#[derive(Iden)]
|
||||
enum Role { Table,
|
||||
Rid,
|
||||
Name,
|
||||
Weight,
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||
manager
|
||||
.drop_table(Table::drop()
|
||||
.table(Role::Table)
|
||||
.to_owned()
|
||||
)
|
||||
.await
|
||||
}
|
||||
}
|
||||
|
||||
impl MigrationName for Migration {
|
||||
fn name(&self) -> &str {
|
||||
module_name!()
|
||||
}
|
||||
}
|
||||
|
|
@ -1,54 +0,0 @@
|
|||
use crate::db::migration::*;
|
||||
|
||||
#[derive(Iden)]
|
||||
enum User {
|
||||
Table,
|
||||
Id,
|
||||
Title,
|
||||
Text,
|
||||
}
|
||||
|
||||
pub struct Migration;
|
||||
|
||||
impl MigrationName for Migration {
|
||||
fn name(&self) -> &str {
|
||||
"m20220312_000001_create_table_user"
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl MigrationTrait for Migration {
|
||||
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||
manager
|
||||
.create_table(
|
||||
Table::create()
|
||||
.table(User::Table)
|
||||
.if_not_exists()
|
||||
.col(ColumnDef::new(User::Id)
|
||||
.integer()
|
||||
.not_null()
|
||||
.auto_increment()
|
||||
.primary_key(),
|
||||
)
|
||||
.col(ColumnDef::new(User::Title)
|
||||
.string()
|
||||
.not_null()
|
||||
)
|
||||
.col(ColumnDef::new(User::Text)
|
||||
.string()
|
||||
.not_null()
|
||||
)
|
||||
.to_owned()
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||
manager
|
||||
.drop_table(Table::drop()
|
||||
.table(User::Table)
|
||||
.to_owned()
|
||||
)
|
||||
.await
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,77 @@
|
|||
use pagetop::db::migration::*;
|
||||
|
||||
#[derive(Iden)]
|
||||
enum RolePermission { Table,
|
||||
Rid,
|
||||
Permission,
|
||||
Module,
|
||||
}
|
||||
#[derive(Iden)]
|
||||
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()
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||
manager
|
||||
.drop_table(Table::drop()
|
||||
.table(RolePermission::Table)
|
||||
.to_owned()
|
||||
)
|
||||
.await
|
||||
}
|
||||
}
|
||||
|
||||
impl MigrationName for Migration {
|
||||
fn name(&self) -> &str {
|
||||
module_name!()
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,97 @@
|
|||
use pagetop::db::migration::*;
|
||||
|
||||
#[derive(Iden)]
|
||||
enum User { Table,
|
||||
Uid,
|
||||
Name,
|
||||
Pass,
|
||||
Mail,
|
||||
Created,
|
||||
Changed,
|
||||
Access,
|
||||
Login,
|
||||
Status,
|
||||
Timezone,
|
||||
}
|
||||
|
||||
pub struct Migration;
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl MigrationTrait for Migration {
|
||||
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||
manager
|
||||
.create_table(
|
||||
// Stores user data.
|
||||
Table::create()
|
||||
.table(User::Table)
|
||||
.if_not_exists()
|
||||
// Primary Key: Unique user ID.
|
||||
.col(ColumnDef::new(User::Uid)
|
||||
.unsigned()
|
||||
.not_null()
|
||||
.primary_key()
|
||||
)
|
||||
// Unique user name.
|
||||
.col(ColumnDef::new(User::Name)
|
||||
.string_len(60)
|
||||
.not_null()
|
||||
.unique_key()
|
||||
)
|
||||
// User's password (hashed).
|
||||
.col(ColumnDef::new(User::Pass)
|
||||
.string_len(128)
|
||||
.not_null()
|
||||
)
|
||||
// User's e-mail address.
|
||||
.col(ColumnDef::new(User::Mail)
|
||||
.string_len(255)
|
||||
)
|
||||
// Timestamp for when user was created.
|
||||
.col(ColumnDef::new(User::Created)
|
||||
.timestamp()
|
||||
.not_null()
|
||||
)
|
||||
// Timestamp for when user was changed.
|
||||
.col(ColumnDef::new(User::Changed)
|
||||
.timestamp()
|
||||
.not_null()
|
||||
)
|
||||
// Timestamp for previous time user accessed the site.
|
||||
.col(ColumnDef::new(User::Access)
|
||||
.timestamp()
|
||||
.not_null()
|
||||
)
|
||||
// Timestamp for user's last login.
|
||||
.col(ColumnDef::new(User::Login)
|
||||
.timestamp()
|
||||
.not_null()
|
||||
)
|
||||
// Whether the user is active(1) or blocked(0).
|
||||
.col(ColumnDef::new(User::Status)
|
||||
.boolean()
|
||||
.not_null()
|
||||
)
|
||||
// User's time zone.
|
||||
.col(ColumnDef::new(User::Timezone)
|
||||
.string_len(32)
|
||||
)
|
||||
.to_owned()
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||
manager
|
||||
.drop_table(Table::drop()
|
||||
.table(User::Table)
|
||||
.to_owned()
|
||||
)
|
||||
.await
|
||||
}
|
||||
}
|
||||
|
||||
impl MigrationName for Migration {
|
||||
fn name(&self) -> &str {
|
||||
module_name!()
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,80 @@
|
|||
use pagetop::db::migration::*;
|
||||
|
||||
#[derive(Iden)]
|
||||
enum UserRole { Table,
|
||||
Uid,
|
||||
Rid,
|
||||
}
|
||||
#[derive(Iden)]
|
||||
enum User { Table,
|
||||
Uid,
|
||||
// ...
|
||||
}
|
||||
#[derive(Iden)]
|
||||
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()
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||
manager
|
||||
.drop_table(Table::drop()
|
||||
.table(UserRole::Table)
|
||||
.to_owned()
|
||||
)
|
||||
.await
|
||||
}
|
||||
}
|
||||
|
||||
impl MigrationName for Migration {
|
||||
fn name(&self) -> &str {
|
||||
module_name!()
|
||||
}
|
||||
}
|
||||
|
|
@ -1 +1,4 @@
|
|||
pub mod m20220312_000001_create_table_user;
|
||||
pub mod m20220312_000001_create_table_role;
|
||||
pub mod m20220312_000002_create_table_role_permission;
|
||||
pub mod m20220312_000003_create_table_user;
|
||||
pub mod m20220312_000004_create_table_user_role;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue