migratrom 0.2.0

Declarative, self-verifying database migrations (core engine).
Documentation
//! PostgreSQL catalog existence probes.
//!
//! Each function returns a `SELECT` that yields a single boolean. `negate`
//! flips the sense (`SELECT NOT ...`), which builders use to assert a thing is
//! absent (precheck) or present (postcheck).

use super::identifiers::{quote_literal, regclass_literal};

fn exists_select(exists: &str, negate: bool) -> String {
    if negate {
        format!("SELECT NOT {exists}")
    } else {
        format!("SELECT {exists}")
    }
}

pub fn column_exists_sql(schema: &str, table: &str, column: &str, negate: bool) -> String {
    let exists = format!(
        "EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = {}
      AND table_name = {}
      AND column_name = {}
  )",
        quote_literal(schema),
        quote_literal(table),
        quote_literal(column)
    );
    exists_select(&exists, negate)
}

pub fn constraint_exists_sql(schema: &str, table: &str, name: &str, negate: bool) -> String {
    let exists = format!(
        "EXISTS (SELECT 1 FROM pg_constraint WHERE conname = {} AND conrelid = {}::regclass)",
        quote_literal(name),
        regclass_literal(schema, table)
    );
    exists_select(&exists, negate)
}

pub fn primary_key_exists_sql(schema: &str, table: &str, negate: bool) -> String {
    let exists = format!(
        "EXISTS (SELECT 1 FROM pg_constraint WHERE contype = 'p' AND conrelid = {}::regclass)",
        regclass_literal(schema, table)
    );
    exists_select(&exists, negate)
}

pub fn check_constraint_exists_sql(schema: &str, table: &str, name: &str, negate: bool) -> String {
    let exists = format!(
        "EXISTS (SELECT 1 FROM pg_constraint WHERE conname = {} AND contype = 'c' AND conrelid = {}::regclass)",
        quote_literal(name),
        regclass_literal(schema, table)
    );
    exists_select(&exists, negate)
}

pub fn schema_exists_sql(schema: &str, negate: bool) -> String {
    let exists = format!(
        "EXISTS (
    SELECT 1 FROM information_schema.schemata
    WHERE schema_name = {}
  )",
        quote_literal(schema)
    );
    exists_select(&exists, negate)
}

pub fn extension_exists_sql(name: &str, negate: bool) -> String {
    let exists = format!(
        "EXISTS (SELECT 1 FROM pg_extension WHERE extname = {})",
        quote_literal(name)
    );
    exists_select(&exists, negate)
}

pub fn regclass_exists_sql(schema: &str, name: &str, negate: bool) -> String {
    let reg = regclass_literal(schema, name);
    if negate {
        format!("SELECT to_regclass({reg}) IS NULL")
    } else {
        format!("SELECT to_regclass({reg}) IS NOT NULL")
    }
}

pub fn enum_type_exists_sql(schema: &str, name: &str, negate: bool) -> String {
    let exists = format!(
        "EXISTS (
    SELECT 1 FROM pg_type t
    JOIN pg_namespace n ON n.oid = t.typnamespace
    WHERE n.nspname = {}
      AND t.typname = {}
      AND t.typtype = 'e'
  )",
        quote_literal(schema),
        quote_literal(name)
    );
    exists_select(&exists, negate)
}

pub fn enum_label_exists_sql(schema: &str, type_name: &str, value: &str, negate: bool) -> String {
    let exists = format!(
        "EXISTS (
    SELECT 1 FROM pg_enum e
    JOIN pg_type t ON t.oid = e.enumtypid
    JOIN pg_namespace n ON n.oid = t.typnamespace
    WHERE n.nspname = {}
      AND t.typname = {}
      AND e.enumlabel = {}
  )",
        quote_literal(schema),
        quote_literal(type_name),
        quote_literal(value)
    );
    exists_select(&exists, negate)
}

pub fn view_exists_sql(schema: &str, name: &str, negate: bool) -> String {
    let exists = format!(
        "EXISTS (
    SELECT 1 FROM information_schema.views
    WHERE table_schema = {}
      AND table_name = {}
  )",
        quote_literal(schema),
        quote_literal(name)
    );
    exists_select(&exists, negate)
}

pub fn matview_exists_sql(schema: &str, name: &str, negate: bool) -> String {
    let exists = format!(
        "EXISTS (SELECT 1 FROM pg_matviews WHERE matviewname = {} AND schemaname = {})",
        quote_literal(name),
        quote_literal(schema)
    );
    exists_select(&exists, negate)
}

pub fn column_default_set_sql(schema: &str, table: &str, column: &str, negate: bool) -> String {
    let exists = format!(
        "EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = {}
      AND table_name = {}
      AND column_name = {}
      AND column_default IS NOT NULL
  )",
        quote_literal(schema),
        quote_literal(table),
        quote_literal(column)
    );
    exists_select(&exists, negate)
}

pub fn column_not_null_sql(schema: &str, table: &str, column: &str, negate: bool) -> String {
    let exists = format!(
        "EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_schema = {}
      AND table_name = {}
      AND column_name = {}
      AND is_nullable = 'NO'
  )",
        quote_literal(schema),
        quote_literal(table),
        quote_literal(column)
    );
    exists_select(&exists, negate)
}