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

        manager
            .alter_table(
                Table::alter()
                    .table(Alias::new("site_settings"))
                    .add_column(ColumnDef::new(Alias::new("site_url")).string().null())
                    .to_owned(),
            )
            .await
    }

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

        manager
            .alter_table(
                Table::alter()
                    .table(Alias::new("site_settings"))
                    .drop_column(Alias::new("site_url"))
                    .to_owned(),
            )
            .await
    }
}
