migratrom 0.2.0

Declarative, self-verifying database migrations (core engine).
Documentation
//! Column-altering builders: set default, set NOT NULL.

use super::helpers::{check, require_capability, step};
use crate::error::Result;
use crate::sql::SqlDialect;
use crate::types::Operation;

/// Set a default expression on an existing column without a default.
///
/// `default_sql` is the clause after `ALTER COLUMN <col> SET`, starting with
/// `DEFAULT`, e.g. `"DEFAULT (now())"`.
pub fn set_column_default(
    schema: &str,
    table: &str,
    column: &str,
    default_sql: &str,
    dialect: &dyn SqlDialect,
) -> Result<Operation> {
    require_capability(
        dialect,
        dialect.capabilities().alter_column_default,
        "ALTER COLUMN SET DEFAULT",
    )?;
    let alter_sql = format!(
        "ALTER TABLE {} ALTER COLUMN {} SET {default_sql}",
        dialect.qualified(schema, table),
        dialect.quote_ident(column)
    );

    Ok(Operation {
        id: format!("column_default.{table}.{column}"),
        label: format!("Set default on column \"{column}\" of \"{table}\""),
        precheck: vec![
            check(
                format!("ensure column \"{column}\" exists on \"{table}\""),
                dialect.column_exists_sql(schema, table, column, false),
            ),
            check(
                format!("ensure column \"{column}\" has no default yet on \"{table}\""),
                dialect.column_default_set_sql(schema, table, column, true),
            ),
        ],
        execute: vec![step(
            format!("set default on column \"{column}\" of \"{table}\""),
            alter_sql,
        )],
        postcheck: vec![check(
            format!("verify column \"{column}\" has a default on \"{table}\""),
            dialect.column_default_set_sql(schema, table, column, false),
        )],
        outside_transaction: false,
    })
}

/// Promote a nullable column to `NOT NULL`.
pub fn set_column_not_null(
    schema: &str,
    table: &str,
    column: &str,
    dialect: &dyn SqlDialect,
) -> Result<Operation> {
    require_capability(
        dialect,
        dialect.capabilities().alter_column_not_null,
        "ALTER COLUMN SET NOT NULL",
    )?;
    let alter_sql = format!(
        "ALTER TABLE {} ALTER COLUMN {} SET NOT NULL",
        dialect.qualified(schema, table),
        dialect.quote_ident(column)
    );

    Ok(Operation {
        id: format!("column_not_null.{table}.{column}"),
        label: format!("Set NOT NULL on column \"{column}\" of \"{table}\""),
        precheck: vec![
            check(
                format!("ensure column \"{column}\" exists on \"{table}\""),
                dialect.column_exists_sql(schema, table, column, false),
            ),
            check(
                format!("ensure column \"{column}\" is nullable on \"{table}\""),
                dialect.column_not_null_sql(schema, table, column, true),
            ),
        ],
        execute: vec![step(
            format!("set NOT NULL on column \"{column}\" of \"{table}\""),
            alter_sql,
        )],
        postcheck: vec![check(
            format!("verify column \"{column}\" is NOT NULL on \"{table}\""),
            dialect.column_not_null_sql(schema, table, column, false),
        )],
        outside_transaction: false,
    })
}