athena_rs 3.26.4

Hyper performant polyglot Database driver
Documentation
//! Observed-table map builder for `/debug/schema`.
//!
//! This module maps raw relation/column rows into a stable, relation-keyed
//! representation used by logging-schema report composition. Relation-row
//! seeding is delegated to `debug_observed_relations` and column attachment is
//! delegated to `debug_observed_columns`. Typed builder-input contracts are
//! provided by `debug_observed_table_builder_input_contracts` and typed
//! builder-input payload assembly is provided by
//! `debug_observed_table_builder_input_assembly`.

use super::debug_observed_columns::apply_observed_columns;
use super::debug_observed_relations::seed_observed_tables_from_relations;
use super::debug_observed_table_builder_input_contracts::LoggingSchemaObservedTablesBuilderInput;
use super::debug_observed_table_contracts::ObservedTablesMap;

/// Builds a relation-keyed map of observed logging tables and discovered columns.
pub(super) fn build_observed_tables_map(
    input: LoggingSchemaObservedTablesBuilderInput,
) -> ObservedTablesMap {
    let LoggingSchemaObservedTablesBuilderInput { relations, columns } = input;
    let mut observed_map = seed_observed_tables_from_relations(relations);

    apply_observed_columns(&mut observed_map, columns);

    observed_map
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::api::schema::debug_observed_table_builder_input_assembly::build_observed_table_builder_input_assembly;
    use crate::api::schema::service::{SchemaColumnRecord, SchemaRelationRecord};

    /// Builds a minimal column record for observed-table mapping tests.
    fn column(table_schema: &str, table_name: &str, column_name: &str) -> SchemaColumnRecord {
        SchemaColumnRecord {
            table_schema: table_schema.to_string(),
            table_name: table_name.to_string(),
            column_name: column_name.to_string(),
            data_type: None,
            column_default: None,
            is_nullable: Some("YES".to_string()),
        }
    }

    #[test]
    /// Normalizes relation type casing and deduplicates repeated column rows.
    fn observed_map_normalizes_relation_type_and_deduplicates_columns() {
        let relations = vec![SchemaRelationRecord {
            table_schema: "public".to_string(),
            table_name: "gateway_request_log".to_string(),
            relation_type: "base table".to_string(),
        }];
        let columns = vec![
            column("public", "gateway_request_log", "request_id"),
            column("public", "gateway_request_log", "request_id"),
            column("public", "gateway_request_log", "status_code"),
        ];

        let input = build_observed_table_builder_input_assembly(relations, columns);
        let map = build_observed_tables_map(input);
        let table = map
            .get("public.gateway_request_log")
            .expect("table present");

        assert_eq!(table.relation_type, "BASE TABLE");
        assert_eq!(
            table.columns,
            vec!["request_id".to_string(), "status_code".to_string()]
        );
    }
}