add models

This commit is contained in:
guochao 2022-08-06 11:56:56 +08:00
parent 92979a7391
commit 4dbd95fd4b
Signed by: guochao
GPG Key ID: 79F7306D2AA32FC3
9 changed files with 247 additions and 45 deletions

View File

@ -0,0 +1,52 @@
use sea_orm::sea_query::Table;
use sea_orm_migration::prelude::*;
use sea_orm_migration::{MigrationName, MigrationTrait};
pub struct Migration;
impl MigrationName for Migration {
fn name(&self) -> &str {
"m20220804_000001_create_audit_table"
}
}
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &sea_orm_migration::SchemaManager) -> Result<(), sea_orm::DbErr> {
manager
.create_table(
Table::create()
.table(Audit::Table)
.col(
ColumnDef::new(Audit::Id)
.integer()
.primary_key()
.auto_increment()
.not_null(),
)
.col(ColumnDef::new(Audit::CreatedAt).date_time().not_null())
.col(ColumnDef::new(Audit::Source).string().not_null())
.col(ColumnDef::new(Audit::Type).string().not_null())
.col(ColumnDef::new(Audit::Description).string().not_null())
.to_owned(),
)
.await
}
async fn down(&self, manager: &sea_orm_migration::SchemaManager) -> Result<(), sea_orm::DbErr> {
manager
.drop_table(Table::drop().table(Audit::Table).to_owned())
.await
}
}
#[derive(Iden)]
pub enum Audit {
Table,
CreatedAt,
Id,
Source,
Type,
Description,
}

View File

@ -1,37 +0,0 @@
use sea_orm::{sea_query::Table};
use sea_orm_migration::{MigrationTrait, MigrationName};
use sea_orm_migration::prelude::*;
pub struct Migration;
impl MigrationName for Migration {
fn name(&self) -> &str {
"m20220804_000001_create_user_table"
}
}
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: & sea_orm_migration::SchemaManager) -> Result<(),sea_orm::DbErr> {
manager.create_table(
Table::create()
.table(User::Table)
.col(ColumnDef::new(User::Id).integer().primary_key().auto_increment().not_null())
.col(ColumnDef::new(User::Name).string().unique_key().not_null())
.col(ColumnDef::new(User::Password).string().not_null())
.to_owned()
).await
}
async fn down(&self, manager: & sea_orm_migration::SchemaManager) -> Result<(),sea_orm::DbErr> {
manager.drop_table(Table::drop().table(User::Table).to_owned()).await
}
}
#[derive(Iden)]
pub enum User {
Table,
Id,
Name,
Password,
}

View File

@ -0,0 +1,115 @@
use sea_orm::sea_query::Table;
use sea_orm_migration::prelude::*;
use sea_orm_migration::{MigrationName, MigrationTrait};
pub struct Migration;
impl MigrationName for Migration {
fn name(&self) -> &str {
"m20220804_000002_create_user_table"
}
}
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &sea_orm_migration::SchemaManager) -> Result<(), sea_orm::DbErr> {
manager
.create_table(
Table::create()
.table(User::Table)
.col(
ColumnDef::new(User::Id)
.integer()
.primary_key()
.auto_increment()
.not_null(),
)
.col(ColumnDef::new(User::CreatedAt).date_time().not_null())
.col(ColumnDef::new(User::UpdatedAt).date_time().not_null())
.col(ColumnDef::new(User::DeletedAt).date_time().not_null())
.col(ColumnDef::new(User::Name).string().unique_key().not_null())
.col(
ColumnDef::new(User::Enabled)
.boolean()
.default(false)
.not_null(),
)
.to_owned(),
)
.await?;
manager
.create_index(
Index::create()
.name("IDX_username")
.table(User::Table)
.col(User::Name)
.to_owned(),
)
.await?;
manager
.create_table(
Table::create()
.table(UserPassword::Table)
.col(
ColumnDef::new(User::Id)
.integer()
.primary_key()
.auto_increment()
.not_null(),
)
.col(
ColumnDef::new(UserPassword::CreatedAt)
.date_time()
.not_null(),
)
.col(ColumnDef::new(UserPassword::UserId).integer().not_null())
.col(ColumnDef::new(UserPassword::Password).string().not_null())
.foreign_key(
ForeignKey::create()
.name("FK_user_password")
.to(User::Table, User::Id)
.from(UserPassword::Table, UserPassword::UserId)
.on_delete(ForeignKeyAction::Cascade)
.on_update(ForeignKeyAction::Cascade),
)
.to_owned(),
)
.await?;
Ok(())
}
async fn down(&self, manager: &sea_orm_migration::SchemaManager) -> Result<(), sea_orm::DbErr> {
manager
.drop_table(Table::drop().table(UserPassword::Table).to_owned())
.await?;
manager
.drop_table(Table::drop().table(User::Table).to_owned())
.await?;
Ok(())
}
}
#[derive(Iden)]
pub enum User {
Table,
CreatedAt,
UpdatedAt,
DeletedAt,
Id,
Name,
Enabled,
}
#[derive(Iden)]
pub enum UserPassword {
Table,
CreatedAt,
Id,
UserId,
Password,
}

View File

@ -2,13 +2,15 @@ use sea_orm_migration::prelude::*;
pub struct Migrator; pub struct Migrator;
mod m20220804_000001_create_user_table; mod m20220804_000001_create_audit_table;
mod m20220804_000002_create_user_table;
#[async_trait::async_trait] #[async_trait::async_trait]
impl MigratorTrait for Migrator { impl MigratorTrait for Migrator {
fn migrations() -> Vec<Box<dyn MigrationTrait>> { fn migrations() -> Vec<Box<dyn MigrationTrait>> {
vec![ vec![
Box::new(m20220804_000001_create_user_table::Migration), Box::new(m20220804_000001_create_audit_table::Migration),
Box::new(m20220804_000002_create_user_table::Migration),
] ]
} }
} }

26
src/web/model/audit.rs Normal file
View File

@ -0,0 +1,26 @@
//! SeaORM Entity. Generated by sea-orm-codegen 0.9.1
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Serialize, Deserialize)]
#[sea_orm(table_name = "audit")]
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
pub created_at: String,
pub source: String,
pub r#type: String,
pub description: String,
}
#[derive(Copy, Clone, Debug, EnumIter)]
pub enum Relation {}
impl RelationTrait for Relation {
fn def(&self) -> RelationDef {
panic!("No RelationDef")
}
}
impl ActiveModelBehavior for ActiveModel {}

View File

@ -2,4 +2,6 @@
pub mod prelude; pub mod prelude;
pub mod audit;
pub mod user; pub mod user;
pub mod user_password;

View File

@ -1,3 +1,5 @@
//! SeaORM Entity. Generated by sea-orm-codegen 0.9.1 //! SeaORM Entity. Generated by sea-orm-codegen 0.9.1
pub use super::audit::Entity as Audit;
pub use super::user::Entity as User; pub use super::user::Entity as User;
pub use super::user_password::Entity as UserPassword;

View File

@ -8,16 +8,22 @@ use serde::{Deserialize, Serialize};
pub struct Model { pub struct Model {
#[sea_orm(primary_key)] #[sea_orm(primary_key)]
pub id: i32, pub id: i32,
pub created_at: String,
pub updated_at: String,
pub deleted_at: String,
pub name: String, pub name: String,
pub password: String, pub enabled: i32,
} }
#[derive(Copy, Clone, Debug, EnumIter)] #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {} pub enum Relation {
#[sea_orm(has_many = "super::user_password::Entity")]
UserPassword,
}
impl RelationTrait for Relation { impl Related<super::user_password::Entity> for Entity {
fn def(&self) -> RelationDef { fn to() -> RelationDef {
panic!("No RelationDef") Relation::UserPassword.def()
} }
} }

View File

@ -0,0 +1,34 @@
//! SeaORM Entity. Generated by sea-orm-codegen 0.9.1
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Serialize, Deserialize)]
#[sea_orm(table_name = "user_password")]
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
pub created_at: String,
pub user_id: i32,
pub password: String,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(
belongs_to = "super::user::Entity",
from = "Column::UserId",
to = "super::user::Column::Id",
on_update = "Cascade",
on_delete = "Cascade"
)]
User,
}
impl Related<super::user::Entity> for Entity {
fn to() -> RelationDef {
Relation::User.def()
}
}
impl ActiveModelBehavior for ActiveModel {}