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(Article::Table) .if_not_exists() .col(ColumnDef::new(Article::Id).uuid().not_null().primary_key()) .col(ColumnDef::new(Article::TenantId).uuid().not_null()) .col(ColumnDef::new(Article::Title).string_len(200).not_null()) .col(ColumnDef::new(Article::Summary).text().null()) .col(ColumnDef::new(Article::Content).text().not_null()) .col(ColumnDef::new(Article::CoverImage).string_len(500).null()) .col(ColumnDef::new(Article::Category).string_len(50).null()) .col(ColumnDef::new(Article::Author).string_len(100).null()) .col(ColumnDef::new(Article::PublishedAt).timestamp_with_time_zone().null()) .col( ColumnDef::new(Article::CreatedAt) .timestamp_with_time_zone() .not_null() .default(Expr::current_timestamp()), ) .col( ColumnDef::new(Article::UpdatedAt) .timestamp_with_time_zone() .not_null() .default(Expr::current_timestamp()), ) .col(ColumnDef::new(Article::CreatedBy).uuid().null()) .col(ColumnDef::new(Article::UpdatedBy).uuid().null()) .col(ColumnDef::new(Article::DeletedAt).timestamp_with_time_zone().null()) .col( ColumnDef::new(Article::Version) .integer() .not_null() .default(1), ) .to_owned(), ) .await?; manager .create_index( Index::create() .if_not_exists() .name("idx_article_tenant_category") .table(Article::Table) .col(Article::TenantId) .col(Article::Category) .to_owned(), ) .await?; manager .create_index( Index::create() .if_not_exists() .name("idx_article_tenant_published") .table(Article::Table) .col(Article::TenantId) .col(Article::PublishedAt) .to_owned(), ) .await } async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { manager .drop_index(Index::drop().name("idx_article_tenant_published").to_owned()) .await?; manager .drop_index(Index::drop().name("idx_article_tenant_category").to_owned()) .await?; manager .drop_table(Table::drop().table(Article::Table).to_owned()) .await } } #[derive(DeriveIden)] enum Article { Table, Id, TenantId, Title, Summary, Content, CoverImage, Category, Author, PublishedAt, CreatedAt, UpdatedAt, CreatedBy, UpdatedBy, DeletedAt, Version, }