1.5 KiB · text History 504ebaf
//! `Obsessed { since }`: a rank stronger than Always, carrying the date the
//! obsession began. The rank column already holds the kind as a string
//! ("obsessed" fits its 16 chars); the date gets its own column, set only
//! when the rank is "obsessed".
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
            .alter_table(
                Table::alter()
                    .table(Rankings::Table)
                    .add_column(ColumnDef::new(Rankings::ObsessedSince).date().null())
                    .to_owned(),
            )
            .await
    }

    async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        // An obsession without its date isn't representable; fall back to
        // the next strongest rank.
        manager
            .exec_stmt(
                Query::update()
                    .table(Rankings::Table)
                    .value(Rankings::Rank, "always")
                    .and_where(Expr::col(Rankings::Rank).eq("obsessed"))
                    .to_owned(),
            )
            .await?;
        manager
            .alter_table(Table::alter().table(Rankings::Table).drop_column(Rankings::ObsessedSince).to_owned())
            .await
    }
}

#[derive(DeriveIden)]
enum Rankings {
    Table,
    Rank,
    ObsessedSince,
}