use crate::{MigrationError, SchemaDialect, TableBlueprint};

#[allow(async_fn_in_trait)]
pub trait BlueprintExecutor {
    fn dialect(&self) -> SchemaDialect;
    async fn execute_raw_blueprint(&mut self, sql: &str) -> Result<u64, MigrationError>;
}

pub(crate) async fn execute_table_blueprint<C>(
    ctx: &mut C,
    table: TableBlueprint,
) -> Result<(), MigrationError>
where
    C: BlueprintExecutor,
{
    for sql in table.create_statements(ctx.dialect()) {
        ctx.execute_raw_blueprint(&sql).await?;
    }
    Ok(())
}
