//! The lunch domain: areas, restaurants, per-person rankings, and the lunch
//! history the suggester uses to avoid repeats.
//!
//! An *area* is a region restaurants are grouped under — the home office, or a
//! city someone is travelling to. Suggestions are always made within one area.
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(Areas::Table)
.if_not_exists()
.col(ColumnDef::new(Areas::Id).uuid().not_null().primary_key())
.col(ColumnDef::new(Areas::Name).string_len(100).unique_key().not_null())
.col(ColumnDef::new(Areas::Lat).double().not_null())
.col(ColumnDef::new(Areas::Lon).double().not_null())
.col(ColumnDef::new(Areas::Zoom).integer().not_null().default(15))
.col(ColumnDef::new(Areas::CreatedBy).uuid().null())
.col(
ColumnDef::new(Areas::CreatedAt)
.timestamp_with_time_zone()
.not_null()
.default(Expr::current_timestamp()),
)
.foreign_key(
ForeignKey::create()
.name("fk_areas_created_by")
.from(Areas::Table, Areas::CreatedBy)
.to(Users::Table, Users::Id)
.on_delete(ForeignKeyAction::SetNull),
)
.to_owned(),
)
.await?;
manager
.create_table(
Table::create()
.table(Restaurants::Table)
.if_not_exists()
.col(ColumnDef::new(Restaurants::Id).uuid().not_null().primary_key())
.col(ColumnDef::new(Restaurants::AreaId).uuid().not_null())
.col(ColumnDef::new(Restaurants::Name).string_len(255).not_null())
// N123 / W123 / R123, as the OSM API spells it. Null for a
// hand-dropped pin that isn't in OSM.
.col(ColumnDef::new(Restaurants::OsmRef).string_len(32).null())
.col(ColumnDef::new(Restaurants::Lat).double().not_null())
.col(ColumnDef::new(Restaurants::Lon).double().not_null())
.col(ColumnDef::new(Restaurants::Cuisine).string_len(255).null())
.col(ColumnDef::new(Restaurants::Address).string_len(255).null())
.col(ColumnDef::new(Restaurants::Website).string_len(512).null())
.col(ColumnDef::new(Restaurants::Notes).text().not_null().default(""))
.col(ColumnDef::new(Restaurants::Archived).boolean().not_null().default(false))
.col(ColumnDef::new(Restaurants::AddedBy).uuid().null())
.col(
ColumnDef::new(Restaurants::CreatedAt)
.timestamp_with_time_zone()
.not_null()
.default(Expr::current_timestamp()),
)
.foreign_key(
ForeignKey::create()
.name("fk_restaurants_area_id")
.from(Restaurants::Table, Restaurants::AreaId)
.to(Areas::Table, Areas::Id)
.on_delete(ForeignKeyAction::Cascade),
)
.foreign_key(
ForeignKey::create()
.name("fk_restaurants_added_by")
.from(Restaurants::Table, Restaurants::AddedBy)
.to(Users::Table, Users::Id)
.on_delete(ForeignKeyAction::SetNull),
)
.to_owned(),
)
.await?;
manager
.create_index(
Index::create()
.name("idx_restaurants_area_id")
.table(Restaurants::Table)
.col(Restaurants::AreaId)
.to_owned(),
)
.await?;
// One OSM element is one restaurant; adding it twice is a duplicate.
// Postgres treats NULLs as distinct, so custom pins are unaffected.
manager
.create_index(
Index::create()
.name("idx_restaurants_osm_ref")
.table(Restaurants::Table)
.col(Restaurants::OsmRef)
.unique()
.to_owned(),
)
.await?;
manager
.create_table(
Table::create()
.table(Rankings::Table)
.if_not_exists()
.col(ColumnDef::new(Rankings::UserId).uuid().not_null())
.col(ColumnDef::new(Rankings::RestaurantId).uuid().not_null())
// always | positive | neutral | never
.col(ColumnDef::new(Rankings::Rank).string_len(16).not_null())
.col(
ColumnDef::new(Rankings::UpdatedAt)
.timestamp_with_time_zone()
.not_null()
.default(Expr::current_timestamp()),
)
.primary_key(Index::create().col(Rankings::UserId).col(Rankings::RestaurantId))
.foreign_key(
ForeignKey::create()
.name("fk_rankings_user_id")
.from(Rankings::Table, Rankings::UserId)
.to(Users::Table, Users::Id)
.on_delete(ForeignKeyAction::Cascade),
)
.foreign_key(
ForeignKey::create()
.name("fk_rankings_restaurant_id")
.from(Rankings::Table, Rankings::RestaurantId)
.to(Restaurants::Table, Restaurants::Id)
.on_delete(ForeignKeyAction::Cascade),
)
.to_owned(),
)
.await?;
manager
.create_table(
Table::create()
.table(Lunches::Table)
.if_not_exists()
.col(ColumnDef::new(Lunches::Id).uuid().not_null().primary_key())
.col(ColumnDef::new(Lunches::RestaurantId).uuid().not_null())
.col(ColumnDef::new(Lunches::WentOn).date().not_null())
.col(ColumnDef::new(Lunches::LoggedBy).uuid().null())
.col(
ColumnDef::new(Lunches::CreatedAt)
.timestamp_with_time_zone()
.not_null()
.default(Expr::current_timestamp()),
)
.foreign_key(
ForeignKey::create()
.name("fk_lunches_restaurant_id")
.from(Lunches::Table, Lunches::RestaurantId)
.to(Restaurants::Table, Restaurants::Id)
.on_delete(ForeignKeyAction::Cascade),
)
.foreign_key(
ForeignKey::create()
.name("fk_lunches_logged_by")
.from(Lunches::Table, Lunches::LoggedBy)
.to(Users::Table, Users::Id)
.on_delete(ForeignKeyAction::SetNull),
)
.to_owned(),
)
.await?;
manager
.create_index(
Index::create()
.name("idx_lunches_restaurant_went_on")
.table(Lunches::Table)
.col(Lunches::RestaurantId)
.col(Lunches::WentOn)
.to_owned(),
)
.await?;
manager
.create_table(
Table::create()
.table(LunchAttendees::Table)
.if_not_exists()
.col(ColumnDef::new(LunchAttendees::LunchId).uuid().not_null())
.col(ColumnDef::new(LunchAttendees::UserId).uuid().not_null())
.primary_key(
Index::create().col(LunchAttendees::LunchId).col(LunchAttendees::UserId),
)
.foreign_key(
ForeignKey::create()
.name("fk_lunch_attendees_lunch_id")
.from(LunchAttendees::Table, LunchAttendees::LunchId)
.to(Lunches::Table, Lunches::Id)
.on_delete(ForeignKeyAction::Cascade),
)
.foreign_key(
ForeignKey::create()
.name("fk_lunch_attendees_user_id")
.from(LunchAttendees::Table, LunchAttendees::UserId)
.to(Users::Table, Users::Id)
.on_delete(ForeignKeyAction::Cascade),
)
.to_owned(),
)
.await?;
Ok(())
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
for table in [
LunchAttendees::Table.into_iden(),
Lunches::Table.into_iden(),
Rankings::Table.into_iden(),
Restaurants::Table.into_iden(),
Areas::Table.into_iden(),
] {
manager.drop_table(Table::drop().table(table).to_owned()).await?;
}
Ok(())
}
}
#[derive(DeriveIden)]
enum Users {
Table,
Id,
}
#[derive(DeriveIden)]
enum Areas {
Table,
Id,
Name,
Lat,
Lon,
Zoom,
CreatedBy,
CreatedAt,
}
#[derive(DeriveIden)]
enum Restaurants {
Table,
Id,
AreaId,
Name,
OsmRef,
Lat,
Lon,
Cuisine,
Address,
Website,
Notes,
Archived,
AddedBy,
CreatedAt,
}
#[derive(DeriveIden)]
enum Rankings {
Table,
UserId,
RestaurantId,
Rank,
UpdatedAt,
}
#[derive(DeriveIden)]
enum Lunches {
Table,
Id,
RestaurantId,
WentOn,
LoggedBy,
CreatedAt,
}
#[derive(DeriveIden)]
enum LunchAttendees {
Table,
LunchId,
UserId,
}
neiam
/fieri-forever
the flavortown lunch engine
public · Issues · Pulls · Labels · Forks · Compare · Actions success · Badges · Packages
⭐
Log in to mark this repository.
10.7 KiB · text
History
7a00c02