server: Add an entity to represent households

This commit is contained in:
traxys 2023-05-28 19:50:55 +02:00
parent e005d8b129
commit f50d7c1076
8 changed files with 181 additions and 2 deletions

View file

@ -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<Box<dyn MigrationTrait>> {
vec![Box::new(m20220101_000001_account::Migration)]
vec![
Box::new(m20220101_000001_account::Migration),
Box::new(m20230520_203638_household::Migration),
]
}
}

View file

@ -4,7 +4,7 @@ use sea_orm_migration::prelude::*;
pub struct Migration;
#[derive(Iden)]
enum User {
pub(crate) enum User {
Table,
Id,
Name,

View file

@ -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(())
}
}