4.5 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> {
        // category_watches
        manager
            .create_table(
                Table::create()
                    .table(CategoryWatches::Table)
                    .if_not_exists()
                    .col(ColumnDef::new(CategoryWatches::Id).uuid().not_null().primary_key())
                    .col(ColumnDef::new(CategoryWatches::UserId).uuid().not_null())
                    .col(ColumnDef::new(CategoryWatches::CategoryId).uuid().not_null())
                    .col(ColumnDef::new(CategoryWatches::CreatedAt).timestamp_with_time_zone().not_null().default(Expr::current_timestamp()))
                    .foreign_key(ForeignKey::create().name("fk_category_watches_user").from(CategoryWatches::Table, CategoryWatches::UserId).to(Alias::new("users"), Alias::new("id")).on_delete(ForeignKeyAction::Cascade).on_update(ForeignKeyAction::Cascade))
                    .foreign_key(ForeignKey::create().name("fk_category_watches_category").from(CategoryWatches::Table, CategoryWatches::CategoryId).to(Alias::new("categories"), Alias::new("id")).on_delete(ForeignKeyAction::Cascade).on_update(ForeignKeyAction::Cascade))
                    .to_owned(),
            )
            .await?;
        manager
            .create_index(Index::create().name("idx_category_watches_user_cat").table(CategoryWatches::Table).col(CategoryWatches::UserId).col(CategoryWatches::CategoryId).unique().to_owned())
            .await?;

        // notifications
        manager
            .create_table(
                Table::create()
                    .table(Notifications::Table)
                    .if_not_exists()
                    .col(ColumnDef::new(Notifications::Id).uuid().not_null().primary_key())
                    .col(ColumnDef::new(Notifications::UserId).uuid().not_null())
                    .col(ColumnDef::new(Notifications::Kind).string_len(32).not_null())
                    .col(ColumnDef::new(Notifications::ActorId).uuid().null())
                    .col(ColumnDef::new(Notifications::TopicId).uuid().null())
                    .col(ColumnDef::new(Notifications::PostId).uuid().null())
                    .col(ColumnDef::new(Notifications::IsRead).boolean().not_null().default(false))
                    .col(ColumnDef::new(Notifications::CreatedAt).timestamp_with_time_zone().not_null().default(Expr::current_timestamp()))
                    .foreign_key(ForeignKey::create().name("fk_notifications_user").from(Notifications::Table, Notifications::UserId).to(Alias::new("users"), Alias::new("id")).on_delete(ForeignKeyAction::Cascade).on_update(ForeignKeyAction::Cascade))
                    .foreign_key(ForeignKey::create().name("fk_notifications_actor").from(Notifications::Table, Notifications::ActorId).to(Alias::new("users"), Alias::new("id")).on_delete(ForeignKeyAction::SetNull).on_update(ForeignKeyAction::Cascade))
                    .foreign_key(ForeignKey::create().name("fk_notifications_topic").from(Notifications::Table, Notifications::TopicId).to(Alias::new("topics"), Alias::new("id")).on_delete(ForeignKeyAction::Cascade).on_update(ForeignKeyAction::Cascade))
                    .foreign_key(ForeignKey::create().name("fk_notifications_post").from(Notifications::Table, Notifications::PostId).to(Alias::new("posts"), Alias::new("id")).on_delete(ForeignKeyAction::Cascade).on_update(ForeignKeyAction::Cascade))
                    .to_owned(),
            )
            .await?;
        manager
            .create_index(Index::create().name("idx_notifications_user_read").table(Notifications::Table).col(Notifications::UserId).col(Notifications::IsRead).to_owned())
            .await?;
        manager
            .create_index(Index::create().name("idx_notifications_user_created").table(Notifications::Table).col(Notifications::UserId).col(Notifications::CreatedAt).to_owned())
            .await?;
        Ok(())
    }

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

#[derive(DeriveIden)]
enum CategoryWatches {
    Table,
    Id,
    UserId,
    CategoryId,
    CreatedAt,
}

#[derive(DeriveIden)]
enum Notifications {
    Table,
    Id,
    UserId,
    Kind,
    ActorId,
    TopicId,
    PostId,
    IsRead,
    CreatedAt,
}