diff --git a/entities/src/admin_trail.rs b/entities/src/admin_trail.rs new file mode 100644 index 00000000..26ad1e3b --- /dev/null +++ b/entities/src/admin_trail.rs @@ -0,0 +1,37 @@ +//! `SeaORM` Entity. Generated by sea-orm-codegen 0.10.6 + +use sea_orm::entity::prelude::*; + +#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)] +#[sea_orm(table_name = "admin_trail")] +pub struct Model { + #[sea_orm(primary_key)] + pub id: i32, + pub caller: u64, + pub imitating_user: Option, + pub endpoint: String, + pub payload: String, + pub timestamp: DateTimeUtc, +} + +#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] +pub enum Relation { + #[sea_orm( + belongs_to = "super::user::Entity", + from = "Column::Caller", + to = "super::user::Column::Id", + on_update = "NoAction", + on_delete = "NoAction" + )] + User2, + #[sea_orm( + belongs_to = "super::user::Entity", + from = "Column::ImitatingUser", + to = "super::user::Column::Id", + on_update = "NoAction", + on_delete = "NoAction" + )] + User1, +} + +impl ActiveModelBehavior for ActiveModel {} diff --git a/entities/src/mod.rs b/entities/src/mod.rs index 545dc4db..627d1d6f 100644 --- a/entities/src/mod.rs +++ b/entities/src/mod.rs @@ -3,6 +3,7 @@ pub mod prelude; pub mod admin; +pub mod admin_trail; pub mod login; pub mod pending_login; pub mod referral; diff --git a/entities/src/prelude.rs b/entities/src/prelude.rs index eba83250..ad1ac9a9 100644 --- a/entities/src/prelude.rs +++ b/entities/src/prelude.rs @@ -1,5 +1,7 @@ //! `SeaORM` Entity. Generated by sea-orm-codegen 0.10.5 +pub use super::admin::Entity as Admin; +pub use super::admin_trail::Entity as AdminTrail; pub use super::login::Entity as Login; pub use super::pending_login::Entity as PendingLogin; pub use super::revert_log::Entity as RevertLog; diff --git a/migration/src/m20230215_152254_admin_trail.rs b/migration/src/m20230215_152254_admin_trail.rs new file mode 100644 index 00000000..994361ee --- /dev/null +++ b/migration/src/m20230215_152254_admin_trail.rs @@ -0,0 +1,93 @@ +use sea_orm_migration::prelude::*; + +#[derive(DeriveMigrationName)] +pub struct Migration; + +#[async_trait::async_trait] +impl MigrationTrait for Migration { + async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .create_table( + Table::create() + .table(AdminTrail::Table) + .if_not_exists() + .col( + ColumnDef::new(AdminTrail::Id) + .integer() + .not_null() + .auto_increment() + .primary_key(), + ) + .col( + ColumnDef::new(AdminTrail::Caller) + .big_unsigned() + .not_null() + // TODO: Add Foreign Key + ) + .foreign_key( + sea_query::ForeignKey::create() + .from(AdminTrail::Table, AdminTrail::Caller) + .to(User::Table, User::Id), + ) + .col( + ColumnDef::new(AdminTrail::ImitatingUser) + .big_unsigned() + // Can be null bcs maybe we're just logging in / using endpoints that don't imitate a user + // TODO: Add Foreign Key + ) + .foreign_key( + sea_query::ForeignKey::create() + .from(AdminTrail::Table, AdminTrail::ImitatingUser) + .to(User::Table, User::Id), + ) + .col( + ColumnDef::new(AdminTrail::Endpoint) + .string() + .not_null() + ) + .col( + ColumnDef::new(AdminTrail::Payload) + .string() + .not_null() + ) + .col( + ColumnDef::new(AdminTrail::Timestamp) + .timestamp() + .not_null() + .extra("DEFAULT CURRENT_TIMESTAMP".to_string()) + ) + .to_owned(), + ) + .await + } + + async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { + // Replace the sample below with your own migration scripts + manager + .drop_table(Table::drop().table(AdminTrail::Table).to_owned()) + .await + } +} + +/// Learn more at https://docs.rs/sea-query#iden +#[derive(Iden)] +enum AdminTrail { + Table, + Id, + Caller, + ImitatingUser, + Endpoint, + Payload, + Timestamp +} + + +/// Learn more at https://docs.rs/sea-query#iden +#[derive(Iden)] +enum User { + Table, + Id, + Address, + Description, + Email, +} \ No newline at end of file