diff --git a/src/migrations/m20220804_000001_create_audit_table.rs b/src/migrations/m20220804_000001_create_audit_table.rs new file mode 100644 index 0000000..494582e --- /dev/null +++ b/src/migrations/m20220804_000001_create_audit_table.rs @@ -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, +} diff --git a/src/migrations/m20220804_000001_create_user_table.rs b/src/migrations/m20220804_000001_create_user_table.rs deleted file mode 100644 index 7a92e10..0000000 --- a/src/migrations/m20220804_000001_create_user_table.rs +++ /dev/null @@ -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, -} \ No newline at end of file diff --git a/src/migrations/m20220804_000002_create_user_table.rs b/src/migrations/m20220804_000002_create_user_table.rs new file mode 100644 index 0000000..0f6ccce --- /dev/null +++ b/src/migrations/m20220804_000002_create_user_table.rs @@ -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, +} diff --git a/src/migrations/mod.rs b/src/migrations/mod.rs index 145574b..a573474 100644 --- a/src/migrations/mod.rs +++ b/src/migrations/mod.rs @@ -2,13 +2,15 @@ use sea_orm_migration::prelude::*; 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] impl MigratorTrait for Migrator { fn migrations() -> 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), ] } } diff --git a/src/web/model/audit.rs b/src/web/model/audit.rs new file mode 100644 index 0000000..9a98a2a --- /dev/null +++ b/src/web/model/audit.rs @@ -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 {} diff --git a/src/web/model/mod.rs b/src/web/model/mod.rs index 4d40e82..5574801 100644 --- a/src/web/model/mod.rs +++ b/src/web/model/mod.rs @@ -2,4 +2,6 @@ pub mod prelude; +pub mod audit; pub mod user; +pub mod user_password; diff --git a/src/web/model/prelude.rs b/src/web/model/prelude.rs index 9fe89ba..8318c36 100644 --- a/src/web/model/prelude.rs +++ b/src/web/model/prelude.rs @@ -1,3 +1,5 @@ //! 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_password::Entity as UserPassword; diff --git a/src/web/model/user.rs b/src/web/model/user.rs index 3a98b4f..4438a30 100644 --- a/src/web/model/user.rs +++ b/src/web/model/user.rs @@ -8,16 +8,22 @@ use serde::{Deserialize, Serialize}; pub struct Model { #[sea_orm(primary_key)] pub id: i32, + pub created_at: String, + pub updated_at: String, + pub deleted_at: String, pub name: String, - pub password: String, + pub enabled: i32, } -#[derive(Copy, Clone, Debug, EnumIter)] -pub enum Relation {} +#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] +pub enum Relation { + #[sea_orm(has_many = "super::user_password::Entity")] + UserPassword, +} -impl RelationTrait for Relation { - fn def(&self) -> RelationDef { - panic!("No RelationDef") +impl Related for Entity { + fn to() -> RelationDef { + Relation::UserPassword.def() } } diff --git a/src/web/model/user_password.rs b/src/web/model/user_password.rs new file mode 100644 index 0000000..855a617 --- /dev/null +++ b/src/web/model/user_password.rs @@ -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 for Entity { + fn to() -> RelationDef { + Relation::User.def() + } +} + +impl ActiveModelBehavior for ActiveModel {}