Skip to main content

fraiseql_cli/commands/
compile.rs

1//! Schema compilation command
2//!
3//! Compiles schema.json (from Python/TypeScript/etc.) into optimized schema.compiled.json
4
5use std::{fs, path::Path, process::Command};
6
7use anyhow::{Context, Result};
8use fraiseql_core::schema::{
9    CompiledSchema, FieldType, InputStyle, MutationOperation, NamingConvention, content_hash_of,
10};
11use fraiseql_db::postgres::PostgresTlsConfig;
12use tracing::{info, warn};
13
14use crate::{
15    config::{ConfigSource, TomlProjectConfig},
16    schema::{
17        CompiledArtifact, ConvertOptions, IntermediateSchema, OptimizationReport, SchemaConverter,
18        SchemaOptimizer, SchemaValidator,
19        database_validator::validate_schema_against_database,
20        mutation_contract::{Severity, validate_mutation_contract},
21        pg_catalog::PgCatalog,
22    },
23};
24
25/// Input source configuration for schema compilation.
26#[derive(Debug, Default)]
27pub struct CompileOptions<'a> {
28    /// Path to `fraiseql.toml` (TOML workflow) or `schema.json` (legacy).
29    pub input:          &'a str,
30    /// Optional path to `types.json` (TOML workflow, backward compat).
31    pub types:          Option<&'a str>,
32    /// Optional directory for schema file auto-discovery.
33    pub schema_dir:     Option<&'a str>,
34    /// Explicit type file paths (highest priority).
35    pub type_files:     Vec<String>,
36    /// Explicit query file paths.
37    pub query_files:    Vec<String>,
38    /// Explicit mutation file paths.
39    pub mutation_files: Vec<String>,
40    /// Optional database URL for indexed column validation.
41    pub database:       Option<&'a str>,
42    /// Skip embedding content hash in compiled schema (for test fixtures).
43    pub skip_hash:      bool,
44    /// Downgrade error-severity schema↔database drift findings to advisories
45    /// (#384). Without this, error-severity drift fails the compile.
46    pub allow_drift:    bool,
47    /// The project config to compile with. `None` resolves it from `input` with
48    /// [`ConfigSource::resolve`], exactly as `compile` and `run` do — never from the
49    /// working directory (#1387).
50    pub config:         Option<ConfigSource>,
51}
52
53impl<'a> CompileOptions<'a> {
54    /// Create new compile options with just the input path.
55    #[must_use]
56    pub fn new(input: &'a str) -> Self {
57        Self {
58            input,
59            ..Default::default()
60        }
61    }
62
63    /// Set the types path.
64    #[must_use]
65    pub fn with_types(mut self, types: &'a str) -> Self {
66        self.types = Some(types);
67        self
68    }
69
70    /// Set the schema directory for auto-discovery.
71    #[must_use]
72    pub fn with_schema_dir(mut self, schema_dir: &'a str) -> Self {
73        self.schema_dir = Some(schema_dir);
74        self
75    }
76
77    /// Set the database URL for validation.
78    #[must_use]
79    pub fn with_database(mut self, database: &'a str) -> Self {
80        self.database = Some(database);
81        self
82    }
83}
84
85/// Select and execute the appropriate schema-loading strategy for TOML-based workflows.
86///
87/// Tries strategies in priority order:
88/// 1. Explicit file lists (highest priority)
89/// 2. Directory auto-discovery
90/// 3. Single types file (backward-compatible)
91/// 4. Domain discovery → TOML includes → TOML-only (fallback sequence)
92///
93/// # Errors
94///
95/// Returns an error if a **configured** schema source fails to load. Distinguishing
96/// "not configured" (fall through to the next strategy) from "configured but failed"
97/// (propagate) is the substance of #723 — see the fallback sequence below.
98#[allow(clippy::cognitive_complexity)] // Reason: multi-strategy schema discovery with fallback chain
99pub fn load_intermediate_schema(
100    toml_path: &str,
101    type_files: &[String],
102    query_files: &[String],
103    mutation_files: &[String],
104    schema_dir: Option<&str>,
105    types_path: Option<&str>,
106) -> Result<IntermediateSchema> {
107    if !type_files.is_empty() || !query_files.is_empty() || !mutation_files.is_empty() {
108        info!("Mode: Explicit file lists");
109        return crate::schema::SchemaMerger::merge_explicit_files(
110            toml_path,
111            type_files,
112            query_files,
113            mutation_files,
114        )
115        .context("Failed to load explicit schema files");
116    }
117    if let Some(dir) = schema_dir {
118        info!("Mode: Auto-discovery from directory: {}", dir);
119        return crate::schema::SchemaMerger::merge_from_directory(toml_path, dir)
120            .context("Failed to load schema from directory");
121    }
122    if let Some(types) = types_path {
123        info!("Mode: Language + TOML (types.json + fraiseql.toml)");
124        return crate::schema::SchemaMerger::merge_files(types, toml_path)
125            .context("Failed to merge types.json with TOML");
126    }
127    // Fallback sequence: domain discovery → TOML includes → TOML-only.
128    //
129    // Each step asks "is this configured?" **before** attempting it, so a failure inside a
130    // configured source propagates instead of being swallowed by the next fallback (#723).
131    //
132    // This used to be `if let Ok(schema) = merge_from_domains(toml_path)`, twice. A bad
133    // `root_dir`, an unreadable domain file or a JSON parse error was discarded and
134    // compilation fell through to TOML-only definitions — producing either a schema silently
135    // missing the user's domain types, or a later death with the misleading "Failed to load
136    // schema from TOML" naming the wrong thing entirely. That directly contradicts the #612
137    // doctrine this codebase otherwise holds to: configured input that fails must fail loud.
138    let toml_schema = crate::config::TomlSchema::from_file(toml_path)
139        .context(format!("Failed to load TOML from {toml_path}"))?;
140
141    if toml_schema.domain_discovery.enabled {
142        info!("Mode: TOML-based with domain discovery");
143        return crate::schema::SchemaMerger::merge_from_domains(toml_path).context(
144            "Failed to load schema from the configured [domain_discovery] section. Fix the \
145             error above, or remove the section to compile from TOML definitions only.",
146        );
147    }
148
149    if !toml_schema.includes.is_empty() {
150        info!("Mode: TOML-based with schema includes");
151        return crate::schema::SchemaMerger::merge_with_includes(toml_path).context(
152            "Failed to load schema from the configured [includes] section. Fix the error \
153             above, or remove the section to compile from TOML definitions only.",
154        );
155    }
156
157    info!("No domains or includes configured, using TOML-only definitions");
158    crate::schema::SchemaMerger::merge_toml_only(toml_path)
159        .context("Failed to load schema from TOML")
160}
161
162/// Compile a schema to the whole `schema.compiled.json` artifact, without writing it.
163///
164/// This is the core compilation logic, shared between `compile` (which writes to disk)
165/// and `run` (which serves in-memory without any file artifacts).
166///
167/// # Arguments
168///
169/// * `opts` - Compilation options including input paths and configuration
170///
171/// # Errors
172///
173/// Returns error if input is missing, parsing fails, validation fails, or the database
174/// connection fails (when `database` is provided).
175#[allow(clippy::cognitive_complexity)] // Reason: end-to-end compilation pipeline with validation, introspection, and output stages
176pub async fn compile_to_schema(
177    opts: CompileOptions<'_>,
178) -> Result<(CompiledArtifact, OptimizationReport)> {
179    info!("Compiling schema: {}", opts.input);
180
181    // A removed engine's URL is refused before any work, naming the PostgreSQL-only rule
182    // (#1341), rather than failing inside the connection pool at step 5b.
183    if let Some(db_url) = opts.database {
184        crate::connection::require_postgres(db_url)?;
185    }
186
187    // 1. Determine workflow based on input file and options
188    let input_path = Path::new(opts.input);
189    if !input_path.exists() {
190        anyhow::bail!("Input file not found: {}", opts.input);
191    }
192
193    // Load schema based on file type and options
194    let is_toml = input_path
195        .extension()
196        .and_then(|ext| ext.to_str())
197        .is_some_and(|ext| ext.eq_ignore_ascii_case("toml"));
198    let mut intermediate: IntermediateSchema = if is_toml {
199        info!("Using TOML-based workflow");
200        load_intermediate_schema(
201            opts.input,
202            &opts.type_files,
203            &opts.query_files,
204            &opts.mutation_files,
205            opts.schema_dir,
206            opts.types,
207        )?
208    } else {
209        // Legacy JSON workflow
210        info!("Using legacy JSON workflow");
211        let schema_json = fs::read_to_string(input_path).context("Failed to read schema.json")?;
212
213        // 2. Parse JSON into IntermediateSchema (language-agnostic format). Parse via Value first
214        //    so we can detect a federation block that the SDK emitted but that failed to bind into
215        //    the schema (the silent-drop class this issue fixed): the block must carry through or
216        //    fail loudly, never vanish into a non-federated subgraph.
217        info!("Parsing intermediate schema...");
218        let raw: serde_json::Value =
219            serde_json::from_str(&schema_json).context("Failed to parse schema.json")?;
220
221        // Refuse a security control declared under a key that does not bind (#806/#807).
222        // Must run on the raw JSON: after deserialization the evidence is gone, which is
223        // precisely the defect — an unread key becomes an empty default and nothing
224        // downstream can distinguish "no scope declared" from "scope declared and lost".
225        crate::schema::intermediate::reject_drifted_keys(&raw)
226            .map_err(|e| anyhow::anyhow!("{e}"))?;
227
228        let input_has_federation = ["federation", "federation_config"].iter().any(|key| {
229            raw.get(*key)
230                .and_then(serde_json::Value::as_object)
231                .is_some_and(|o| !o.is_empty())
232        });
233        let intermediate: IntermediateSchema =
234            serde_json::from_value(raw).context("Failed to parse schema.json")?;
235        if input_has_federation && intermediate.federation_config.is_none() {
236            anyhow::bail!(
237                "schema.json carries a `federation` block that did not bind into the compiled \
238                 schema — refusing to compile a silently non-federated subgraph. This is a \
239                 compiler bug; please report it."
240            );
241        }
242        intermediate
243    };
244
245    // 2a. Load and apply the project config that belongs to this input (#1387).
246    // A TOML input carries its settings itself (`ConfigSource::Input` names no
247    // separate file): it is a TomlSchema, a different format from TomlProjectConfig.
248    let config_source = match opts.config {
249        Some(source) => source,
250        None => ConfigSource::resolve(input_path, None)?,
251    };
252    info!("Project config: {config_source}");
253    // Opt-in mutation-error-union synthesis, read from [fraiseql.mutations] below.
254    let mut auto_error_union = false;
255    // Casing acronyms from [fraiseql.naming], added on top of the built-in defaults.
256    let mut naming_acronyms: Vec<String> = Vec::new();
257    // Per-operation @cost weight overrides from [fraiseql.cost_weights] (#379).
258    let mut operation_cost_weights: std::collections::HashMap<String, usize> =
259        std::collections::HashMap::new();
260    // GraphQL surface convention for the legacy JSON (Workflow-B) path. Defaults
261    // to camelCase (snake_case DB, camelCase client surface + input recasing);
262    // [fraiseql.naming] convention = "preserve" restores the as-authored names.
263    // The TomlSchema path carries its own naming_convention via the merger and is
264    // left untouched (see the `if !is_toml` apply below).
265    let mut naming_convention = NamingConvention::CamelCase;
266    // Transport security for the `--database` checks (#1429): `[database] ssl_mode` from
267    // the config this compile loaded. Unset leaves the URL's own `?sslmode=` in charge.
268    let mut database_tls = if is_toml && opts.database.is_some() {
269        crate::config::TomlSchema::from_file(opts.input)
270            .with_context(|| format!("Failed to load TOML from {}", opts.input))?
271            .database
272            .postgres_tls()?
273    } else {
274        PostgresTlsConfig::default()
275    };
276    if let Some(config_path) = config_source.project_config() {
277        info!("Loading security configuration from {}...", config_path.display());
278        let config_file = config_path.to_str().ok_or_else(|| {
279            anyhow::anyhow!("Config path is not valid UTF-8: {}", config_path.display())
280        })?;
281        match TomlProjectConfig::from_file(config_file) {
282            Ok(config) => {
283                info!("Validating security configuration...");
284                config.validate()?;
285
286                auto_error_union = config.fraiseql.mutations.auto_error_union;
287                naming_acronyms.clone_from(&config.fraiseql.naming.acronyms);
288                operation_cost_weights.clone_from(&config.fraiseql.cost_weights);
289                naming_convention = config.fraiseql.naming.convention;
290                database_tls = config.database.postgres_tls()?;
291
292                info!("Applying security configuration to schema...");
293                // Merge security config into intermediate schema
294                let mut security_json = config.fraiseql.security.to_json();
295
296                // Embed the tenancy configuration whatever its mode: `tenant_claim` names the
297                // request's tenant on every path since #1388, not only under row or schema
298                // isolation, and dropping it with `mode = "none"` reverted the runtime to
299                // `tenant_id` without a word (#1542).
300                security_json["tenancy"] = config.fraiseql.tenancy.to_json();
301
302                intermediate.security = Some(security_json);
303
304                // Session variables are the mechanism RLS policies read; carry them
305                // through rather than leaving `schema.json` as the only way to
306                // declare them (#628).
307                if config.fraiseql.session_variables
308                    != fraiseql_core::schema::SessionVariablesConfig::default()
309                {
310                    intermediate.session_variables = Some(config.fraiseql.session_variables);
311                }
312
313                // `[locale]` (#1512) is configuration, so it lives only in `fraiseql.toml`; a
314                // schema document carrying a different one is an authoring conflict.
315                if let Some(locale) = config.locale {
316                    if intermediate.locale.as_ref().is_some_and(|l| *l != locale) {
317                        anyhow::bail!(
318                            "[locale] in {} disagrees with the `locale` the schema document \
319                             declares; declare it once, in fraiseql.toml",
320                            config_path.display()
321                        );
322                    }
323                    intermediate.locale = Some(locale);
324                }
325
326                // `[inject_defaults]` lives in the config the SDK loaders read (#1384); a
327                // copy the SDK emitted into the schema must agree with it.
328                intermediate.inject_defaults =
329                    crate::config::inject_defaults::InjectDefaultsToml::reconcile(
330                        config.inject_defaults.as_ref(),
331                        intermediate.inject_defaults.take(),
332                    )?;
333
334                info!("Security configuration applied successfully");
335            },
336            Err(e) => {
337                anyhow::bail!(
338                    "Failed to parse {}: {e}\n\
339                     Fix the configuration file or remove it to use defaults.",
340                    config_path.display()
341                );
342            },
343        }
344    }
345
346    // Install the project's casing acronyms so compile-time key inference
347    // (native-column resolution, DDL) agrees with the runtime. Defaults apply
348    // when none are configured.
349    fraiseql_core::utils::casing::set_runtime_acronyms(&naming_acronyms);
350
351    // Apply the naming convention to the legacy JSON (Workflow-B) path. The
352    // JSON schema never carries one, so without this it would fall to the enum
353    // default (Preserve); Workflow-B instead defaults to CamelCase (overridable
354    // via [fraiseql.naming].convention). The TomlSchema path already carries its
355    // own convention from the merger, so it is left untouched.
356    if !is_toml {
357        intermediate.naming_convention = naming_convention;
358    }
359
360    // 2b. Validate @tenant_id annotations when tenancy mode is "row".
361    // Extract tenancy config from the already-embedded security JSON.
362    let tenancy_row_claim: Option<String> = intermediate.security.as_ref().and_then(|sec| {
363        let tenancy = sec.get("tenancy")?;
364        let mode = tenancy.get("mode").and_then(|m| m.as_str()).unwrap_or("none");
365        if mode == "row" {
366            // `tenant_claim`, matching `fraiseql_core::schema::TenancyConfig`. This
367            // read `tenantClaim` while the runtime read `tenant_claim`, so the
368            // compiler validated `@tenant_id` against one claim and the server
369            // injected another (#757).
370            Some(
371                tenancy
372                    .get("tenant_claim")
373                    .and_then(|c| c.as_str())
374                    .unwrap_or("tenant_id")
375                    .to_string(),
376            )
377        } else {
378            None
379        }
380    });
381    if let Some(tenant_claim) = &tenancy_row_claim {
382        info!("Validating @tenant_id annotations for row-isolation tenancy...");
383        crate::schema::converter::tenancy::validate_tenant_annotations(
384            &mut intermediate,
385            tenant_claim,
386        )
387        .context("@tenant_id validation failed")?;
388    }
389
390    // 3. Validate intermediate schema
391    info!("Validating schema structure...");
392    let validation_report =
393        SchemaValidator::validate(&intermediate).context("Failed to validate schema")?;
394
395    if !validation_report.is_valid() {
396        validation_report.print();
397        // The errors themselves, not only their count: a caller of `compile_to_schema` that
398        // is not a terminal (an embedder, `fraiseql run`) has nothing else to read (#1530).
399        let errors: Vec<String> = validation_report
400            .errors
401            .iter()
402            .filter(|e| e.severity == crate::schema::validator::ErrorSeverity::Error)
403            .map(|e| format!("{} ({})", e.message, e.path))
404            .collect();
405        anyhow::bail!(
406            "Schema validation failed with {} error(s):\n  - {}",
407            validation_report.error_count(),
408            errors.join("\n  - ")
409        );
410    }
411
412    // Print warnings if any
413    if validation_report.warning_count() > 0 {
414        validation_report.print();
415    }
416
417    // 4. Convert to the compiled artifact (validates and normalizes). `functions` is
418    // a sibling section of the compiled schema, not a field of it, so the converter
419    // hands both halves back and the rest of this pipeline sharpens the schema half.
420    // Which parameters each mutation receives from `[inject_defaults]` rather than declares
421    // (#1385), so a contract error a default caused can name the default instead of
422    // repeating once per mutation. Computed by the converter's own rule; a refusal that
423    // rule raises is the converter's to report.
424    let mutation_defaults =
425        intermediate.inject_defaults.clone().unwrap_or_default().for_mutations();
426    let default_args: std::collections::HashMap<String, Vec<String>> = intermediate
427        .mutations
428        .iter()
429        .filter_map(|m| {
430            let mut inject = m.inject.clone();
431            crate::schema::intermediate::IntermediateInjectDefaults::apply_to(
432                &mutation_defaults,
433                &mut inject,
434                &m.exclude_inject_defaults,
435                &m.name,
436            )
437            .ok()
438            .filter(|added| !added.is_empty())
439            .map(|added| (m.name.clone(), added))
440        })
441        .collect();
442
443    info!("Converting to compiled format...");
444    let CompiledArtifact {
445        mut schema,
446        functions,
447    } = SchemaConverter::convert_artifact(intermediate, &ConvertOptions { auto_error_union })
448        .context("Failed to convert schema to compiled format")?;
449
450    // Carry the project's casing acronyms into the compiled schema so the runtime
451    // installs them at boot (see `fraiseql_db::utils::set_runtime_acronyms`).
452    schema.naming_acronyms = naming_acronyms;
453
454    // Carry the project's @cost weight overrides (#379) into the compiled schema so
455    // the runtime per-tenant cost-budget check can apply them. Non-clobbering: only
456    // overwrite when configured, leaving any value a future merger path may set.
457    if !operation_cost_weights.is_empty() {
458        schema.operation_cost_weights = operation_cost_weights;
459    }
460
461    // 5. Optimize schema and generate SQL hints (mutates schema in place, report for display)
462    info!("Analyzing schema for optimization opportunities...");
463    let report = SchemaOptimizer::optimize(&mut schema).context("Failed to optimize schema")?;
464
465    // 5b-pre. Infer native_columns for ID/UUID-typed arguments on JSONB-backed queries.
466    // DB introspection (step 5b) overrides these inferred values when `--database` is provided.
467    infer_native_columns_from_arg_types(&mut schema);
468
469    // 5b. Optional: Validate native columns against database.
470    if let Some(db_url) = opts.database {
471        info!("Validating native columns for direct query arguments...");
472        // One pool, one connection decision, for every check below (#1429).
473        let pool = crate::connection::postgres_pool(db_url, "database validation", &database_tls)
474            .await
475            .context("Failed to connect for database validation")?;
476        let pg_introspector = fraiseql_core::db::postgres::PostgresIntrospector::new(pool.clone());
477        let db_report = validate_schema_against_database(&schema, &pg_introspector).await?;
478
479        // Error-severity drift fails the compile (#384) — collected here, raised
480        // after every check has reported, so one run surfaces the full list.
481        let mut drift_errors: Vec<String> = Vec::new();
482        for w in &db_report.warnings {
483            match w.severity() {
484                Severity::Error => {
485                    warn!("{w} [drift error]");
486                    drift_errors.push(w.to_string());
487                },
488                Severity::Warn => warn!("{w}"),
489            }
490        }
491        apply_database_report(&mut schema, &db_report);
492
493        // Mutation call/response contract (#384 item 3: inject_params resolve to real
494        // function arguments). Every URL that reached the drift check is PostgreSQL —
495        // a removed engine's was refused up front — so this runs for every form of it,
496        // libpq `key=value` included (#1403).
497        info!("Validating mutation contract against the database...");
498        let catalog = PgCatalog::from_pool(pool);
499
500        // #1587: the SQL columns each relationship joins on, where pg_tviews' registry
501        // proves them, so a composed read correlates on an indexed column.
502        let sources: Vec<String> = schema
503            .types
504            .iter()
505            .map(|t| t.sql_source.to_string())
506            .filter(|s| !s.is_empty())
507            .collect();
508        // An optimization: a registry this role cannot read leaves the stored keys, and says so.
509        match catalog.tview_references(&sources).await {
510            Ok(references) => {
511                let proven =
512                    crate::schema::native_joins::apply_native_joins(&mut schema, &references);
513                if proven > 0 {
514                    info!(
515                        "{proven} relationship(s) correlate on native columns (pg_tviews references)"
516                    );
517                }
518            },
519            Err(error) => warn!(
520                "cannot read tviews.registry.references ({error:#}); composed reads correlate on \
521                 document keys"
522            ),
523        }
524
525        let contract = validate_mutation_contract(&schema, &catalog).await?;
526        let mut caused_by_default: std::collections::BTreeMap<String, Vec<String>> =
527            std::collections::BTreeMap::new();
528        for m in &contract.mutations {
529            let added = default_args.get(&m.mutation).map_or(&[][..], Vec::as_slice);
530            for v in &m.violations {
531                let kind = match v.severity() {
532                    Severity::Error => "contract error",
533                    Severity::Warn => "contract warning",
534                };
535                warn!("mutation `{}` (sql_source: {}): {v} [{kind}]", m.mutation, m.sql_source);
536                if v.severity() != Severity::Error {
537                    continue;
538                }
539                if let Some(key) = default_caused(v, added) {
540                    caused_by_default.entry(key).or_default().push(m.mutation.clone());
541                } else {
542                    drift_errors.push(format!(
543                        "mutation `{}` (sql_source: {}): {v}",
544                        m.mutation, m.sql_source
545                    ));
546                }
547            }
548        }
549        for (key, mutations) in caused_by_default {
550            drift_errors.push(default_collision(&key, &mutations));
551        }
552
553        // #1391: a pg_tviews cascade source calls pg_tviews before every commit; against a
554        // database that has none, each such mutation's first write would fail.
555        let tviews_sourced: Vec<&str> = schema
556            .mutations
557            .iter()
558            .filter(|m| m.cascade_source == fraiseql_core::schema::CascadeSource::PgTviews)
559            .map(|m| m.name.as_str())
560            .collect();
561        if !tviews_sourced.is_empty() && !catalog.pg_tviews_flush_available().await? {
562            for name in tviews_sourced {
563                drift_errors.push(format!(
564                    "mutation `{name}`: cascade_source = \"pg_tviews\", and the database has no \
565                     tviews.pg_tviews_flush_and_report(integer, boolean, boolean): install \
566                     pg_tviews (preloaded), or drop cascade_source"
567                ));
568            }
569        }
570
571        // The linter must be able to FAIL (#384): a schema that names database
572        // objects which do not exist — or cannot serve the declared shape —
573        // does not compile. `--allow-drift` restores the advisory behaviour;
574        // the findings above have already been reported either way.
575        if !drift_errors.is_empty() {
576            if opts.allow_drift {
577                warn!(
578                    "{} schema↔database drift error(s) suppressed by --allow-drift",
579                    drift_errors.len()
580                );
581            } else {
582                let list =
583                    drift_errors.iter().map(|e| format!("  - {e}")).collect::<Vec<_>>().join("\n");
584                anyhow::bail!(
585                    "schema↔database drift: {} error(s):\n{list}\n\
586                     Fix the declarations or the database objects, or pass --allow-drift \
587                     to compile anyway (advisory mode).",
588                    drift_errors.len()
589                );
590            }
591        }
592    } else {
593        // Warn for queries that still have unresolved direct arguments after inference.
594        // Arguments already covered by native_columns inference are not warned about.
595        for query in &schema.queries {
596            if query.sql_source.is_none() {
597                continue;
598            }
599            let unresolved: Vec<_> = query
600                .arguments
601                .iter()
602                .filter(|a| !NATIVE_COLUMN_SKIP_ARGS.contains(&a.name.as_str()))
603                .filter(|a| !query.native_columns.contains_key(&a.name))
604                .collect();
605            if !unresolved.is_empty() {
606                let names: Vec<_> = unresolved.iter().map(|a| a.name.as_str()).collect();
607                warn!(
608                    "query `{}`: argument(s) {:?} on `{}` could not be resolved to native \
609                     columns — no --database URL provided. These filters will use JSONB \
610                     extraction. Provide --database or annotate with native_columns.",
611                    query.name,
612                    names,
613                    query.sql_source.as_deref().unwrap_or("?"),
614                );
615            }
616        }
617    }
618
619    // 5d. Warn when mutations have wide invalidation fan-out (HOT update pressure).
620    warn_wide_cascade_mutations(&schema);
621
622    // 5e. Warn when a `preserve` schema would silently forward camelCase input keys
623    // to snake_case SQL functions on the single-JSONB mutation path (#456).
624    warn_jsonb_preserve_mismatch(&schema);
625
626    // 5f. Refuse a schema in which a mutation could invalidate nothing (#910).
627    refuse_unattributable_mutations(&schema)?;
628
629    // 5g. Refuse authorization declarations no enforcer reads (#983).
630    refuse_unenforced_authz_declarations(&schema)?;
631
632    // 5h. Refuse a schema a server would refuse to load (ruling AF 4).
633    refuse_what_a_server_would_not_load(&schema)?;
634
635    Ok((CompiledArtifact { schema, functions }, report))
636}
637
638/// Refuse a compiled schema that a server would refuse to load (ruling AF 4).
639///
640/// The load-time checks (`CompiledSchema::finish_load`: duplicate names, type roles and
641/// injects, subscription policies and filters, `requires_scope` without a `security`
642/// section, fact-table links, relationships) hold for every producer, so they live at load
643/// — and until now they ran only when a server started, on an artifact the compiler had
644/// already written. The schema is serialized as the writer serializes it, stamped with its
645/// content hash, and loaded exactly as a server loads it; a refusal fails the compile with
646/// the loader's own message.
647///
648/// # Errors
649///
650/// Returns the loader's refusal.
651/// The `[inject_defaults]` key a contract error is due to, if it is due to one (#1385).
652///
653/// Attributed only when the arithmetic says so: the function takes exactly the arguments
654/// the mutation would send without its default-supplied parameters, or the parameter the
655/// call binds at a mismatching position is one a default supplied. Anything else is the
656/// function's own error and is reported as such.
657pub(crate) fn default_caused(
658    violation: &crate::schema::mutation_contract::ContractViolation,
659    added: &[String],
660) -> Option<String> {
661    use crate::schema::mutation_contract::ContractViolation;
662    match violation {
663        ContractViolation::ArityMismatch { expected, found }
664            if !added.is_empty() && found.contains(&expected.saturating_sub(added.len())) =>
665        {
666            Some(added.join(", "))
667        },
668        ContractViolation::InjectNameMismatch { expected, .. } if added.contains(expected) => {
669            Some(expected.clone())
670        },
671        _ => None,
672    }
673}
674
675/// One error for every mutation a default does not fit, instead of one per mutation.
676pub(crate) fn default_collision(key: &str, mutations: &[String]) -> String {
677    const SHOWN: usize = 5;
678    let shown = mutations.iter().take(SHOWN).map(|m| format!("`{m}`")).collect::<Vec<_>>();
679    let more = mutations.len().saturating_sub(SHOWN);
680    let list = if more == 0 {
681        shown.join(", ")
682    } else {
683        format!("{}, and {more} more", shown.join(", "))
684    };
685    format!(
686        "[inject_defaults] adds `{key}` to {} mutation(s) whose functions do not take it: \
687         {list}. Exclude it on those mutations (`exclude_inject_defaults = [\"{key}\"]`), or \
688         move it from the base [inject_defaults] table to [inject_defaults.queries].",
689        mutations.len()
690    )
691}
692
693fn refuse_what_a_server_would_not_load(schema: &CompiledSchema) -> Result<()> {
694    let body = serde_json::to_string(schema).context("Failed to serialize compiled schema")?;
695    let mut value: serde_json::Value = serde_json::from_str(&body)?;
696    let hash = content_hash_of(&value);
697    value
698        .as_object_mut()
699        .context("schema must serialise as JSON object")?
700        .insert("_content_hash".to_string(), serde_json::Value::String(hash));
701    CompiledSchema::from_json(&serde_json::to_string(&value)?, true).map_err(|e| {
702        anyhow::anyhow!("the compiled schema would be refused when a server loads it: {e}")
703    })?;
704    Ok(())
705}
706
707/// Refuse `security.rules`, `security.field_auth` and `security.default_policy` (#983).
708///
709/// All three are carried by the compiled seam and read by **nothing**: field-level
710/// RBAC runs on `role_definitions` + scopes, and `#677` lowered type gates onto
711/// operations. An operator who writes `[[security.rules]]` in a hand-authored
712/// `schema.json` gets a successful compile and zero enforcement — an authorization
713/// rule that is silently not applied, which is the worst shape a security
714/// declaration can take.
715///
716/// Refused rather than wired: an enforcer for them is `#626`'s design, and wiring
717/// three keys ad hoc to make the compile stop lying would prejudge it. This is the
718/// same disposition `#612` gave `security.policies`, which is why that one is
719/// already unproducible.
720///
721/// # Errors
722///
723/// Returns an error naming each declaration present.
724fn refuse_unenforced_authz_declarations(schema: &CompiledSchema) -> Result<()> {
725    let Some(security) = schema.security.as_ref() else {
726        return Ok(());
727    };
728
729    let mut declared: Vec<String> = Vec::new();
730    if !security.rules.is_empty() {
731        declared.push(format!("`security.rules` ({} rule(s))", security.rules.len()));
732    }
733    if !security.field_auth.is_empty() {
734        declared.push(format!("`security.field_auth` ({} rule(s))", security.field_auth.len()));
735    }
736    if let Some(policy) = security.default_policy.as_deref() {
737        declared.push(format!("`security.default_policy` (\"{policy}\")"));
738    }
739    if declared.is_empty() {
740        return Ok(());
741    }
742
743    anyhow::bail!(
744        "authorization declaration(s) with no enforcer: {}.\n\n\
745         FraiseQL enforces field-level authorization through `security.role_definitions` \
746         (roles → scopes) and each field's `requires_scope`; nothing reads these keys, so a \
747         rule written here is not applied to any request. Remove them, or express the same \
748         intent with role definitions and `requires_scope`.\n\n\
749         (A general authorization-rule engine is tracked separately; until it exists, a \
750         declaration that silently enforces nothing is refused rather than accepted.)",
751        declared.join(", ")
752    )
753}
754
755/// Refuse a schema whose caching depends on a mutation the engine cannot attribute (#910).
756///
757/// A mutation's invalidation is resolved from `invalidates_views`, the return type's
758/// view, the entity a payload type wraps, the `entity_type` its function stamps on
759/// `mutation_response`, and its cascade envelope. The first three are knowable here;
760/// the last two are not. When the knowable ones resolve to nothing, the mutation
761/// *may* invalidate nothing at runtime — and for a view annotated
762/// `cache_ttl_seconds = 0`, documented as "mutation-invalidated only", nothing means
763/// **forever**: the write lands, every cached entry stays warm for the process
764/// lifetime, and no log line says so.
765///
766/// The compiler knows every mutation and every `cache_ttl_seconds`, so it can make
767/// the shape impossible instead of loud. A `tracing::warn!` beside a successful
768/// compile is the same defect with more text; a boot-time refusal is later and
769/// per-deployment. Declaring `invalidates_views` costs one line and is checkable.
770///
771/// Schemas that annotate no view as cacheable are unaffected — there is no entry
772/// to strand.
773///
774/// # Errors
775///
776/// Returns an error naming every unattributable mutation when the schema also
777/// declares at least one cacheable view.
778fn refuse_unattributable_mutations(schema: &CompiledSchema) -> Result<()> {
779    if !fraiseql_core::cache::declares_cacheable_views(schema) {
780        return Ok(());
781    }
782    let unattributable = fraiseql_core::cache::unattributable_mutations(schema);
783    if unattributable.is_empty() {
784        return Ok(());
785    }
786
787    let cacheable: Vec<&str> = schema
788        .queries
789        .iter()
790        .filter(|q| q.cache_ttl_seconds.is_some())
791        .filter_map(|q| q.sql_source.as_deref())
792        .collect();
793    let list = unattributable.iter().map(|m| format!("  - {m}")).collect::<Vec<_>>().join("\n");
794    anyhow::bail!(
795        "mutation(s) whose cache invalidation cannot be resolved from the schema:\n{list}\n\n\
796         This schema annotates {} cacheable view(s) ({}), and a mutation that resolves to no \
797         view invalidates nothing when it succeeds — permanently, for any view annotated \
798         `cache_ttl_seconds = 0`.\n\n\
799         Fix by declaring what each mutation writes:\n\
800         \x20   invalidates_views = [\"v_price\"]\n\n\
801         A mutation whose return type is backed by a view, or whose payload wraps an entity \
802         that is, already resolves and needs no annotation.",
803        cacheable.len(),
804        cacheable.join(", "),
805    )
806}
807
808/// Run the compile command
809///
810/// # Arguments
811///
812/// * `input` - Path to fraiseql.toml (TOML) or schema.json (legacy)
813/// * `types` - Optional path to types.json (when using TOML workflow)
814/// * `schema_dir` - Optional directory for auto-discovery of schema files
815/// * `type_files` - Optional vector of explicit type file paths
816/// * `query_files` - Optional vector of explicit query file paths
817/// * `mutation_files` - Optional vector of explicit mutation file paths
818/// * `output` - Path to write schema.compiled.json
819/// * `check` - If true, validate only without writing output
820/// * `database` - Optional database URL for indexed column validation
821/// * `emit_ddl` - Optional directory to write `CREATE TABLE` DDL files (confiture format)
822/// * `check_migrations` - If true, run `confiture migrate validate` after compilation
823/// * `skip_hash` - Skip embedding content hash (for test fixtures)
824/// * `allow_drift` - Downgrade error-severity schema↔database drift to advisories (#384)
825///
826/// # Errors
827///
828/// Returns error if:
829/// - Input file doesn't exist or can't be read
830/// - JSON/TOML parsing fails
831/// - Schema validation fails
832/// - Output file can't be written
833/// - Database connection fails (when database URL is provided)
834/// - DDL output directory cannot be created (when `emit_ddl` is provided)
835/// - `confiture` is not installed (when `check_migrations` is true)
836/// - Migration drift detected (when `check_migrations` is true)
837#[allow(clippy::too_many_arguments, clippy::fn_params_excessive_bools)]
838// Reason: run() is the CLI entry point that receives individual args from clap; keeping them
839// separate for clarity — each bool is a distinct clap flag
840#[doc(hidden)] // Internal-pub: CLI entry point dispatched by runner.rs; not a stable downstream API.
841pub async fn run(
842    input: &str,
843    types: Option<&str>,
844    schema_dir: Option<&str>,
845    type_files: Vec<String>,
846    query_files: Vec<String>,
847    mutation_files: Vec<String>,
848    output: &str,
849    check: bool,
850    database: Option<&str>,
851    emit_ddl: Option<&str>,
852    check_migrations: bool,
853    skip_hash: bool,
854    allow_drift: bool,
855    config: Option<&str>,
856) -> Result<()> {
857    // Defense-in-depth: never write the compiled output over the input schema.
858    // The removed `serve` command did exactly this (H23) by deriving an output
859    // path identical to its input via a faulty extension swap. `--check` writes
860    // nothing, so the guard only applies to a real write.
861    if !check && Path::new(output) == Path::new(input) {
862        anyhow::bail!(
863            "Refusing to write compiled output over the input file '{input}'. \
864             Use a distinct --output path (e.g. schema.compiled.json)."
865        );
866    }
867
868    // Which config an artifact was compiled with is not answerable from the artifact,
869    // so every compile says it, before the work that could fail (#1387).
870    let config = ConfigSource::resolve(Path::new(input), config.map(Path::new))?;
871    println!("Config: {config}");
872
873    let opts = CompileOptions {
874        input,
875        types,
876        schema_dir,
877        type_files,
878        query_files,
879        mutation_files,
880        database,
881        skip_hash,
882        allow_drift,
883        config: Some(config),
884    };
885    let (artifact, optimization_report) = compile_to_schema(opts).await?;
886    let schema = &artifact.schema;
887
888    // If check-only mode, stop here
889    if check {
890        println!("✓ Schema is valid");
891        println!("  Types: {}", schema.types.len());
892        println!("  Queries: {}", schema.queries.len());
893        println!("  Mutations: {}", schema.mutations.len());
894        optimization_report.print();
895        print!("{}", localized_index_report_text(schema));
896        return Ok(());
897    }
898
899    // Write compiled schema
900    info!("Writing compiled schema to: {output}");
901    // One routine assembles the file — schema plus every sibling section — so the
902    // `_content_hash` below covers all of it (#899/#1325).
903    let value = artifact.to_json_value()?;
904
905    let output_json = if skip_hash {
906        serde_json::to_string_pretty(&value).context("Failed to serialize compiled schema")?
907    } else {
908        // One routine, shared with the `from_json` verifier, so writer and reader
909        // cannot drift (#899).
910        let hash_hex = content_hash_of(&value);
911
912        let obj = value.as_object().context("schema must serialise as JSON object")?;
913
914        // Insert _content_hash as the first field (serde_json::Map preserves insertion order)
915        let mut new_obj = serde_json::Map::new();
916        new_obj.insert("_content_hash".to_string(), serde_json::Value::String(hash_hex));
917        for (k, v) in obj {
918            new_obj.insert(k.clone(), v.clone());
919        }
920        serde_json::to_string_pretty(&serde_json::Value::Object(new_obj))?
921    };
922
923    fs::write(output, output_json).context("Failed to write compiled schema")?;
924
925    // Success message
926    println!("✓ Schema compiled successfully");
927    println!("  Input:  {input}");
928    println!("  Output: {output}");
929    println!("  Types: {}", schema.types.len());
930    println!("  Queries: {}", schema.queries.len());
931    println!("  Mutations: {}", schema.mutations.len());
932    optimization_report.print();
933    print!("{}", localized_index_report_text(schema));
934
935    // Emit DDL to directory if requested
936    if let Some(ddl_dir) = emit_ddl {
937        emit_ddl_to_dir(schema, ddl_dir)?;
938    }
939
940    // Check for migration drift if requested
941    if check_migrations {
942        run_check_migrations(schema)?;
943    }
944
945    Ok(())
946}
947
948/// The expression indexes a filter or sort on a localized field reads (#1513), as printed.
949///
950/// One per (field, allowed locale), each on exactly the key the query builds. Empty when the
951/// schema has no localized field.
952///
953/// A view gets the advice and no DDL: an index belongs on its base table, which the schema
954/// does not name.
955#[must_use]
956pub fn localized_index_report_text(schema: &fraiseql_core::schema::CompiledSchema) -> String {
957    use std::fmt::Write as _;
958    let report = schema.localized_index_report();
959    if report.is_empty() {
960        return String::new();
961    }
962    let mut text = String::from(
963        "\nLocalized field indexes (one per field and allowed locale; a filter or sort on the \
964         field reads it):\n",
965    );
966    for advice in &report {
967        let subject = format!(
968            "{}.{} [{}] on {}",
969            advice.type_name, advice.field, advice.locale, advice.table
970        );
971        // fmt::Write for String is infallible.
972        let _ = match &advice.index {
973            Some(index) => writeln!(text, "  {subject}:\n    {}", index.ddl),
974            None => writeln!(
975                text,
976                "  {subject}: a view; create the index on its base table, on the expression the \
977                 query reads"
978            ),
979        };
980    }
981    text
982}
983
984/// The `--emit-ddl` artifact's format version (#965), the first line of every file it
985/// writes. Raised when a consumer reading the old format would misread the new one.
986pub const EMIT_DDL_FORMAT: u32 = 1;
987
988/// Emit `CREATE TABLE` DDL files for all compiled schema types to `output_dir`.
989///
990/// Each type produces one `<type_snake_case>.sql` file containing a `CREATE TABLE IF NOT EXISTS`
991/// statement. The output directory is created if it does not already exist.
992///
993/// The directory is a contract (#965; `docs/operations/emit-ddl.md`): Confiture's `migrate diff
994/// --to <dir>` reads it. Every file opens with the format version ([`EMIT_DDL_FORMAT`]) and the
995/// compiler that wrote it, and the bytes are a function of the schema alone: the same schema
996/// gives the same files whatever order its types arrived in.
997///
998/// # Errors
999///
1000/// Returns an error if two types would write one file (their table names coincide), if the
1001/// output directory cannot be created, or if any DDL file cannot be written. A refusal writes
1002/// nothing.
1003pub fn emit_ddl_to_dir(schema: &CompiledSchema, output_dir: &str) -> Result<()> {
1004    // Every file built before any is written, so a refusal leaves the directory as it was.
1005    let mut files: std::collections::BTreeMap<String, (&str, String)> =
1006        std::collections::BTreeMap::new();
1007    for type_def in &schema.types {
1008        let table_name = to_snake_case(type_def.name.as_str());
1009        let ddl = build_create_table_ddl(&table_name, type_def)?;
1010        if let Some((other, _)) = files.get(&table_name) {
1011            let (first, second) = if *other < type_def.name.as_str() {
1012                (*other, type_def.name.as_str())
1013            } else {
1014                (type_def.name.as_str(), *other)
1015            };
1016            anyhow::bail!(
1017                "--emit-ddl: types '{first}' and '{second}' both map to the table \
1018                 `tb_{table_name}` and the file `{table_name}.sql`; rename one of them"
1019            );
1020        }
1021        files.insert(table_name, (type_def.name.as_str(), ddl));
1022    }
1023
1024    fs::create_dir_all(output_dir)
1025        .context(format!("Failed to create DDL output directory: {output_dir}"))?;
1026    for (table_name, (type_name, ddl)) in &files {
1027        let file_path = Path::new(output_dir).join(format!("{table_name}.sql"));
1028        fs::write(&file_path, ddl)
1029            .context(format!("Failed to write DDL for type '{type_name}'"))?;
1030    }
1031
1032    println!("✓ DDL emitted to {output_dir}/ ({} table(s))", files.len());
1033    Ok(())
1034}
1035
1036/// Delegate to `confiture migrate validate` for migration drift detection.
1037///
1038/// Emits DDL to a temporary directory, then invokes confiture. Exits non-zero when
1039/// drift is detected, printing a friendly remediation hint.
1040///
1041/// # Errors
1042///
1043/// Returns an error if confiture is not installed, if the temp directory cannot be
1044/// created, or if confiture reports drift or validation failures.
1045fn run_check_migrations(schema: &CompiledSchema) -> Result<()> {
1046    let tmp_dir = tempfile::tempdir().context("Failed to create temporary DDL directory")?;
1047    let tmp_path = tmp_dir.path().to_str().context("Temp directory path is not valid UTF-8")?;
1048
1049    emit_ddl_to_dir(schema, tmp_path)?;
1050
1051    info!("Running confiture migrate validate for drift detection...");
1052
1053    let status = Command::new("confiture").args(["migrate", "validate"]).status();
1054
1055    match status {
1056        Err(_) => {
1057            // confiture not installed — warn but don't fail the build
1058            eprintln!(
1059                "WARN: confiture is not installed; skipping migration drift check.\n\
1060                 Install it with: {}",
1061                super::migrate::CONFITURE_INSTALL_COMMANDS.join("  (or)  ")
1062            );
1063            Ok(())
1064        },
1065        Ok(s) if s.success() => {
1066            println!("✓ No migration drift detected.");
1067            Ok(())
1068        },
1069        Ok(_) => {
1070            eprintln!(
1071                "WARN: compiled schema diverges from database — run fraiseql migrate generate"
1072            );
1073            anyhow::bail!(
1074                "Migration drift detected. Run `fraiseql migrate generate` to create a migration."
1075            )
1076        },
1077    }
1078}
1079
1080/// Convert a `PascalCase` or `camelCase` type name to `snake_case`.
1081///
1082/// Delegates to the pipeline's one namer rather than reimplementing it. This used to
1083/// insert a separator before every uppercase character, so `HTTPServer` became
1084/// `h_t_t_p_server` in emitted DDL while JSONB key derivation — which already used
1085/// `fraiseql_core::utils::casing` — produced `http_server`. Two casing systems that
1086/// disagree about the same name produce DDL for a table the runtime never looks in.
1087pub(crate) fn to_snake_case(name: &str) -> String {
1088    fraiseql_core::utils::to_snake_case(name)
1089}
1090
1091/// Generate a `CREATE TABLE IF NOT EXISTS` DDL statement for a compiled type definition.
1092///
1093/// # Errors
1094///
1095/// Returns an error when a vector field declares an index the metric has no
1096/// pgvector operator class for — DDL `CREATE INDEX` would refuse (#959).
1097fn build_create_table_ddl(
1098    table_name: &str,
1099    type_def: &fraiseql_core::schema::TypeDefinition,
1100) -> Result<String> {
1101    let mut lines: Vec<String> = Vec::new();
1102    lines.push(format!("-- fraiseql emit-ddl format {EMIT_DDL_FORMAT}"));
1103    lines.push(format!("-- Generated by fraiseql-cli {}", env!("CARGO_PKG_VERSION")));
1104    lines.push(format!("-- Type: {}", type_def.name));
1105    if let Some(desc) = &type_def.description {
1106        lines.push(format!("-- {desc}"));
1107    }
1108    lines.push(String::new());
1109    lines.push(format!("CREATE TABLE IF NOT EXISTS tb_{table_name} ("));
1110
1111    let col_lines: Vec<String> = type_def
1112        .fields
1113        .iter()
1114        .map(|field| {
1115            let col_name = to_snake_case(field.name.as_str());
1116            // #386: a dimensioned `vector(N)` column — a bare `vector` column
1117            // cannot carry an HNSW/IVFFlat index, so a dimension-less emission
1118            // silently produced unindexable DDL. #959: the same for `bit(N)`,
1119            // where a missing width is worse still — bare `bit` is `bit(1)`.
1120            let pg_type =
1121                if field.field_type.is_searchable_vector() && field.vector_config.is_some() {
1122                    field.field_type.to_sql_type(field.vector_config.as_ref())
1123                } else {
1124                    field_type_to_pg(&field.field_type)
1125                };
1126            let nullable = if field.nullable { "" } else { " NOT NULL" };
1127            format!("    {col_name} {pg_type}{nullable}")
1128        })
1129        .collect();
1130
1131    let last = col_lines.len().saturating_sub(1);
1132    for (i, col) in col_lines.iter().enumerate() {
1133        if i < last {
1134            lines.push(format!("{col},"));
1135        } else {
1136            lines.push(col.clone());
1137        }
1138    }
1139
1140    lines.push(");".to_string());
1141
1142    // #386: vector columns need the extension and their declared ANN index.
1143    // Binary vectors need it too — `bit(N)` is a PostgreSQL type, but `<~>`,
1144    // `<%>` and the `bit_*_ops` operator classes come from pgvector (#959).
1145    let vector_fields: Vec<_> = type_def
1146        .fields
1147        .iter()
1148        .filter_map(|f| f.vector_config.as_ref().map(|c| (f, c)))
1149        .collect();
1150    if !vector_fields.is_empty() {
1151        lines.push(String::new());
1152        lines.push("CREATE EXTENSION IF NOT EXISTS vector;".to_string());
1153        for (field, config) in vector_fields {
1154            let col_name = to_snake_case(field.name.as_str());
1155            let index_ddl = config
1156                .index_type
1157                .index_sql(
1158                    &format!("tb_{table_name}"),
1159                    &col_name,
1160                    &field.field_type,
1161                    config.distance_metric,
1162                )
1163                .with_context(|| format!("field '{}.{}'", type_def.name, field.name.as_str()))?;
1164            if let Some(index_ddl) = index_ddl {
1165                lines.push(format!("{index_ddl};"));
1166            }
1167        }
1168    }
1169
1170    lines.push(String::new());
1171    Ok(lines.join("\n"))
1172}
1173
1174/// Map a `FieldType` to its PostgreSQL column type string.
1175pub(crate) fn field_type_to_pg(ft: &FieldType) -> String {
1176    match ft {
1177        FieldType::String | FieldType::Scalar(_) => "TEXT".to_string(),
1178        FieldType::Int => "INTEGER".to_string(),
1179        FieldType::Float => "DOUBLE PRECISION".to_string(),
1180        FieldType::Boolean => "BOOLEAN".to_string(),
1181        FieldType::Id | FieldType::Uuid => "UUID".to_string(),
1182        FieldType::DateTime => "TIMESTAMPTZ".to_string(),
1183        FieldType::Date => "DATE".to_string(),
1184        FieldType::Time => "TIME".to_string(),
1185        FieldType::Json | FieldType::List { item: _, .. } | FieldType::Object(_) => {
1186            "JSONB".to_string()
1187        },
1188        FieldType::Decimal => "NUMERIC".to_string(),
1189        FieldType::Vector => "VECTOR".to_string(),
1190        // Width-less on purpose: a `BitVector` field always carries a
1191        // `vector_config`, so the dimensioned `bit(N)` form is what the DDL
1192        // emitter uses; this branch is the shape a config-less one would take,
1193        // and `varbit` is the only bit type that does not silently truncate.
1194        FieldType::BitVector => "VARBIT".to_string(),
1195        // Width-less for the same reason as `BitVector`: a vector field always
1196        // carries a `vector_config`, so the dimensioned form is what the DDL
1197        // emitter uses.
1198        FieldType::HalfVector => "HALFVEC".to_string(),
1199        FieldType::SparseVector => "SPARSEVEC".to_string(),
1200        // Use the actual Postgres enum type name so DDL matches the schema.
1201        FieldType::Enum(name) => name.clone(),
1202        FieldType::Input(_) | FieldType::Interface(_) | FieldType::Union(_) => "JSONB".to_string(),
1203        // FieldType is #[non_exhaustive]; future variants default to TEXT.
1204        _ => "TEXT".to_string(),
1205    }
1206}
1207
1208/// Minimum distinct invalidation targets (views + fact tables) that triggers
1209/// the HOT-update fan-out warning.
1210pub(crate) const WIDE_FANOUT_THRESHOLD: usize = 3;
1211
1212/// Return mutations whose total invalidation fan-out meets or exceeds `threshold`.
1213///
1214/// Fan-out is the count of distinct views (`invalidates_views`) plus fact tables
1215/// (`invalidates_fact_tables`) that a mutation touches on every successful write.
1216/// Used by [`warn_wide_cascade_mutations`] and exposed for unit testing.
1217pub(crate) fn wide_cascade_mutations(
1218    schema: &CompiledSchema,
1219    threshold: usize,
1220) -> Vec<&fraiseql_core::schema::MutationDefinition> {
1221    schema
1222        .mutations
1223        .iter()
1224        .filter(|m| m.invalidates_views.len() + m.invalidates_fact_tables.len() >= threshold)
1225        .collect()
1226}
1227
1228/// Emit a warning for each mutation whose invalidation fan-out is wide enough
1229/// to risk exhausting PostgreSQL HOT-update page slots under high write load.
1230///
1231/// When a mutation touches many tables on every write, the free space reserved
1232/// on each heap page (needed for HOT updates) fills up quickly. Subsequent
1233/// mutations must write to a new page instead of updating in place, which
1234/// increases I/O and table bloat. Setting `fillfactor=70-80` on the backing
1235/// tables leaves 20-30 % of each page free, keeping HOT updates available.
1236///
1237/// The warning lists ready-to-run `ALTER TABLE … SET (fillfactor = 75)` statements
1238/// derived from the view names using FraiseQL naming conventions
1239/// (`tv_foo` / `v_foo` → `tb_foo`).
1240fn warn_wide_cascade_mutations(schema: &CompiledSchema) {
1241    for mutation in wide_cascade_mutations(schema, WIDE_FANOUT_THRESHOLD) {
1242        let total = mutation.invalidates_views.len() + mutation.invalidates_fact_tables.len();
1243
1244        // Build a sorted, deduplicated target list for a stable message.
1245        let mut targets: Vec<&str> = mutation
1246            .invalidates_views
1247            .iter()
1248            .chain(mutation.invalidates_fact_tables.iter())
1249            .map(String::as_str)
1250            .collect();
1251        targets.sort_unstable();
1252        targets.dedup();
1253
1254        // Derive a likely backing-table name from FraiseQL view naming conventions.
1255        // tv_foo → tb_foo, v_foo → tb_foo, anything else (e.g. fact tables) unchanged.
1256        let alter_stmts: Vec<String> = targets
1257            .iter()
1258            .map(|&name| {
1259                let table = name
1260                    .strip_prefix("tv_")
1261                    .or_else(|| name.strip_prefix("v_"))
1262                    .map_or_else(|| name.to_string(), |rest| format!("tb_{rest}"));
1263                format!("ALTER TABLE {table} SET (fillfactor = 75);")
1264            })
1265            .collect();
1266
1267        warn!(
1268            "mutation '{}' has a wide invalidation fan-out ({} targets: [{}]). \
1269             Under high write load, HOT-update page slots on these tables may be \
1270             exhausted, forcing full-page writes and reducing mutation throughput. \
1271             Set fillfactor=70-80 on the backing tables: {}  \
1272             Monitor HOT efficiency: SELECT relname, \
1273             n_tup_hot_upd * 100 / NULLIF(n_tup_upd, 0) AS hot_pct \
1274             FROM pg_stat_user_tables WHERE n_tup_upd > 0 ORDER BY hot_pct;",
1275            mutation.name,
1276            total,
1277            targets.join(", "),
1278            alter_stmts.join("  "),
1279        );
1280    }
1281}
1282
1283/// Detect single-JSONB mutations that would silently forward `camelCase` input
1284/// keys to a `snake_case` SQL function under `naming_convention = "preserve"`.
1285///
1286/// Returns `(mutation_name, camelCase_field_names)` for every mutation that, on a
1287/// `Preserve` schema, takes one declared `input` Input type through the
1288/// single-JSONB path (`input_style = "jsonb"` or an `Update`) and whose Input
1289/// type has `camelCase`-looking field name(s). Under `Preserve` the runtime
1290/// forwards the payload verbatim — no `snake_case` recasing — so a function
1291/// reading `payload->>'snake_field'` sees NULL for every multi-word field (#456).
1292///
1293/// Pure (no I/O) so it can be unit-tested; [`warn_jsonb_preserve_mismatch`] is the
1294/// thin logging wrapper.
1295pub(crate) fn jsonb_preserve_mismatches(schema: &CompiledSchema) -> Vec<(String, Vec<String>)> {
1296    if schema.naming_convention != NamingConvention::Preserve {
1297        return Vec::new();
1298    }
1299    let mut out = Vec::new();
1300    for mutation in &schema.mutations {
1301        // The single-JSONB path: an explicit `input_style = jsonb` or an Update,
1302        // both of which forward the whole `input` object as one JSONB arg.
1303        let single_jsonb = matches!(mutation.input_style, InputStyle::Jsonb)
1304            || matches!(mutation.operation, MutationOperation::Update { .. });
1305        if !single_jsonb {
1306            continue;
1307        }
1308        // The single-`input`-object pattern: exactly one arg named "input" whose
1309        // type is a declared input type (mirrors the runtime's detection). The
1310        // compiler emits input-type references as `FieldType::Object`, never
1311        // `FieldType::Input`, so recognise an `Object` naming a registered input
1312        // type too — otherwise this warning is blind to every real compiled schema
1313        // (#456).
1314        let input_type_name = match mutation.arguments.as_slice() {
1315            [arg] if arg.name == "input" => match &arg.arg_type {
1316                FieldType::Input(name) => name.as_str(),
1317                FieldType::Object(name) if schema.find_input_type(name).is_some() => name.as_str(),
1318                _ => continue,
1319            },
1320            _ => continue,
1321        };
1322        let Some(input_type) = schema.find_input_type(input_type_name) else {
1323            continue;
1324        };
1325        // A field name with any uppercase letter is camelCase-looking — under
1326        // Preserve it reaches the function verbatim and won't match a snake key.
1327        let camel_fields: Vec<String> = input_type
1328            .fields
1329            .iter()
1330            .filter(|f| f.name.chars().any(|c| c.is_ascii_uppercase()))
1331            .map(|f| f.name.clone())
1332            .collect();
1333        if !camel_fields.is_empty() {
1334            out.push((mutation.name.clone(), camel_fields));
1335        }
1336    }
1337    out
1338}
1339
1340/// Warn for every [`jsonb_preserve_mismatches`] hit — the silent #456
1341/// misconfiguration where a `preserve` schema forwards camelCase input keys to a
1342/// snake_case SQL function on the single-JSONB path.
1343fn warn_jsonb_preserve_mismatch(schema: &CompiledSchema) {
1344    for (mutation, fields) in jsonb_preserve_mismatches(schema) {
1345        warn!(
1346            "mutation '{mutation}' forwards its input as a single JSONB payload but the schema \
1347             uses naming_convention = \"preserve\", and its input type has camelCase field(s) \
1348             [{}]. Under 'preserve' the runtime forwards input keys verbatim (no snake_case \
1349             recasing), so a SQL function reading payload->>'snake_field' will receive these \
1350             camelCase keys and see NULL. Set naming_convention = \"camelCase\" (the default for \
1351             new schemas) if the function expects snake_case keys, or rename the fields. (#456)",
1352            fields.join(", "),
1353        );
1354    }
1355}
1356
1357/// Build a PostgreSQL introspector connected to `db_url`.
1358///
1359/// # Errors
1360///
1361/// Returns error if the connection URL is invalid, the server cannot be reached, or
1362/// it is older than PostgreSQL 18.
1363pub(crate) async fn build_postgres_introspector(
1364    db_url: &str,
1365    tls: &PostgresTlsConfig,
1366) -> Result<fraiseql_core::db::postgres::PostgresIntrospector> {
1367    let pool = crate::connection::postgres_pool(db_url, "database validation", tls).await?;
1368    Ok(fraiseql_core::db::postgres::PostgresIntrospector::new(pool))
1369}
1370
1371/// Auto-param names excluded from `native_columns` inference and JSONB-extraction warnings.
1372const NATIVE_COLUMN_SKIP_ARGS: &[&str] = &[
1373    "where", "limit", "offset", "orderBy", "first", "last", "after", "before",
1374];
1375
1376/// Fold what introspection discovered back into the compiled schema.
1377///
1378/// Both halves are *sharpenings*: the schema is already correct without a
1379/// database, and a `--database` compile makes it cheaper. Split out of the
1380/// pipeline so the fold is testable without one — the patch loop it replaced was
1381/// reachable only from a live connection, which is the shape a discovery that
1382/// silently stops being applied hides in.
1383///
1384/// An authored `pagination_order` is protected upstream, in the validator, which
1385/// only reports a sharpening for a query whose ordering the compiler derived. It
1386/// is stated here too because this is where the overwrite happens.
1387pub(crate) fn apply_database_report(
1388    schema: &mut fraiseql_core::schema::CompiledSchema,
1389    report: &crate::schema::database_validator::DatabaseValidationReport,
1390) {
1391    for query in &mut schema.queries {
1392        if let Some(cols) = report.native_columns.get(&query.name) {
1393            query.native_columns = cols.clone();
1394        }
1395        if let Some(order) = report.pagination_orders.get(&query.name) {
1396            query.pagination_order = Some(order.clone());
1397        }
1398    }
1399}
1400
1401/// Infer `native_columns` for `ID`/`UUID`-typed arguments on JSONB-backed queries.
1402///
1403/// When a query reads from a JSONB table (`sql_source` + non-empty `jsonb_column`) and an
1404/// argument is typed [`FieldType::Id`] or [`FieldType::Uuid`], the argument name almost
1405/// certainly maps to a native UUID column alongside the `data` JSONB column
1406/// (e.g. `id UUID NOT NULL`). Emitting `WHERE id = $1::uuid` instead of
1407/// `WHERE data->>'id' = $1` lets the planner use the B-tree index without
1408/// needing a database connection at compile time.
1409///
1410/// Auto-param names (`where`, `limit`, `offset`, etc.) are skipped.
1411/// Arguments already present in `native_columns` are not overridden.
1412pub(crate) fn infer_native_columns_from_arg_types(schema: &mut CompiledSchema) {
1413    for query in &mut schema.queries {
1414        if query.sql_source.is_none() || query.jsonb_column.is_empty() {
1415            continue;
1416        }
1417        for arg in &query.arguments {
1418            if NATIVE_COLUMN_SKIP_ARGS.contains(&arg.name.as_str()) {
1419                continue;
1420            }
1421            if query.native_columns.contains_key(&arg.name) {
1422                continue; // already explicitly declared — don't override
1423            }
1424            if matches!(arg.arg_type, FieldType::Id | FieldType::Uuid) {
1425                // Inferred from the argument's type: nothing proves the column NOT NULL.
1426                query.native_columns.insert(
1427                    arg.name.clone(),
1428                    fraiseql_core::schema::NativeColumn::nullable("uuid"),
1429                );
1430            }
1431        }
1432    }
1433}