7.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> {
        // categories
        manager
            .create_table(
                Table::create()
                    .table(Categories::Table)
                    .if_not_exists()
                    .col(ColumnDef::new(Categories::Id).uuid().not_null().primary_key())
                    .col(ColumnDef::new(Categories::Name).string_len(100).not_null())
                    .col(ColumnDef::new(Categories::Slug).string_len(120).unique_key().not_null())
                    .col(ColumnDef::new(Categories::Description).text().null())
                    .col(ColumnDef::new(Categories::Color).string_len(7).not_null().default("#0088CC"))
                    .col(ColumnDef::new(Categories::Position).integer().not_null().default(0))
                    .col(ColumnDef::new(Categories::CreatedAt).timestamp_with_time_zone().not_null().default(Expr::current_timestamp()))
                    .col(ColumnDef::new(Categories::UpdatedAt).timestamp_with_time_zone().not_null().default(Expr::current_timestamp()))
                    .to_owned(),
            )
            .await?;

        // topics
        manager
            .create_table(
                Table::create()
                    .table(Topics::Table)
                    .if_not_exists()
                    .col(ColumnDef::new(Topics::Id).uuid().not_null().primary_key())
                    .col(ColumnDef::new(Topics::CategoryId).uuid().not_null())
                    .col(ColumnDef::new(Topics::UserId).uuid().not_null())
                    .col(ColumnDef::new(Topics::Title).string_len(255).not_null())
                    .col(ColumnDef::new(Topics::IsPinned).boolean().not_null().default(false))
                    .col(ColumnDef::new(Topics::IsClosed).boolean().not_null().default(false))
                    .col(ColumnDef::new(Topics::Views).integer().not_null().default(0))
                    .col(ColumnDef::new(Topics::PostsCount).integer().not_null().default(0))
                    .col(ColumnDef::new(Topics::CreatedAt).timestamp_with_time_zone().not_null().default(Expr::current_timestamp()))
                    .col(ColumnDef::new(Topics::UpdatedAt).timestamp_with_time_zone().not_null().default(Expr::current_timestamp()))
                    .col(ColumnDef::new(Topics::BumpedAt).timestamp_with_time_zone().not_null().default(Expr::current_timestamp()))
                    .foreign_key(
                        ForeignKey::create()
                            .name("fk_topics_category_id")
                            .from(Topics::Table, Topics::CategoryId)
                            .to(Categories::Table, Categories::Id)
                            .on_delete(ForeignKeyAction::Cascade)
                            .on_update(ForeignKeyAction::Cascade),
                    )
                    .foreign_key(
                        ForeignKey::create()
                            .name("fk_topics_user_id")
                            .from(Topics::Table, Topics::UserId)
                            .to(Users::Table, Users::Id)
                            .on_delete(ForeignKeyAction::Cascade)
                            .on_update(ForeignKeyAction::Cascade),
                    )
                    .to_owned(),
            )
            .await?;

        // posts
        manager
            .create_table(
                Table::create()
                    .table(Posts::Table)
                    .if_not_exists()
                    .col(ColumnDef::new(Posts::Id).uuid().not_null().primary_key())
                    .col(ColumnDef::new(Posts::TopicId).uuid().not_null())
                    .col(ColumnDef::new(Posts::UserId).uuid().not_null())
                    .col(ColumnDef::new(Posts::PostNumber).integer().not_null())
                    .col(ColumnDef::new(Posts::Raw).text().not_null())
                    .col(ColumnDef::new(Posts::Cooked).text().not_null())
                    .col(ColumnDef::new(Posts::CreatedAt).timestamp_with_time_zone().not_null().default(Expr::current_timestamp()))
                    .col(ColumnDef::new(Posts::UpdatedAt).timestamp_with_time_zone().not_null().default(Expr::current_timestamp()))
                    .foreign_key(
                        ForeignKey::create()
                            .name("fk_posts_topic_id")
                            .from(Posts::Table, Posts::TopicId)
                            .to(Topics::Table, Topics::Id)
                            .on_delete(ForeignKeyAction::Cascade)
                            .on_update(ForeignKeyAction::Cascade),
                    )
                    .foreign_key(
                        ForeignKey::create()
                            .name("fk_posts_user_id")
                            .from(Posts::Table, Posts::UserId)
                            .to(Users::Table, Users::Id)
                            .on_delete(ForeignKeyAction::Cascade)
                            .on_update(ForeignKeyAction::Cascade),
                    )
                    .to_owned(),
            )
            .await?;

        manager.create_index(Index::create().name("idx_topics_category_id").table(Topics::Table).col(Topics::CategoryId).to_owned()).await?;
        manager.create_index(Index::create().name("idx_topics_bumped_at").table(Topics::Table).col(Topics::BumpedAt).to_owned()).await?;
        manager.create_index(Index::create().name("idx_posts_topic_id").table(Posts::Table).col(Posts::TopicId).to_owned()).await?;
        manager
            .create_index(
                Index::create()
                    .name("idx_posts_topic_number")
                    .table(Posts::Table)
                    .col(Posts::TopicId)
                    .col(Posts::PostNumber)
                    .unique()
                    .to_owned(),
            )
            .await?;

        // Seed a couple of starter categories so topics can be created right away.
        let db = manager.get_connection();
        db.execute_unprepared(
            "INSERT INTO categories (id, name, slug, description, color, position, created_at, updated_at) VALUES \
             (gen_random_uuid(), 'Announcements', 'announcements', 'Official announcements and news.', '#E45735', 0, now(), now()), \
             (gen_random_uuid(), 'General', 'general', 'General discussion about anything.', '#0088CC', 1, now(), now()) \
             ON CONFLICT (slug) DO NOTHING;",
        )
        .await?;

        Ok(())
    }

    async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager.drop_table(Table::drop().table(Posts::Table).to_owned()).await?;
        manager.drop_table(Table::drop().table(Topics::Table).to_owned()).await?;
        manager.drop_table(Table::drop().table(Categories::Table).to_owned()).await?;
        Ok(())
    }
}

#[derive(DeriveIden)]
enum Categories {
    Table,
    Id,
    Name,
    Slug,
    Description,
    Color,
    Position,
    CreatedAt,
    UpdatedAt,
}

#[derive(DeriveIden)]
enum Topics {
    Table,
    Id,
    CategoryId,
    UserId,
    Title,
    IsPinned,
    IsClosed,
    Views,
    PostsCount,
    CreatedAt,
    UpdatedAt,
    BumpedAt,
}

#[derive(DeriveIden)]
enum Posts {
    Table,
    Id,
    TopicId,
    UserId,
    PostNumber,
    Raw,
    Cooked,
    CreatedAt,
    UpdatedAt,
}

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