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    CURRENT_SCHEMA_FORMAT_VERSION, CompiledSchema, FieldType, canonicalize_json,
10};
11use tracing::{info, warn};
12
13use crate::{
14    config::TomlProjectConfig,
15    schema::{
16        IntermediateSchema, OptimizationReport, SchemaConverter, SchemaOptimizer, SchemaValidator,
17        database_validator::validate_schema_against_database,
18    },
19};
20
21/// Input source configuration for schema compilation.
22#[derive(Debug, Default)]
23pub struct CompileOptions<'a> {
24    /// Path to `fraiseql.toml` (TOML workflow) or `schema.json` (legacy).
25    pub input:          &'a str,
26    /// Optional path to `types.json` (TOML workflow, backward compat).
27    pub types:          Option<&'a str>,
28    /// Optional directory for schema file auto-discovery.
29    pub schema_dir:     Option<&'a str>,
30    /// Explicit type file paths (highest priority).
31    pub type_files:     Vec<String>,
32    /// Explicit query file paths.
33    pub query_files:    Vec<String>,
34    /// Explicit mutation file paths.
35    pub mutation_files: Vec<String>,
36    /// Optional database URL for indexed column validation.
37    pub database:       Option<&'a str>,
38    /// Skip embedding content hash in compiled schema (for test fixtures).
39    pub skip_hash:      bool,
40}
41
42impl<'a> CompileOptions<'a> {
43    /// Create new compile options with just the input path.
44    #[must_use]
45    pub fn new(input: &'a str) -> Self {
46        Self {
47            input,
48            ..Default::default()
49        }
50    }
51
52    /// Set the types path.
53    #[must_use]
54    pub fn with_types(mut self, types: &'a str) -> Self {
55        self.types = Some(types);
56        self
57    }
58
59    /// Set the schema directory for auto-discovery.
60    #[must_use]
61    pub fn with_schema_dir(mut self, schema_dir: &'a str) -> Self {
62        self.schema_dir = Some(schema_dir);
63        self
64    }
65
66    /// Set the database URL for validation.
67    #[must_use]
68    pub fn with_database(mut self, database: &'a str) -> Self {
69        self.database = Some(database);
70        self
71    }
72}
73
74/// Select and execute the appropriate schema-loading strategy for TOML-based workflows.
75///
76/// Tries strategies in priority order:
77/// 1. Explicit file lists (highest priority)
78/// 2. Directory auto-discovery
79/// 3. Single types file (backward-compatible)
80/// 4. Domain discovery → TOML includes → TOML-only (fallback sequence)
81#[allow(clippy::cognitive_complexity)] // Reason: multi-strategy schema discovery with fallback chain
82fn load_intermediate_schema(
83    toml_path: &str,
84    type_files: &[String],
85    query_files: &[String],
86    mutation_files: &[String],
87    schema_dir: Option<&str>,
88    types_path: Option<&str>,
89) -> Result<IntermediateSchema> {
90    if !type_files.is_empty() || !query_files.is_empty() || !mutation_files.is_empty() {
91        info!("Mode: Explicit file lists");
92        return crate::schema::SchemaMerger::merge_explicit_files(
93            toml_path,
94            type_files,
95            query_files,
96            mutation_files,
97        )
98        .context("Failed to load explicit schema files");
99    }
100    if let Some(dir) = schema_dir {
101        info!("Mode: Auto-discovery from directory: {}", dir);
102        return crate::schema::SchemaMerger::merge_from_directory(toml_path, dir)
103            .context("Failed to load schema from directory");
104    }
105    if let Some(types) = types_path {
106        info!("Mode: Language + TOML (types.json + fraiseql.toml)");
107        return crate::schema::SchemaMerger::merge_files(types, toml_path)
108            .context("Failed to merge types.json with TOML");
109    }
110    info!("Mode: TOML-based (checking for domain discovery...)");
111    if let Ok(schema) = crate::schema::SchemaMerger::merge_from_domains(toml_path) {
112        return Ok(schema);
113    }
114    info!("No domains configured, checking for TOML includes...");
115    if let Ok(schema) = crate::schema::SchemaMerger::merge_with_includes(toml_path) {
116        return Ok(schema);
117    }
118    info!("No includes configured, using TOML-only definitions");
119    crate::schema::SchemaMerger::merge_toml_only(toml_path)
120        .context("Failed to load schema from TOML")
121}
122
123/// Compile a schema to `CompiledSchema` without writing to disk.
124///
125/// This is the core compilation logic, shared between `compile` (which writes to disk)
126/// and `run` (which serves in-memory without any file artifacts).
127///
128/// # Arguments
129///
130/// * `opts` - Compilation options including input paths and configuration
131///
132/// # Errors
133///
134/// Returns error if input is missing, parsing fails, validation fails, or the database
135/// connection fails (when `database` is provided).
136#[allow(clippy::cognitive_complexity)] // Reason: end-to-end compilation pipeline with validation, introspection, and output stages
137pub async fn compile_to_schema(
138    opts: CompileOptions<'_>,
139) -> Result<(CompiledSchema, OptimizationReport)> {
140    info!("Compiling schema: {}", opts.input);
141
142    // 1. Determine workflow based on input file and options
143    let input_path = Path::new(opts.input);
144    if !input_path.exists() {
145        anyhow::bail!("Input file not found: {}", opts.input);
146    }
147
148    // Load schema based on file type and options
149    let is_toml = input_path
150        .extension()
151        .and_then(|ext| ext.to_str())
152        .is_some_and(|ext| ext.eq_ignore_ascii_case("toml"));
153    let mut intermediate: IntermediateSchema = if is_toml {
154        info!("Using TOML-based workflow");
155        load_intermediate_schema(
156            opts.input,
157            &opts.type_files,
158            &opts.query_files,
159            &opts.mutation_files,
160            opts.schema_dir,
161            opts.types,
162        )?
163    } else {
164        // Legacy JSON workflow
165        info!("Using legacy JSON workflow");
166        let schema_json = fs::read_to_string(input_path).context("Failed to read schema.json")?;
167
168        // 2. Parse JSON into IntermediateSchema (language-agnostic format)
169        info!("Parsing intermediate schema...");
170        serde_json::from_str(&schema_json).context("Failed to parse schema.json")?
171    };
172
173    // 2a. Load and apply security configuration from fraiseql.toml if it exists.
174    // Skip when the input itself is a TomlSchema file: in that case the security
175    // settings are embedded in the TomlSchema, and the CWD fraiseql.toml uses a
176    // different TOML format (TomlSchema vs TomlProjectConfig) that is not compatible.
177    if !is_toml && Path::new("fraiseql.toml").exists() {
178        info!("Loading security configuration from fraiseql.toml...");
179        match TomlProjectConfig::from_file("fraiseql.toml") {
180            Ok(config) => {
181                info!("Validating security configuration...");
182                config.validate()?;
183
184                info!("Applying security configuration to schema...");
185                // Merge security config into intermediate schema
186                let mut security_json = config.fraiseql.security.to_json();
187
188                // Embed tenancy configuration into the security section
189                if !matches!(
190                    config.fraiseql.tenancy.mode,
191                    crate::config::security::TenancyModeConfig::None
192                ) {
193                    security_json["tenancy"] = config.fraiseql.tenancy.to_json();
194                }
195
196                intermediate.security = Some(security_json);
197
198                info!("Security configuration applied successfully");
199            },
200            Err(e) => {
201                anyhow::bail!(
202                    "Failed to parse fraiseql.toml: {e}\n\
203                     Fix the configuration file or remove it to use defaults."
204                );
205            },
206        }
207    } else {
208        info!("No fraiseql.toml found, using default security configuration");
209    }
210
211    // 2b. Validate @tenant_id annotations when tenancy mode is "row".
212    // Extract tenancy config from the already-embedded security JSON.
213    let tenancy_row_claim: Option<String> = intermediate.security.as_ref().and_then(|sec| {
214        let tenancy = sec.get("tenancy")?;
215        let mode = tenancy.get("mode").and_then(|m| m.as_str()).unwrap_or("none");
216        if mode == "row" {
217            Some(
218                tenancy
219                    .get("tenantClaim")
220                    .and_then(|c| c.as_str())
221                    .unwrap_or("tenant_id")
222                    .to_string(),
223            )
224        } else {
225            None
226        }
227    });
228    if let Some(tenant_claim) = &tenancy_row_claim {
229        info!("Validating @tenant_id annotations for row-isolation tenancy...");
230        crate::schema::converter::tenancy::validate_tenant_annotations(
231            &mut intermediate,
232            tenant_claim,
233        )
234        .context("@tenant_id validation failed")?;
235    }
236
237    // 3. Validate intermediate schema
238    info!("Validating schema structure...");
239    let validation_report =
240        SchemaValidator::validate(&intermediate).context("Failed to validate schema")?;
241
242    if !validation_report.is_valid() {
243        validation_report.print();
244        anyhow::bail!("Schema validation failed with {} error(s)", validation_report.error_count());
245    }
246
247    // Print warnings if any
248    if validation_report.warning_count() > 0 {
249        validation_report.print();
250    }
251
252    // 4. Convert to CompiledSchema (validates and normalizes)
253    info!("Converting to compiled format...");
254    let mut schema = SchemaConverter::convert(intermediate)
255        .context("Failed to convert schema to compiled format")?;
256
257    // 5. Optimize schema and generate SQL hints (mutates schema in place, report for display)
258    info!("Analyzing schema for optimization opportunities...");
259    let report = SchemaOptimizer::optimize(&mut schema).context("Failed to optimize schema")?;
260
261    // 5a. Stamp schema format version for runtime compatibility checks.
262    schema.schema_format_version = Some(CURRENT_SCHEMA_FORMAT_VERSION);
263
264    // 5b-pre. Infer native_columns for ID/UUID-typed arguments on JSONB-backed queries.
265    // DB introspection (step 5b) overrides these inferred values when `--database` is provided.
266    infer_native_columns_from_arg_types(&mut schema);
267
268    // 5b. Optional: Validate indexed columns and native columns against database.
269    if let Some(db_url) = opts.database {
270        info!("Validating indexed columns against database...");
271        validate_indexed_columns(&schema, db_url).await?;
272
273        info!("Validating native columns for direct query arguments...");
274        let pg_introspector = build_postgres_introspector(db_url)
275            .context("Failed to connect for native column validation")?;
276        let db_report = validate_schema_against_database(&schema, &pg_introspector).await?;
277        for w in &db_report.warnings {
278            warn!("{w}");
279        }
280        // Patch QueryDefinitions with DB-discovered native_columns, overriding inferred values.
281        for query in &mut schema.queries {
282            if let Some(cols) = db_report.native_columns.get(&query.name) {
283                query.native_columns = cols.clone();
284            }
285        }
286    } else {
287        // Warn for queries that still have unresolved direct arguments after inference.
288        // Arguments already covered by native_columns inference are not warned about.
289        for query in &schema.queries {
290            if query.sql_source.is_none() {
291                continue;
292            }
293            let unresolved: Vec<_> = query
294                .arguments
295                .iter()
296                .filter(|a| !NATIVE_COLUMN_SKIP_ARGS.contains(&a.name.as_str()))
297                .filter(|a| !query.native_columns.contains_key(&a.name))
298                .collect();
299            if !unresolved.is_empty() {
300                let names: Vec<_> = unresolved.iter().map(|a| a.name.as_str()).collect();
301                warn!(
302                    "query `{}`: argument(s) {:?} on `{}` could not be resolved to native \
303                     columns — no --database URL provided. These filters will use JSONB \
304                     extraction. Provide --database or annotate with native_columns.",
305                    query.name,
306                    names,
307                    query.sql_source.as_deref().unwrap_or("?"),
308                );
309            }
310        }
311    }
312
313    // 5c. Warn when SQLite is the target but the schema uses features SQLite doesn't support.
314    check_sqlite_compatibility_warnings(&schema, opts.input, is_toml, opts.database);
315
316    // 5d. Warn when mutations have wide invalidation fan-out (HOT update pressure).
317    warn_wide_cascade_mutations(&schema);
318
319    Ok((schema, report))
320}
321
322/// Run the compile command
323///
324/// # Arguments
325///
326/// * `input` - Path to fraiseql.toml (TOML) or schema.json (legacy)
327/// * `types` - Optional path to types.json (when using TOML workflow)
328/// * `schema_dir` - Optional directory for auto-discovery of schema files
329/// * `type_files` - Optional vector of explicit type file paths
330/// * `query_files` - Optional vector of explicit query file paths
331/// * `mutation_files` - Optional vector of explicit mutation file paths
332/// * `output` - Path to write schema.compiled.json
333/// * `check` - If true, validate only without writing output
334/// * `database` - Optional database URL for indexed column validation
335/// * `emit_ddl` - Optional directory to write `CREATE TABLE` DDL files (confiture format)
336/// * `check_migrations` - If true, run `confiture migrate validate` after compilation
337/// * `skip_hash` - Skip embedding content hash (for test fixtures)
338///
339/// # Errors
340///
341/// Returns error if:
342/// - Input file doesn't exist or can't be read
343/// - JSON/TOML parsing fails
344/// - Schema validation fails
345/// - Output file can't be written
346/// - Database connection fails (when database URL is provided)
347/// - DDL output directory cannot be created (when `emit_ddl` is provided)
348/// - `confiture` is not installed (when `check_migrations` is true)
349/// - Migration drift detected (when `check_migrations` is true)
350#[allow(clippy::too_many_arguments)]
351// Reason: run() is the CLI entry point that receives individual args from clap; keeping them
352// separate for clarity
353#[doc(hidden)] // Internal-pub: CLI entry point dispatched by runner.rs; not a stable downstream API.
354pub async fn run(
355    input: &str,
356    types: Option<&str>,
357    schema_dir: Option<&str>,
358    type_files: Vec<String>,
359    query_files: Vec<String>,
360    mutation_files: Vec<String>,
361    output: &str,
362    check: bool,
363    database: Option<&str>,
364    emit_ddl: Option<&str>,
365    check_migrations: bool,
366    skip_hash: bool,
367) -> Result<()> {
368    let opts = CompileOptions {
369        input,
370        types,
371        schema_dir,
372        type_files,
373        query_files,
374        mutation_files,
375        database,
376        skip_hash,
377    };
378    let (schema, optimization_report) = compile_to_schema(opts).await?;
379
380    // If check-only mode, stop here
381    if check {
382        println!("✓ Schema is valid");
383        println!("  Types: {}", schema.types.len());
384        println!("  Queries: {}", schema.queries.len());
385        println!("  Mutations: {}", schema.mutations.len());
386        optimization_report.print();
387        return Ok(());
388    }
389
390    // Write compiled schema
391    info!("Writing compiled schema to: {output}");
392    let output_json = if skip_hash {
393        serde_json::to_string_pretty(&schema).context("Failed to serialize compiled schema")?
394    } else {
395        use sha2::{Digest, Sha256};
396
397        let body =
398            serde_json::to_string_pretty(&schema).context("Failed to serialize compiled schema")?;
399        let value: serde_json::Value = serde_json::from_str(&body)?;
400        // Canonicalize (recursively sort keys) before hashing — matches from_json verifier
401        let canonical = serde_json::to_string_pretty(&canonicalize_json(&value))?;
402        let hash = Sha256::digest(canonical.as_bytes());
403        let hash_hex = hex::encode(&hash[..16]);
404
405        let obj = value.as_object().context("schema must serialise as JSON object")?;
406
407        // Insert _content_hash as the first field (serde_json::Map preserves insertion order)
408        let mut new_obj = serde_json::Map::new();
409        new_obj.insert("_content_hash".to_string(), serde_json::Value::String(hash_hex));
410        for (k, v) in obj {
411            new_obj.insert(k.clone(), v.clone());
412        }
413        serde_json::to_string_pretty(&serde_json::Value::Object(new_obj))?
414    };
415
416    fs::write(output, output_json).context("Failed to write compiled schema")?;
417
418    // Success message
419    println!("✓ Schema compiled successfully");
420    println!("  Input:  {input}");
421    println!("  Output: {output}");
422    println!("  Types: {}", schema.types.len());
423    println!("  Queries: {}", schema.queries.len());
424    println!("  Mutations: {}", schema.mutations.len());
425    optimization_report.print();
426
427    // Emit DDL to directory if requested
428    if let Some(ddl_dir) = emit_ddl {
429        emit_ddl_to_dir(&schema, ddl_dir)?;
430    }
431
432    // Check for migration drift if requested
433    if check_migrations {
434        run_check_migrations(&schema)?;
435    }
436
437    Ok(())
438}
439
440/// Emit `CREATE TABLE` DDL files for all compiled schema types to `output_dir`.
441///
442/// Each type produces one `<type_snake_case>.sql` file containing a `CREATE TABLE IF NOT EXISTS`
443/// statement. The output directory is created if it does not already exist.
444///
445/// Output is compatible with confiture's `db/schema/` directory format, so that
446/// `fraiseql migrate generate` can auto-detect drift between the compiled schema and the
447/// current migrations.
448///
449/// # Errors
450///
451/// Returns an error if the output directory cannot be created, or if any DDL file
452/// cannot be written.
453pub fn emit_ddl_to_dir(schema: &CompiledSchema, output_dir: &str) -> Result<()> {
454    fs::create_dir_all(output_dir)
455        .context(format!("Failed to create DDL output directory: {output_dir}"))?;
456
457    let mut count = 0;
458    for type_def in &schema.types {
459        let table_name = to_snake_case(type_def.name.as_str());
460        let ddl = build_create_table_ddl(&table_name, type_def);
461
462        let file_path = Path::new(output_dir).join(format!("{table_name}.sql"));
463        fs::write(&file_path, ddl)
464            .context(format!("Failed to write DDL for type '{}'", type_def.name))?;
465        count += 1;
466    }
467
468    println!("✓ DDL emitted to {output_dir}/ ({count} table(s))");
469    Ok(())
470}
471
472/// Delegate to `confiture migrate validate` for migration drift detection.
473///
474/// Emits DDL to a temporary directory, then invokes confiture. Exits non-zero when
475/// drift is detected, printing a friendly remediation hint.
476///
477/// # Errors
478///
479/// Returns an error if confiture is not installed, if the temp directory cannot be
480/// created, or if confiture reports drift or validation failures.
481fn run_check_migrations(schema: &CompiledSchema) -> Result<()> {
482    let tmp_dir = tempfile::tempdir().context("Failed to create temporary DDL directory")?;
483    let tmp_path = tmp_dir.path().to_str().context("Temp directory path is not valid UTF-8")?;
484
485    emit_ddl_to_dir(schema, tmp_path)?;
486
487    info!("Running confiture migrate validate for drift detection...");
488
489    let status = Command::new("confiture").args(["migrate", "validate"]).status();
490
491    match status {
492        Err(_) => {
493            // confiture not installed — warn but don't fail the build
494            eprintln!(
495                "WARN: confiture is not installed; skipping migration drift check.\n\
496                 Install it with: cargo install confiture"
497            );
498            Ok(())
499        },
500        Ok(s) if s.success() => {
501            println!("✓ No migration drift detected.");
502            Ok(())
503        },
504        Ok(_) => {
505            eprintln!(
506                "WARN: compiled schema diverges from database — run fraiseql migrate generate"
507            );
508            anyhow::bail!(
509                "Migration drift detected. Run `fraiseql migrate generate` to create a migration."
510            )
511        },
512    }
513}
514
515/// Convert a `PascalCase` or `camelCase` type name to `snake_case`.
516pub(crate) fn to_snake_case(name: &str) -> String {
517    let mut result = String::with_capacity(name.len() + 4);
518    for (i, ch) in name.chars().enumerate() {
519        if ch.is_uppercase() && i > 0 {
520            result.push('_');
521        }
522        result.extend(ch.to_lowercase());
523    }
524    result
525}
526
527/// Generate a `CREATE TABLE IF NOT EXISTS` DDL statement for a compiled type definition.
528fn build_create_table_ddl(
529    table_name: &str,
530    type_def: &fraiseql_core::schema::TypeDefinition,
531) -> String {
532    let mut lines: Vec<String> = Vec::new();
533    lines.push("-- Generated by fraiseql compile --emit-ddl".to_string());
534    lines.push(format!("-- Type: {}", type_def.name));
535    if let Some(desc) = &type_def.description {
536        lines.push(format!("-- {desc}"));
537    }
538    lines.push(String::new());
539    lines.push(format!("CREATE TABLE IF NOT EXISTS tb_{table_name} ("));
540
541    let col_lines: Vec<String> = type_def
542        .fields
543        .iter()
544        .map(|field| {
545            let col_name = to_snake_case(field.name.as_str());
546            let pg_type = field_type_to_pg(&field.field_type);
547            let nullable = if field.nullable { "" } else { " NOT NULL" };
548            format!("    {col_name} {pg_type}{nullable}")
549        })
550        .collect();
551
552    let last = col_lines.len().saturating_sub(1);
553    for (i, col) in col_lines.iter().enumerate() {
554        if i < last {
555            lines.push(format!("{col},"));
556        } else {
557            lines.push(col.clone());
558        }
559    }
560
561    lines.push(");".to_string());
562    lines.push(String::new());
563    lines.join("\n")
564}
565
566/// Map a `FieldType` to its PostgreSQL column type string.
567pub(crate) fn field_type_to_pg(ft: &FieldType) -> String {
568    match ft {
569        FieldType::String | FieldType::Scalar(_) => "TEXT".to_string(),
570        FieldType::Int => "INTEGER".to_string(),
571        FieldType::Float => "DOUBLE PRECISION".to_string(),
572        FieldType::Boolean => "BOOLEAN".to_string(),
573        FieldType::Id | FieldType::Uuid => "UUID".to_string(),
574        FieldType::DateTime => "TIMESTAMPTZ".to_string(),
575        FieldType::Date => "DATE".to_string(),
576        FieldType::Time => "TIME".to_string(),
577        FieldType::Json | FieldType::List(_) | FieldType::Object(_) => "JSONB".to_string(),
578        FieldType::Decimal => "NUMERIC".to_string(),
579        FieldType::Vector => "VECTOR".to_string(),
580        // Use the actual Postgres enum type name so DDL matches the schema.
581        FieldType::Enum(name) => name.clone(),
582        FieldType::Input(_) | FieldType::Interface(_) | FieldType::Union(_) => "JSONB".to_string(),
583        // FieldType is #[non_exhaustive]; future variants default to TEXT.
584        _ => "TEXT".to_string(),
585    }
586}
587
588/// Emit warnings when schema uses features that SQLite does not support.
589///
590/// SQLite lacks stored procedures (mutations) and relay/subscription support.
591/// A compile-time warning helps catch this before runtime failures.
592fn check_sqlite_compatibility_warnings(
593    schema: &CompiledSchema,
594    input_path: &str,
595    is_toml: bool,
596    database_url: Option<&str>,
597) {
598    let target_is_sqlite = database_url
599        .is_some_and(|url| url.to_ascii_lowercase().starts_with("sqlite://"))
600        || is_toml && detect_sqlite_target_in_toml(input_path);
601
602    if !target_is_sqlite {
603        return;
604    }
605
606    let mutation_count = schema.mutations.len();
607    let relay_count = schema.queries.iter().filter(|q| q.relay).count();
608    let subscription_count = schema.subscriptions.len();
609
610    if mutation_count > 0 {
611        warn!(
612            "Schema contains {} mutation(s) but target database is SQLite. \
613             Mutations are not supported on SQLite. \
614             See: https://fraiseql.dev/docs/database-compatibility",
615            mutation_count,
616        );
617    }
618    if relay_count > 0 {
619        warn!(
620            "Schema contains {} relay query/queries but target database is SQLite. \
621             Relay (keyset pagination) is not supported on SQLite. \
622             See: https://fraiseql.dev/docs/database-compatibility",
623            relay_count,
624        );
625    }
626    if subscription_count > 0 {
627        warn!(
628            "Schema contains {} subscription(s) but target database is SQLite. \
629             Subscriptions are not supported on SQLite. \
630             See: https://fraiseql.dev/docs/database-compatibility",
631            subscription_count,
632        );
633    }
634}
635
636/// Check if the TOML schema file specifies `database_target = "sqlite"`.
637///
638/// Reads and parses the TOML to extract the schema metadata. Returns `false`
639/// on any parse error (non-fatal — warning detection is best-effort).
640fn detect_sqlite_target_in_toml(toml_path: &str) -> bool {
641    let Ok(content) = fs::read_to_string(toml_path) else {
642        return false;
643    };
644    let Ok(toml_schema) = toml::from_str::<crate::config::toml_schema::TomlSchema>(&content) else {
645        return false;
646    };
647    toml_schema.schema.database_target.to_ascii_lowercase().contains("sqlite")
648}
649
650/// Minimum distinct invalidation targets (views + fact tables) that triggers
651/// the HOT-update fan-out warning.
652pub(crate) const WIDE_FANOUT_THRESHOLD: usize = 3;
653
654/// Return mutations whose total invalidation fan-out meets or exceeds `threshold`.
655///
656/// Fan-out is the count of distinct views (`invalidates_views`) plus fact tables
657/// (`invalidates_fact_tables`) that a mutation touches on every successful write.
658/// Used by [`warn_wide_cascade_mutations`] and exposed for unit testing.
659pub(crate) fn wide_cascade_mutations(
660    schema: &CompiledSchema,
661    threshold: usize,
662) -> Vec<&fraiseql_core::schema::MutationDefinition> {
663    schema
664        .mutations
665        .iter()
666        .filter(|m| m.invalidates_views.len() + m.invalidates_fact_tables.len() >= threshold)
667        .collect()
668}
669
670/// Emit a warning for each mutation whose invalidation fan-out is wide enough
671/// to risk exhausting PostgreSQL HOT-update page slots under high write load.
672///
673/// When a mutation touches many tables on every write, the free space reserved
674/// on each heap page (needed for HOT updates) fills up quickly. Subsequent
675/// mutations must write to a new page instead of updating in place, which
676/// increases I/O and table bloat. Setting `fillfactor=70-80` on the backing
677/// tables leaves 20-30 % of each page free, keeping HOT updates available.
678///
679/// The warning lists ready-to-run `ALTER TABLE … SET (fillfactor = 75)` statements
680/// derived from the view names using FraiseQL naming conventions
681/// (`tv_foo` / `v_foo` → `tb_foo`).
682fn warn_wide_cascade_mutations(schema: &CompiledSchema) {
683    for mutation in wide_cascade_mutations(schema, WIDE_FANOUT_THRESHOLD) {
684        let total = mutation.invalidates_views.len() + mutation.invalidates_fact_tables.len();
685
686        // Build a sorted, deduplicated target list for a stable message.
687        let mut targets: Vec<&str> = mutation
688            .invalidates_views
689            .iter()
690            .chain(mutation.invalidates_fact_tables.iter())
691            .map(String::as_str)
692            .collect();
693        targets.sort_unstable();
694        targets.dedup();
695
696        // Derive a likely backing-table name from FraiseQL view naming conventions.
697        // tv_foo → tb_foo, v_foo → tb_foo, anything else (e.g. fact tables) unchanged.
698        let alter_stmts: Vec<String> = targets
699            .iter()
700            .map(|&name| {
701                let table = name
702                    .strip_prefix("tv_")
703                    .or_else(|| name.strip_prefix("v_"))
704                    .map_or_else(|| name.to_string(), |rest| format!("tb_{rest}"));
705                format!("ALTER TABLE {table} SET (fillfactor = 75);")
706            })
707            .collect();
708
709        warn!(
710            "mutation '{}' has a wide invalidation fan-out ({} targets: [{}]). \
711             Under high write load, HOT-update page slots on these tables may be \
712             exhausted, forcing full-page writes and reducing mutation throughput. \
713             Set fillfactor=70-80 on the backing tables: {}  \
714             Monitor HOT efficiency: SELECT relname, \
715             n_tup_hot_upd * 100 / NULLIF(n_tup_upd, 0) AS hot_pct \
716             FROM pg_stat_user_tables WHERE n_tup_upd > 0 ORDER BY hot_pct;",
717            mutation.name,
718            total,
719            targets.join(", "),
720            alter_stmts.join("  "),
721        );
722    }
723}
724
725/// Build a PostgreSQL introspector connected to `db_url`.
726///
727/// Shared by `validate_indexed_columns` and the native column validation path.
728///
729/// # Errors
730///
731/// Returns error if the pool cannot be created or the connection URL is invalid.
732fn build_postgres_introspector(
733    db_url: &str,
734) -> Result<fraiseql_core::db::postgres::PostgresIntrospector> {
735    use deadpool_postgres::{Config, ManagerConfig, RecyclingMethod, Runtime};
736    use tokio_postgres::NoTls;
737
738    let mut cfg = Config::new();
739    cfg.url = Some(db_url.to_string());
740    cfg.manager = Some(ManagerConfig {
741        recycling_method: RecyclingMethod::Fast,
742    });
743    cfg.pool = Some(deadpool_postgres::PoolConfig::new(2));
744
745    let pool = cfg
746        .create_pool(Some(Runtime::Tokio1), NoTls)
747        .context("Failed to create connection pool for database validation")?;
748
749    Ok(fraiseql_core::db::postgres::PostgresIntrospector::new(pool))
750}
751
752/// Validate indexed columns against database views.
753///
754/// Connects to the database and introspects view columns to verify that
755/// any indexed column naming conventions are properly set up.
756///
757/// # Arguments
758///
759/// * `schema` - The compiled schema to validate
760/// * `db_url` - Database connection URL
761///
762/// # Errors
763///
764/// Returns error if database connection fails. Warnings are printed for
765/// missing indexed columns but don't cause validation to fail.
766async fn validate_indexed_columns(schema: &CompiledSchema, db_url: &str) -> Result<()> {
767    use deadpool_postgres::{Config, ManagerConfig, RecyclingMethod, Runtime};
768    use fraiseql_core::db::postgres::PostgresIntrospector;
769    use tokio_postgres::NoTls;
770
771    // Create pool for introspection
772    let mut cfg = Config::new();
773    cfg.url = Some(db_url.to_string());
774    cfg.manager = Some(ManagerConfig {
775        recycling_method: RecyclingMethod::Fast,
776    });
777    cfg.pool = Some(deadpool_postgres::PoolConfig::new(2));
778
779    let pool = cfg
780        .create_pool(Some(Runtime::Tokio1), NoTls)
781        .context("Failed to create connection pool for indexed column validation")?;
782
783    let introspector = PostgresIntrospector::new(pool);
784
785    let mut total_indexed = 0;
786    let mut total_views = 0;
787
788    // Check each query's sql_source (view)
789    for query in &schema.queries {
790        if let Some(view_name) = &query.sql_source {
791            total_views += 1;
792
793            // Get indexed columns for this view
794            match introspector.get_indexed_nested_columns(view_name).await {
795                Ok(indexed_cols) => {
796                    if !indexed_cols.is_empty() {
797                        info!(
798                            "View '{}': found {} indexed column(s): {:?}",
799                            view_name,
800                            indexed_cols.len(),
801                            indexed_cols
802                        );
803                        total_indexed += indexed_cols.len();
804                    }
805                },
806                Err(e) => {
807                    warn!(
808                        "Could not introspect view '{}': {}. Skipping indexed column check.",
809                        view_name, e
810                    );
811                },
812            }
813        }
814    }
815
816    println!("✓ Indexed column validation complete");
817    println!("  Views checked: {total_views}");
818    println!("  Indexed columns found: {total_indexed}");
819
820    Ok(())
821}
822
823/// Auto-param names excluded from `native_columns` inference and JSONB-extraction warnings.
824const NATIVE_COLUMN_SKIP_ARGS: &[&str] = &[
825    "where", "limit", "offset", "orderBy", "first", "last", "after", "before",
826];
827
828/// Infer `native_columns` for `ID`/`UUID`-typed arguments on JSONB-backed queries.
829///
830/// When a query reads from a JSONB table (`sql_source` + non-empty `jsonb_column`) and an
831/// argument is typed [`FieldType::Id`] or [`FieldType::Uuid`], the argument name almost
832/// certainly maps to a native UUID column alongside the `data` JSONB column
833/// (e.g. `id UUID NOT NULL`). Emitting `WHERE id = $1::uuid` instead of
834/// `WHERE data->>'id' = $1` lets the planner use the B-tree index without
835/// needing a database connection at compile time.
836///
837/// Auto-param names (`where`, `limit`, `offset`, etc.) are skipped.
838/// Arguments already present in `native_columns` are not overridden.
839pub(crate) fn infer_native_columns_from_arg_types(schema: &mut CompiledSchema) {
840    for query in &mut schema.queries {
841        if query.sql_source.is_none() || query.jsonb_column.is_empty() {
842            continue;
843        }
844        for arg in &query.arguments {
845            if NATIVE_COLUMN_SKIP_ARGS.contains(&arg.name.as_str()) {
846                continue;
847            }
848            if query.native_columns.contains_key(&arg.name) {
849                continue; // already explicitly declared — don't override
850            }
851            if matches!(arg.arg_type, FieldType::Id | FieldType::Uuid) {
852                query.native_columns.insert(arg.name.clone(), "uuid".to_string());
853            }
854        }
855    }
856}