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", "media_storage_provider")
            .await?
        {
            return Ok(());
        }

        manager
            .alter_table(
                Table::alter()
                    .table(SiteSettings::Table)
                    .add_column(
                        ColumnDef::new(SiteSettings::MediaStorageProvider)
                            .string()
                            .null(),
                    )
                    .to_owned(),
            )
            .await
    }

    async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        if !manager
            .has_column("site_settings", "media_storage_provider")
            .await?
        {
            return Ok(());
        }

        manager
            .alter_table(
                Table::alter()
                    .table(SiteSettings::Table)
                    .drop_column(SiteSettings::MediaStorageProvider)
                    .to_owned(),
            )
            .await
    }
}

#[derive(DeriveIden)]
enum SiteSettings {
    Table,
    MediaStorageProvider,
}
