Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified .DS_Store
Binary file not shown.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,10 @@ target
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/

# SQLite database
.env
*.db
*.db-shm
*.db-wal
.DS_Store
4 changes: 2 additions & 2 deletions migration/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
pub use sea_orm_migration::prelude::*;

mod m20260515_195629_create_users_table;
mod m20260529_144919_create_settlemate_tables;

pub struct Migrator;

#[async_trait::async_trait]
impl MigratorTrait for Migrator {
fn migrations() -> Vec<Box<dyn MigrationTrait>> {
vec![
Box::new(m20260515_195629_create_users_table::Migration),
Box::new(m20260529_144919_create_settlemate_tables::Migration),
]
}
}
50 changes: 0 additions & 50 deletions migration/src/m20260515_195629_create_users_table.rs

This file was deleted.

228 changes: 228 additions & 0 deletions migration/src/m20260529_144919_create_settlemate_tables.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
use sea_orm_migration::{prelude::*, schema::*};

#[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(Users::Table)
.if_not_exists()
.col(pk_auto(Users::Id))
.col(string(Users::Name).not_null())
.col(string(Users::Email).not_null().unique_key())
.col(string(Users::PasswordHash).not_null())
.col(
timestamp(Users::CreatedAt)
.not_null()
.default(Expr::current_timestamp()),
)
.to_owned(),
)
.await?;

manager
.create_table(
Table::create()
.table(Groups::Table)
.if_not_exists()
.col(pk_auto(Groups::Id))
.col(string(Groups::Name).not_null())
.col(
timestamp(Groups::CreatedAt)
.not_null()
.default(Expr::current_timestamp()),
)
.to_owned(),
)
.await?;

manager
.create_table(
Table::create()
.table(GroupMembers::Table)
.if_not_exists()
.col(pk_auto(GroupMembers::Id))
.col(integer(GroupMembers::GroupId).not_null())
.col(integer(GroupMembers::UserId).not_null())
.foreign_key(
ForeignKey::create()
.from(GroupMembers::Table, GroupMembers::GroupId)
.to(Groups::Table, Groups::Id)
.on_delete(ForeignKeyAction::Cascade),
)
.foreign_key(
ForeignKey::create()
.from(GroupMembers::Table, GroupMembers::UserId)
.to(Users::Table, Users::Id)
.on_delete(ForeignKeyAction::Cascade),
)
.to_owned(),
)
.await?;

manager
.create_table(
Table::create()
.table(Expenses::Table)
.if_not_exists()
.col(pk_auto(Expenses::Id))
.col(string(Expenses::Description).not_null())
.col(double(Expenses::Amount).not_null())
.col(integer(Expenses::PaidBy).not_null())
.col(integer(Expenses::GroupId))
.col(string(Expenses::SplitType).not_null())
.col(
timestamp(Expenses::CreatedAt)
.not_null()
.default(Expr::current_timestamp()),
)
.foreign_key(
ForeignKey::create()
.from(Expenses::Table, Expenses::PaidBy)
.to(Users::Table, Users::Id),
)
.foreign_key(
ForeignKey::create()
.from(Expenses::Table, Expenses::GroupId)
.to(Groups::Table, Groups::Id)
.on_delete(ForeignKeyAction::SetNull),
)
.to_owned(),
)
.await?;

manager
.create_table(
Table::create()
.table(ExpenseSplits::Table)
.if_not_exists()
.col(pk_auto(ExpenseSplits::Id))
.col(integer(ExpenseSplits::ExpenseId).not_null())
.col(integer(ExpenseSplits::UserId).not_null())
.col(double(ExpenseSplits::Amount))
.foreign_key(
ForeignKey::create()
.from(ExpenseSplits::Table, ExpenseSplits::ExpenseId)
.to(Expenses::Table, Expenses::Id)
.on_delete(ForeignKeyAction::Cascade),
)
.foreign_key(
ForeignKey::create()
.from(ExpenseSplits::Table, ExpenseSplits::UserId)
.to(Users::Table, Users::Id),
)
.to_owned(),
)
.await?;

manager
.create_table(
Table::create()
.table(Payments::Table)
.if_not_exists()
.col(pk_auto(Payments::Id))
.col(integer(Payments::FromId).not_null())
.col(integer(Payments::ToId).not_null())
.col(double(Payments::Amount).not_null())
.col(integer(Payments::GroupId))
.col(
timestamp(Payments::CreatedAt)
.not_null()
.default(Expr::current_timestamp()),
)
.foreign_key(
ForeignKey::create()
.from(Payments::Table, Payments::FromId)
.to(Users::Table, Users::Id),
)
.foreign_key(
ForeignKey::create()
.from(Payments::Table, Payments::ToId)
.to(Users::Table, Users::Id),
)
.foreign_key(
ForeignKey::create()
.from(Payments::Table, Payments::GroupId)
.to(Groups::Table, Groups::Id)
.on_delete(ForeignKeyAction::SetNull),
)
.to_owned(),
)
.await?;

Ok(())
}

async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager.drop_table(Table::drop().table(Payments::Table).to_owned()).await?;
manager.drop_table(Table::drop().table(ExpenseSplits::Table).to_owned()).await?;
manager.drop_table(Table::drop().table(Expenses::Table).to_owned()).await?;
manager.drop_table(Table::drop().table(GroupMembers::Table).to_owned()).await?;
manager.drop_table(Table::drop().table(Groups::Table).to_owned()).await?;
manager.drop_table(Table::drop().table(Users::Table).to_owned()).await?;

Ok(())
}
}

#[derive(DeriveIden)]
enum Users {
Table,
Id,
Name,
Email,
PasswordHash,
CreatedAt,
}

#[derive(DeriveIden)]
enum Groups {
Table,
Id,
Name,
CreatedAt,
}

#[derive(DeriveIden)]
enum GroupMembers {
Table,
Id,
GroupId,
UserId,
}

#[derive(DeriveIden)]
enum Expenses {
Table,
Id,
Description,
Amount,
PaidBy,
GroupId,
SplitType,
CreatedAt,
}

#[derive(DeriveIden)]
enum ExpenseSplits {
Table,
Id,
ExpenseId,
UserId,
Amount,
}

#[derive(DeriveIden)]
enum Payments {
Table,
Id,
FromId,
ToId,
Amount,
GroupId,
CreatedAt,
}
Binary file removed settlemate.db
Binary file not shown.
49 changes: 49 additions & 0 deletions src/entities/expense_splits.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.20

use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Serialize, Deserialize)]
#[sea_orm(table_name = "expense_splits")]
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
pub expense_id: i32,
pub user_id: i32,
#[sea_orm(column_type = "Double")]
pub amount: f64,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(
belongs_to = "super::expenses::Entity",
from = "Column::ExpenseId",
to = "super::expenses::Column::Id",
on_update = "NoAction",
on_delete = "Cascade"
)]
Expenses,
#[sea_orm(
belongs_to = "super::users::Entity",
from = "Column::UserId",
to = "super::users::Column::Id",
on_update = "NoAction",
on_delete = "NoAction"
)]
Users,
}

impl Related<super::expenses::Entity> for Entity {
fn to() -> RelationDef {
Relation::Expenses.def()
}
}

impl Related<super::users::Entity> for Entity {
fn to() -> RelationDef {
Relation::Users.def()
}
}

impl ActiveModelBehavior for ActiveModel {}
Loading
Loading