diff --git a/migration/src/lib.rs b/migration/src/lib.rs index c1fe52c..502d1b1 100644 --- a/migration/src/lib.rs +++ b/migration/src/lib.rs @@ -1,12 +1,16 @@ pub use sea_orm_migration::prelude::*; mod m20220101_000001_account; +mod m20230520_203638_household; pub struct Migrator; #[async_trait::async_trait] impl MigratorTrait for Migrator { fn migrations() -> Vec> { - vec![Box::new(m20220101_000001_account::Migration)] + vec![ + Box::new(m20220101_000001_account::Migration), + Box::new(m20230520_203638_household::Migration), + ] } } diff --git a/migration/src/m20220101_000001_account.rs b/migration/src/m20220101_000001_account.rs index a804786..d5c67cf 100644 --- a/migration/src/m20220101_000001_account.rs +++ b/migration/src/m20220101_000001_account.rs @@ -4,7 +4,7 @@ use sea_orm_migration::prelude::*; pub struct Migration; #[derive(Iden)] -enum User { +pub(crate) enum User { Table, Id, Name, diff --git a/migration/src/m20230520_203638_household.rs b/migration/src/m20230520_203638_household.rs new file mode 100644 index 0000000..83e4a88 --- /dev/null +++ b/migration/src/m20230520_203638_household.rs @@ -0,0 +1,91 @@ +use sea_orm_migration::prelude::*; + +use super::m20220101_000001_account::User; + +#[derive(DeriveMigrationName)] +pub struct Migration; + +#[derive(Iden)] +enum Household { + Table, + Name, + Id, +} + +#[derive(Iden)] +enum HouseholdMembers { + Table, + Household, + User, +} + +#[async_trait::async_trait] +impl MigrationTrait for Migration { + async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .create_table( + Table::create() + .table(Household::Table) + .if_not_exists() + .col( + ColumnDef::new(Household::Id) + .uuid() + .not_null() + .primary_key(), + ) + .col(ColumnDef::new(Household::Name).string().not_null()) + .to_owned(), + ) + .await?; + + manager + .create_table( + Table::create() + .table(HouseholdMembers::Table) + .if_not_exists() + .col( + ColumnDef::new(HouseholdMembers::Household) + .uuid() + .not_null(), + ) + .col(ColumnDef::new(HouseholdMembers::User).uuid().not_null()) + .primary_key( + Index::create() + .col(HouseholdMembers::Household) + .col(HouseholdMembers::User), + ) + .foreign_key( + ForeignKey::create() + .name("FK_household_members_house") + .from(HouseholdMembers::Table, HouseholdMembers::Household) + .to(Household::Table, Household::Id) + .on_delete(ForeignKeyAction::Restrict) + .on_update(ForeignKeyAction::Restrict), + ) + .foreign_key( + ForeignKey::create() + .name("FK_household_members_user") + .from(HouseholdMembers::Table, HouseholdMembers::User) + .to(User::Table, User::Id) + .on_delete(ForeignKeyAction::Restrict) + .on_update(ForeignKeyAction::Restrict), + ) + .to_owned(), + ) + .await?; + + Ok(()) + } + + async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .drop_table(Table::drop().table(HouseholdMembers::Table).to_owned()) + .await?; + + manager + .drop_table(Table::drop().table(Household::Table).to_owned()) + .await?; + + Ok(()) + } +} diff --git a/src/entity/household.rs b/src/entity/household.rs new file mode 100644 index 0000000..4e5864b --- /dev/null +++ b/src/entity/household.rs @@ -0,0 +1,25 @@ +//! `SeaORM` Entity. Generated by sea-orm-codegen 0.11.3 + +use sea_orm::entity::prelude::*; + +#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)] +#[sea_orm(table_name = "household")] +pub struct Model { + #[sea_orm(primary_key, auto_increment = false)] + pub id: Uuid, + pub name: String, +} + +#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] +pub enum Relation {} + +impl Related for Entity { + fn to() -> RelationDef { + super::household_members::Relation::User.def() + } + fn via() -> Option { + Some(super::household_members::Relation::Household.def().rev()) + } +} + +impl ActiveModelBehavior for ActiveModel {} diff --git a/src/entity/household_members.rs b/src/entity/household_members.rs new file mode 100644 index 0000000..bf7cf95 --- /dev/null +++ b/src/entity/household_members.rs @@ -0,0 +1,46 @@ +//! `SeaORM` Entity. Generated by sea-orm-codegen 0.11.3 + +use sea_orm::entity::prelude::*; + +#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)] +#[sea_orm(table_name = "household_members")] +pub struct Model { + #[sea_orm(primary_key, auto_increment = false)] + pub household: Uuid, + #[sea_orm(primary_key, auto_increment = false)] + pub user: Uuid, +} + +#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] +pub enum Relation { + #[sea_orm( + belongs_to = "super::household::Entity", + from = "Column::Household", + to = "super::household::Column::Id", + on_update = "Restrict", + on_delete = "Restrict" + )] + Household, + #[sea_orm( + belongs_to = "super::user::Entity", + from = "Column::User", + to = "super::user::Column::Id", + on_update = "Restrict", + on_delete = "Restrict" + )] + User, +} + +impl Related for Entity { + fn to() -> RelationDef { + Relation::Household.def() + } +} + +impl Related for Entity { + fn to() -> RelationDef { + Relation::User.def() + } +} + +impl ActiveModelBehavior for ActiveModel {} diff --git a/src/entity/mod.rs b/src/entity/mod.rs index fb510ff..dc2707e 100644 --- a/src/entity/mod.rs +++ b/src/entity/mod.rs @@ -2,4 +2,6 @@ pub mod prelude; +pub mod household; +pub mod household_members; pub mod user; diff --git a/src/entity/prelude.rs b/src/entity/prelude.rs index 7c8fdd7..9cbce14 100644 --- a/src/entity/prelude.rs +++ b/src/entity/prelude.rs @@ -1,3 +1,5 @@ //! `SeaORM` Entity. Generated by sea-orm-codegen 0.11.3 +pub use super::household::Entity as Household; +pub use super::household_members::Entity as HouseholdMembers; pub use super::user::Entity as User; diff --git a/src/entity/user.rs b/src/entity/user.rs index 687cfbd..c3f901e 100644 --- a/src/entity/user.rs +++ b/src/entity/user.rs @@ -16,4 +16,13 @@ pub struct Model { #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] pub enum Relation {} +impl Related for Entity { + fn to() -> RelationDef { + super::household_members::Relation::Household.def() + } + fn via() -> Option { + Some(super::household_members::Relation::User.def().rev()) + } +} + impl ActiveModelBehavior for ActiveModel {}