3.3 KiB · text History 487bb12
//! `snippets`: a saved code card. The look is stored as plain ids (chrome,
//! palette, highlight theme) rather than enums, so adding a decoration or a
//! scheme never needs a migration; unknown ids fall back at render time.
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(Snippets::Table)
                    .if_not_exists()
                    .col(ColumnDef::new(Snippets::Id).uuid().not_null().primary_key())
                    .col(ColumnDef::new(Snippets::UserId).uuid().not_null())
                    .col(ColumnDef::new(Snippets::Slug).string_len(32).not_null().unique_key())
                    .col(ColumnDef::new(Snippets::Title).string_len(255).not_null().default(""))
                    .col(ColumnDef::new(Snippets::Language).string_len(64).not_null())
                    .col(ColumnDef::new(Snippets::Code).text().not_null())
                    .col(ColumnDef::new(Snippets::Chrome).string_len(32).not_null())
                    .col(ColumnDef::new(Snippets::Palette).string_len(32).not_null())
                    .col(ColumnDef::new(Snippets::Highlight).string_len(32).not_null())
                    .col(ColumnDef::new(Snippets::Transparent).boolean().not_null().default(false))
                    .col(ColumnDef::new(Snippets::Padding).integer().not_null().default(48))
                    .col(ColumnDef::new(Snippets::LineNumbers).boolean().not_null().default(false))
                    .col(
                        ColumnDef::new(Snippets::CreatedAt)
                            .timestamp_with_time_zone()
                            .not_null()
                            .default(Expr::current_timestamp()),
                    )
                    .col(
                        ColumnDef::new(Snippets::UpdatedAt)
                            .timestamp_with_time_zone()
                            .not_null()
                            .default(Expr::current_timestamp()),
                    )
                    .foreign_key(
                        ForeignKey::create()
                            .name("fk_snippets_user_id")
                            .from(Snippets::Table, Snippets::UserId)
                            .to(Alias::new("users"), Alias::new("id"))
                            .on_delete(ForeignKeyAction::Cascade)
                            .on_update(ForeignKeyAction::Cascade),
                    )
                    .to_owned(),
            )
            .await?;

        manager
            .create_index(
                Index::create()
                    .name("idx_snippets_user_id")
                    .table(Snippets::Table)
                    .col(Snippets::UserId)
                    .to_owned(),
            )
            .await?;

        Ok(())
    }

    async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager.drop_table(Table::drop().table(Snippets::Table).to_owned()).await
    }
}

#[derive(DeriveIden)]
enum Snippets {
    Table,
    Id,
    UserId,
    Slug,
    Title,
    Language,
    Code,
    Chrome,
    Palette,
    Highlight,
    Transparent,
    Padding,
    LineNumbers,
    CreatedAt,
    UpdatedAt,
}