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> {
        if !manager
            .has_column("site_settings", "paragraph_comments_enabled")
            .await?
        {
            manager
                .alter_table(
                    Table::alter()
                        .table(Alias::new("site_settings"))
                        .add_column(
                            ColumnDef::new(Alias::new("paragraph_comments_enabled"))
                                .boolean()
                                .null()
                                .default(true),
                        )
                        .to_owned(),
                )
                .await?;
        }

        Ok(())
    }

    async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        if manager
            .has_column("site_settings", "paragraph_comments_enabled")
            .await?
        {
            manager
                .alter_table(
                    Table::alter()
                        .table(Alias::new("site_settings"))
                        .drop_column(Alias::new("paragraph_comments_enabled"))
                        .to_owned(),
                )
                .await?;
        }

        Ok(())
    }
}
