8.7 KiB · text History 7def6a5
//! Outings: a lunch being decided. Someone starts one for an area, it draws a
//! handful of options, people join and vote (approval-style: any option
//! they'd be happy with), re-roll swaps the options nobody voted for, and
//! deciding logs a `lunches` row.
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(Outings::Table)
                    .if_not_exists()
                    .col(ColumnDef::new(Outings::Id).uuid().not_null().primary_key())
                    .col(ColumnDef::new(Outings::AreaId).uuid().not_null())
                    .col(ColumnDef::new(Outings::OnDate).date().not_null())
                    // open | decided | cancelled
                    .col(ColumnDef::new(Outings::Status).string_len(16).not_null().default("open"))
                    .col(ColumnDef::new(Outings::OptionCount).integer().not_null().default(3))
                    .col(ColumnDef::new(Outings::Rolls).integer().not_null().default(1))
                    .col(ColumnDef::new(Outings::LunchId).uuid().null())
                    .col(ColumnDef::new(Outings::CreatedBy).uuid().null())
                    .col(
                        ColumnDef::new(Outings::CreatedAt)
                            .timestamp_with_time_zone()
                            .not_null()
                            .default(Expr::current_timestamp()),
                    )
                    .foreign_key(
                        ForeignKey::create()
                            .name("fk_outings_area_id")
                            .from(Outings::Table, Outings::AreaId)
                            .to(Areas::Table, Areas::Id)
                            .on_delete(ForeignKeyAction::Cascade),
                    )
                    .foreign_key(
                        ForeignKey::create()
                            .name("fk_outings_lunch_id")
                            .from(Outings::Table, Outings::LunchId)
                            .to(Lunches::Table, Lunches::Id)
                            .on_delete(ForeignKeyAction::SetNull),
                    )
                    .foreign_key(
                        ForeignKey::create()
                            .name("fk_outings_created_by")
                            .from(Outings::Table, Outings::CreatedBy)
                            .to(Users::Table, Users::Id)
                            .on_delete(ForeignKeyAction::SetNull),
                    )
                    .to_owned(),
            )
            .await?;

        manager
            .create_index(
                Index::create()
                    .name("idx_outings_area_status")
                    .table(Outings::Table)
                    .col(Outings::AreaId)
                    .col(Outings::Status)
                    .to_owned(),
            )
            .await?;

        manager
            .create_table(
                Table::create()
                    .table(OutingAttendees::Table)
                    .if_not_exists()
                    .col(ColumnDef::new(OutingAttendees::OutingId).uuid().not_null())
                    .col(ColumnDef::new(OutingAttendees::UserId).uuid().not_null())
                    .primary_key(
                        Index::create().col(OutingAttendees::OutingId).col(OutingAttendees::UserId),
                    )
                    .foreign_key(
                        ForeignKey::create()
                            .name("fk_outing_attendees_outing_id")
                            .from(OutingAttendees::Table, OutingAttendees::OutingId)
                            .to(Outings::Table, Outings::Id)
                            .on_delete(ForeignKeyAction::Cascade),
                    )
                    .foreign_key(
                        ForeignKey::create()
                            .name("fk_outing_attendees_user_id")
                            .from(OutingAttendees::Table, OutingAttendees::UserId)
                            .to(Users::Table, Users::Id)
                            .on_delete(ForeignKeyAction::Cascade),
                    )
                    .to_owned(),
            )
            .await?;

        // Every restaurant an outing has ever shown. `active` is the current
        // ballot; rolled-away options stay so a re-roll can avoid them.
        manager
            .create_table(
                Table::create()
                    .table(OutingOptions::Table)
                    .if_not_exists()
                    .col(ColumnDef::new(OutingOptions::OutingId).uuid().not_null())
                    .col(ColumnDef::new(OutingOptions::RestaurantId).uuid().not_null())
                    .col(ColumnDef::new(OutingOptions::Roll).integer().not_null())
                    .col(ColumnDef::new(OutingOptions::Probability).double().not_null())
                    .col(ColumnDef::new(OutingOptions::Active).boolean().not_null().default(true))
                    .primary_key(
                        Index::create().col(OutingOptions::OutingId).col(OutingOptions::RestaurantId),
                    )
                    .foreign_key(
                        ForeignKey::create()
                            .name("fk_outing_options_outing_id")
                            .from(OutingOptions::Table, OutingOptions::OutingId)
                            .to(Outings::Table, Outings::Id)
                            .on_delete(ForeignKeyAction::Cascade),
                    )
                    .foreign_key(
                        ForeignKey::create()
                            .name("fk_outing_options_restaurant_id")
                            .from(OutingOptions::Table, OutingOptions::RestaurantId)
                            .to(Restaurants::Table, Restaurants::Id)
                            .on_delete(ForeignKeyAction::Cascade),
                    )
                    .to_owned(),
            )
            .await?;

        // Approval votes: one row per person per option they'd go to.
        manager
            .create_table(
                Table::create()
                    .table(OutingVotes::Table)
                    .if_not_exists()
                    .col(ColumnDef::new(OutingVotes::OutingId).uuid().not_null())
                    .col(ColumnDef::new(OutingVotes::UserId).uuid().not_null())
                    .col(ColumnDef::new(OutingVotes::RestaurantId).uuid().not_null())
                    .primary_key(
                        Index::create()
                            .col(OutingVotes::OutingId)
                            .col(OutingVotes::UserId)
                            .col(OutingVotes::RestaurantId),
                    )
                    .foreign_key(
                        ForeignKey::create()
                            .name("fk_outing_votes_option")
                            .from(OutingVotes::Table, (OutingVotes::OutingId, OutingVotes::RestaurantId))
                            .to(OutingOptions::Table, (OutingOptions::OutingId, OutingOptions::RestaurantId))
                            .on_delete(ForeignKeyAction::Cascade),
                    )
                    .foreign_key(
                        ForeignKey::create()
                            .name("fk_outing_votes_user_id")
                            .from(OutingVotes::Table, OutingVotes::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 [
            OutingVotes::Table.into_iden(),
            OutingOptions::Table.into_iden(),
            OutingAttendees::Table.into_iden(),
            Outings::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,
}

#[derive(DeriveIden)]
enum Restaurants {
    Table,
    Id,
}

#[derive(DeriveIden)]
enum Lunches {
    Table,
    Id,
}

#[derive(DeriveIden)]
enum Outings {
    Table,
    Id,
    AreaId,
    OnDate,
    Status,
    OptionCount,
    Rolls,
    LunchId,
    CreatedBy,
    CreatedAt,
}

#[derive(DeriveIden)]
enum OutingAttendees {
    Table,
    OutingId,
    UserId,
}

#[derive(DeriveIden)]
enum OutingOptions {
    Table,
    OutingId,
    RestaurantId,
    Roll,
    Probability,
    Active,
}

#[derive(DeriveIden)]
enum OutingVotes {
    Table,
    OutingId,
    UserId,
    RestaurantId,
}