4.4 KiB · text History 263b40b
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> {
        // Edit-tracking + soft-delete columns on posts.
        manager
            .alter_table(
                Table::alter()
                    .table(Posts::Table)
                    .add_column(ColumnDef::new(Posts::EditedAt).timestamp_with_time_zone().null())
                    .add_column(ColumnDef::new(Posts::EditCount).integer().not_null().default(0))
                    .add_column(ColumnDef::new(Posts::LastEditorId).uuid().null())
                    .add_column(ColumnDef::new(Posts::DeletedAt).timestamp_with_time_zone().null())
                    .add_column(ColumnDef::new(Posts::DeletedById).uuid().null())
                    .to_owned(),
            )
            .await?;

        // Full version history: one row per version (revision 1 = original).
        manager
            .create_table(
                Table::create()
                    .table(PostRevisions::Table)
                    .if_not_exists()
                    .col(ColumnDef::new(PostRevisions::Id).uuid().not_null().primary_key())
                    .col(ColumnDef::new(PostRevisions::PostId).uuid().not_null())
                    .col(ColumnDef::new(PostRevisions::EditorId).uuid().null())
                    .col(ColumnDef::new(PostRevisions::RevisionNumber).integer().not_null())
                    .col(ColumnDef::new(PostRevisions::Raw).text().not_null())
                    .col(ColumnDef::new(PostRevisions::Cooked).text().not_null())
                    .col(ColumnDef::new(PostRevisions::CreatedAt).timestamp_with_time_zone().not_null().default(Expr::current_timestamp()))
                    .foreign_key(
                        ForeignKey::create()
                            .name("fk_post_revisions_post_id")
                            .from(PostRevisions::Table, PostRevisions::PostId)
                            .to(Posts::Table, Posts::Id)
                            .on_delete(ForeignKeyAction::Cascade)
                            .on_update(ForeignKeyAction::Cascade),
                    )
                    .foreign_key(
                        ForeignKey::create()
                            .name("fk_post_revisions_editor_id")
                            .from(PostRevisions::Table, PostRevisions::EditorId)
                            .to(Users::Table, Users::Id)
                            .on_delete(ForeignKeyAction::SetNull)
                            .on_update(ForeignKeyAction::Cascade),
                    )
                    .to_owned(),
            )
            .await?;

        manager
            .create_index(
                Index::create()
                    .name("idx_post_revisions_post_number")
                    .table(PostRevisions::Table)
                    .col(PostRevisions::PostId)
                    .col(PostRevisions::RevisionNumber)
                    .unique()
                    .to_owned(),
            )
            .await?;

        // Backfill: every existing post becomes its own revision 1.
        manager
            .get_connection()
            .execute_unprepared(
                "INSERT INTO post_revisions (id, post_id, editor_id, revision_number, raw, cooked, created_at) \
                 SELECT gen_random_uuid(), id, user_id, 1, raw, cooked, created_at FROM posts;",
            )
            .await?;

        Ok(())
    }

    async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager.drop_table(Table::drop().table(PostRevisions::Table).to_owned()).await?;
        manager
            .alter_table(
                Table::alter()
                    .table(Posts::Table)
                    .drop_column(Posts::EditedAt)
                    .drop_column(Posts::EditCount)
                    .drop_column(Posts::LastEditorId)
                    .drop_column(Posts::DeletedAt)
                    .drop_column(Posts::DeletedById)
                    .to_owned(),
            )
            .await?;
        Ok(())
    }
}

#[derive(DeriveIden)]
enum Posts {
    Table,
    Id,
    EditedAt,
    EditCount,
    LastEditorId,
    DeletedAt,
    DeletedById,
}

#[derive(DeriveIden)]
enum PostRevisions {
    Table,
    Id,
    PostId,
    EditorId,
    RevisionNumber,
    Raw,
    Cooked,
    CreatedAt,
}

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