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("posts", "source_markdown").await? {
            manager
                .alter_table(
                    Table::alter()
                        .table(Alias::new("posts"))
                        .add_column(ColumnDef::new(Alias::new("source_markdown")).text().null())
                        .to_owned(),
                )
                .await?;
        }

        Ok(())
    }

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

        Ok(())
    }
}
