no-mistakes-postgres-engine 0.109.125

PostgreSQL embedded SQL extraction, schema catalog, and replay-safety analysis for no-mistakes
Documentation
mod allow;
pub use allow::canonical_key;
use anyhow::{bail, Result};
use no_mistakes_rule_kernel::finding::RuleFinding;
use serde::Deserialize;
use std::collections::BTreeSet;
use std::fmt;
use std::str::FromStr;

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CatalogObjectRef {
    Table(String),
    Column { table: String, column: String },
    Index { table: String, index: String },
    Trigger { table: String, trigger: String },
    Constraint { table: String, name: String },
    Function(String),
    Enum(String),
    View(String),
    MaterializedView(String),
}

impl fmt::Display for CatalogObjectRef {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Table(name) => write!(formatter, "table:{name}"),
            Self::Column { table, column } => write!(formatter, "column:{table}.{column}"),
            Self::Index { table, index } => write!(formatter, "index:{table}.{index}"),
            Self::Trigger { table, trigger } => write!(formatter, "trigger:{table}.{trigger}"),
            Self::Constraint { table, name } => write!(formatter, "constraint:{table}.{name}"),
            Self::Function(key) => write!(formatter, "function:{key}"),
            Self::Enum(name) => write!(formatter, "enum:{name}"),
            Self::View(name) => write!(formatter, "view:{name}"),
            Self::MaterializedView(name) => write!(formatter, "materialized-view:{name}"),
        }
    }
}

impl FromStr for CatalogObjectRef {
    type Err = ();

    fn from_str(raw: &str) -> Result<Self, ()> {
        if let Some(rest) = raw.strip_prefix("materialized-view:") {
            return Ok(Self::MaterializedView(non_empty(rest)?));
        }
        let (prefix, rest) = raw.split_once(':').ok_or(())?;
        let object = match prefix {
            "table" => Self::Table(non_empty(rest)?),
            "column" => {
                let (table, column) = pair(rest)?;
                Self::Column { table, column }
            }
            "index" => {
                let (table, index) = pair(rest)?;
                Self::Index { table, index }
            }
            "trigger" => {
                let (table, trigger) = pair(rest)?;
                Self::Trigger { table, trigger }
            }
            "constraint" => {
                let (table, name) = pair(rest)?;
                Self::Constraint { table, name }
            }
            "function" => Self::Function(non_empty(rest)?),
            "enum" => Self::Enum(non_empty(rest)?),
            "view" => Self::View(non_empty(rest)?),
            _ => return Err(()),
        };
        Ok(object)
    }
}

fn non_empty(raw: &str) -> Result<String, ()> {
    (!raw.trim().is_empty()).then(|| raw.to_string()).ok_or(())
}

fn pair(raw: &str) -> Result<(String, String), ()> {
    let (left, right) = raw.rsplit_once('.').ok_or(())?;
    Ok((non_empty(left)?, non_empty(right)?))
}

pub fn catalog_finding(
    rule_id: &str,
    catalog_path: &str,
    object: &CatalogObjectRef,
    text: &str,
) -> RuleFinding {
    let path = slash_normalize(catalog_path);
    let object = object.to_string();
    RuleFinding {
        causes: Vec::new(),
        rule: rule_id.to_string(),
        file: path.clone(),
        line: 1,
        message: format!("{path}: {object}: {text}"),
        import: None,
        target: Some(object),
    }
}

pub fn require_catalog_path(rule_id: &str, path: &str) -> Result<()> {
    if path.trim().is_empty() {
        bail!("{rule_id} option schemaCatalogPath: required");
    }
    Ok(())
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AllowEntry {
    pub object: String,
    pub reason: String,
}

#[derive(Debug, Clone)]
pub struct AllowList {
    rule_id: String,
    entries: Vec<AllowEntry>,
    keys: Vec<String>,
}

impl AllowList {
    pub fn contains(&self, object: &str) -> bool {
        let key = canonical_key(object);
        self.keys.contains(&key)
    }

    /// Entries and finding targets share one key: the table part decoded the way the catalog
    /// reader normalizes table names, so `table:"users"` and `table:users` are one key and
    /// `table:"Orders"` (how `quote_ident` writes a mixed-case table) is `table:Orders`.
    pub fn entry_keys(&self) -> impl Iterator<Item = (usize, &str)> + '_ {
        self.keys
            .iter()
            .enumerate()
            .map(|(index, key)| (index, key.as_str()))
    }

    pub fn entry_objects(&self) -> impl Iterator<Item = (usize, &str)> + '_ {
        self.entries
            .iter()
            .enumerate()
            .map(|(index, entry)| (index, entry.object.as_str()))
    }

    pub fn compile(rule_id: &str, entries: Vec<AllowEntry>) -> Result<Self> {
        let mut seen = BTreeSet::new();
        for entry in &entries {
            if entry.reason.trim().is_empty() {
                bail!(
                    "{rule_id} option allow: entry {} needs a reason",
                    entry.object
                );
            }
            if entry.object.parse::<CatalogObjectRef>().is_err() {
                bail!(
                    "{rule_id} option allow: invalid object ref {}",
                    entry.object
                );
            }
            if !seen.insert(canonical_key(&entry.object)) {
                bail!("{rule_id} option allow: duplicate entry {}", entry.object);
            }
        }
        let keys = entries
            .iter()
            .map(|entry| canonical_key(&entry.object))
            .collect();
        Ok(Self {
            rule_id: rule_id.to_string(),
            entries,
            keys,
        })
    }
}

fn slash_normalize(path: &str) -> String {
    path.replace('\\', "/")
}