mod arbiter;
mod columns;
mod objects;
use super::function::function_from_definition;
use super::model::{CatalogEnum, CatalogFunction, CatalogTable, CatalogView};
use super::names::normalize_table_name;
use super::snapshot::{Snapshot, SnapshotTable};
use super::SchemaCatalog;
use anyhow::Result;
use std::collections::BTreeMap;
pub(super) fn from_snapshot(path: &str, snapshot: Snapshot) -> Result<SchemaCatalog> {
let mut tables = BTreeMap::new();
let mut model_tables = BTreeMap::new();
let mut row_security = BTreeMap::new();
let mut descendant_presence = BTreeMap::new();
let mut trigger_function_identities = BTreeMap::new();
for (name, table) in snapshot.tables {
let key = normalize_table_name(&name);
anyhow::ensure!(
!model_tables.contains_key(&key),
"schemaCatalogPath {path} has duplicate table keys for {key}. Regenerate it with `no-mistakes postgres catalog`"
);
let identities = trigger_function_identities.entry(name.clone()).or_default();
let model = model_table(path, &name, &table, identities)?;
if let Some(present) = table.has_descendants {
descendant_presence.insert(key.clone(), present);
}
tables.insert(key.clone(), arbiter::arbiter_table(&table));
if let Some(security) = table.row_security {
row_security.insert(key.clone(), security);
}
model_tables.insert(key, model);
}
let functions = functions(snapshot.functions, snapshot.schema.as_deref());
Ok(SchemaCatalog {
coverage: snapshot.coverage,
schema: snapshot.schema,
search_path_evidence: snapshot.search_path_evidence,
current_database: snapshot.current_database,
tables,
model_tables,
row_security,
descendant_presence,
trigger_function_identities,
relation_fallback: Default::default(),
functions,
projections: Default::default(),
enums: enums(snapshot.enums),
views: views(snapshot.views),
view_fallback: Default::default(),
locations: Default::default(),
})
}
fn functions(
functions: BTreeMap<String, super::snapshot::SnapshotFunction>,
schema: Option<&str>,
) -> BTreeMap<String, CatalogFunction> {
functions
.into_iter()
.map(|(key, function)| {
let mut catalog_function = function_from_definition(&key, &function.definition);
catalog_function.returns_set = super::function_cardinality::resolve(
function.returns_set,
&function.definition,
&catalog_function.name,
&catalog_function.return_contract,
schema,
);
(key, catalog_function)
})
.collect()
}
fn enums(enums: BTreeMap<String, super::snapshot::SnapshotEnum>) -> BTreeMap<String, CatalogEnum> {
enums
.into_iter()
.map(|(name, enum_type)| {
(
name.clone(),
CatalogEnum {
name,
values: enum_type.values,
},
)
})
.collect()
}
fn views(views: BTreeMap<String, super::snapshot::SnapshotView>) -> BTreeMap<String, CatalogView> {
views
.into_iter()
.map(|(name, view)| {
(
normalize_table_name(&name),
CatalogView {
indexes: view.indexes.as_ref().map(objects::indexes),
name,
materialized: view.materialized,
definition: view.definition,
comment: view.comment,
},
)
})
.collect()
}
fn model_table(
path: &str,
name: &str,
table: &SnapshotTable,
identities: &mut BTreeMap<String, String>,
) -> Result<CatalogTable> {
Ok(CatalogTable {
name: name.to_string(),
relation_kind: columns::relation_kind(path, name, &table.relation_kind)?,
partition_of: table.partition_of.clone(),
omitted_partition_leaves: table.omitted_partition_leaves,
inherited_from: table.inherited_from.clone(),
comment: table.comment.clone(),
columns: columns::columns(path, name, &table.columns)?,
primary_key: table.primary_key.as_ref().map(|key| key.columns.clone()),
foreign_keys: objects::foreign_keys(table),
check_constraints: objects::checks(table),
unique_constraints: objects::uniques(table),
indexes: objects::indexes(&table.indexes),
triggers: objects::triggers(path, name, table, identities)?,
partition_key: objects::partition_key(path, name, table)?,
})
}