diff --git a/entity/src/candidate.rs b/entity/src/candidate.rs index d9199ce..4fb441e 100644 --- a/entity/src/candidate.rs +++ b/entity/src/candidate.rs @@ -37,6 +37,15 @@ pub struct Model { } #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] -pub enum Relation {} +pub enum Relation { + #[sea_orm(has_many = "super::session::Entity")] + Session, +} + +impl Related for Entity { + fn to() -> RelationDef { + Relation::Session.def() + } +} impl ActiveModelBehavior for ActiveModel {} diff --git a/entity/src/session.rs b/entity/src/session.rs index 2df872b..4f0be16 100644 --- a/entity/src/session.rs +++ b/entity/src/session.rs @@ -14,6 +14,21 @@ pub struct Model { } #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] -pub enum Relation {} +pub enum Relation { + #[sea_orm( + belongs_to = "super::candidate::Entity", + from = "Column::UserId", + to = "super::candidate::Column::Application", + on_update = "Cascade", + on_delete = "Cascade" + )] + Candidate, +} + +impl Related for Entity { + fn to() -> RelationDef { + Relation::Candidate.def() + } +} impl ActiveModelBehavior for ActiveModel {} diff --git a/migration/src/lib.rs b/migration/src/lib.rs index adbef73..a8f21b1 100644 --- a/migration/src/lib.rs +++ b/migration/src/lib.rs @@ -5,6 +5,7 @@ mod m20221024_121621_create_candidate; mod m20221024_124701_create_parent; mod m20221024_134454_fill_admin; mod m20221025_154422_create_session; +mod m20221027_194728_session_create_user_fk; pub struct Migrator; @@ -17,6 +18,7 @@ impl MigratorTrait for Migrator { Box::new(m20221024_124701_create_parent::Migration), Box::new(m20221024_134454_fill_admin::Migration::default()), Box::new(m20221025_154422_create_session::Migration), + Box::new(m20221027_194728_session_create_user_fk::Migration), ] } } diff --git a/migration/src/m20221024_121621_create_candidate.rs b/migration/src/m20221024_121621_create_candidate.rs index 974a93f..d11282d 100644 --- a/migration/src/m20221024_121621_create_candidate.rs +++ b/migration/src/m20221024_121621_create_candidate.rs @@ -50,7 +50,7 @@ impl MigrationTrait for Migration { /// Learn more at https://docs.rs/sea-query#iden #[derive(Iden)] -enum Candidate { +pub enum Candidate { Table, Application, Code, diff --git a/migration/src/m20221025_154422_create_session.rs b/migration/src/m20221025_154422_create_session.rs index d7931f0..9327626 100644 --- a/migration/src/m20221025_154422_create_session.rs +++ b/migration/src/m20221025_154422_create_session.rs @@ -1,11 +1,29 @@ use sea_orm_migration::prelude::*; +use crate::m20221024_121621_create_candidate::Candidate; + #[derive(DeriveMigrationName)] pub struct Migration; #[async_trait::async_trait] impl MigrationTrait for Migration { async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { + let table = Table::create() + .table(Session::Table) + .if_not_exists() + .col( + ColumnDef::new(Session::Id) + .uuid() + .not_null() + .unique_key() + .primary_key(), + ) + .col(ColumnDef::new(Session::HashedToken).string().not_null()) + .col(ColumnDef::new(Session::UserId).integer().not_null()) + .col(ColumnDef::new(Session::CreatedAt).date_time().not_null()) + .col(ColumnDef::new(Session::UpdatedAt).date_time().not_null()) + .to_owned(); + manager .create_table( Table::create() @@ -36,7 +54,7 @@ impl MigrationTrait for Migration { /// Learn more at https://docs.rs/sea-query#iden #[derive(Iden)] -enum Session { +pub enum Session { Table, Id, HashedToken, diff --git a/migration/src/m20221027_194728_session_create_user_fk.rs b/migration/src/m20221027_194728_session_create_user_fk.rs new file mode 100644 index 0000000..248fe2d --- /dev/null +++ b/migration/src/m20221027_194728_session_create_user_fk.rs @@ -0,0 +1,26 @@ +use sea_orm_migration::prelude::*; + +use crate::{m20221025_154422_create_session::Session, m20221024_121621_create_candidate::Candidate}; + +#[derive(DeriveMigrationName)] +pub struct Migration; + +#[async_trait::async_trait] +impl MigrationTrait for Migration { + async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager.create_foreign_key(ForeignKey::create() + .name("user_fk") + .from(Session::Table, Session::UserId) + .to(Candidate::Table, Candidate::Application) + .on_delete(ForeignKeyAction::Cascade) + .on_update(ForeignKeyAction::Cascade) + .to_owned()).await + } + + async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager.drop_foreign_key(ForeignKey::drop() + .name("user_fk") + .table(Session::Table) + .to_owned()).await + } +} \ No newline at end of file