fraiseql-cli 2.0.0-rc.13

CLI tools for FraiseQL v2 - Schema compilation and development utilities
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
//! Validate that declared fact tables match database schema.
//!
//! This command checks:
//! - Declared fact tables exist in database
//! - Metadata structure matches actual database schema
//! - Warns about undeclared tf_* tables
//!
//! **Purpose**: CI/CD validation step to catch schema drift.

use std::{collections::HashSet, fs, path::Path};

use anyhow::Result;
use deadpool_postgres::{Config, ManagerConfig, RecyclingMethod, Runtime};
use fraiseql_core::{
    compiler::{
        fact_table::{DatabaseIntrospector, FactTableDetector, FactTableMetadata},
        ir::AuthoringIR,
        parser::SchemaParser,
    },
    db::PostgresIntrospector,
};
use tokio_postgres::NoTls;

/// Validation error type.
#[derive(Debug)]
pub struct ValidationIssue {
    /// Issue type (error or warning)
    pub severity:   IssueSeverity,
    /// Fact table name
    pub table_name: String,
    /// Issue description
    pub message:    String,
}

/// Issue severity level.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum IssueSeverity {
    /// Critical error - validation fails
    Error,
    /// Warning - validation passes with warnings
    Warning,
}

impl ValidationIssue {
    /// Create a new error issue.
    pub const fn error(table_name: String, message: String) -> Self {
        Self {
            severity: IssueSeverity::Error,
            table_name,
            message,
        }
    }

    /// Create a new warning issue.
    pub const fn warning(table_name: String, message: String) -> Self {
        Self {
            severity: IssueSeverity::Warning,
            table_name,
            message,
        }
    }
}

/// Create a PostgreSQL introspector from a database URL
async fn create_introspector(database_url: &str) -> Result<PostgresIntrospector> {
    let mut cfg = Config::new();
    cfg.url = Some(database_url.to_string());
    cfg.manager = Some(ManagerConfig {
        recycling_method: RecyclingMethod::Fast,
    });
    cfg.pool = Some(deadpool_postgres::PoolConfig::new(2));

    let pool = cfg
        .create_pool(Some(Runtime::Tokio1), NoTls)
        .map_err(|e| anyhow::anyhow!("Failed to create database pool: {e}"))?;

    // Test connection
    let _client = pool
        .get()
        .await
        .map_err(|e| anyhow::anyhow!("Failed to connect to database: {e}"))?;

    Ok(PostgresIntrospector::new(pool))
}

/// Validate that declared fact tables match database schema.
///
/// # Arguments
///
/// * `schema_path` - Path to schema.json file
/// * `database_url` - Database connection string
///
/// # Returns
///
/// Success if all validations pass, error otherwise
///
/// # Example
///
/// ```bash
/// fraiseql validate facts --schema schema.json --database postgresql://localhost/mydb
/// ```
pub async fn run(schema_path: &Path, database_url: &str) -> Result<()> {
    eprintln!("🔍 Validating fact tables...");
    eprintln!("   Schema: {}", schema_path.display());
    eprintln!("   Database: {database_url}");
    eprintln!();

    // 1. Load and parse schema
    let schema_str = fs::read_to_string(schema_path)?;

    let parser = SchemaParser::new();
    let ir: AuthoringIR = parser.parse(&schema_str)?;

    let declared_tables: HashSet<String> = ir.fact_tables.keys().cloned().collect();

    eprintln!("📋 Found {} declared fact table(s) in schema", declared_tables.len());

    if declared_tables.is_empty() {
        eprintln!("   No fact tables declared - nothing to validate");
        eprintln!();
        eprintln!("💡 Tip: Use 'fraiseql introspect facts' to discover fact tables");
        return Ok(());
    }

    for table_name in &declared_tables {
        eprintln!("   - {table_name}");
    }
    eprintln!();

    // 2. Connect to database and list actual fact tables
    let introspector = create_introspector(database_url).await?;

    let actual_tables: HashSet<String> = introspector
        .list_fact_tables()
        .await
        .map_err(|e| anyhow::anyhow!("Failed to list fact tables: {e}"))?
        .into_iter()
        .collect();

    eprintln!("📊 Found {} fact table(s) in database", actual_tables.len());
    eprintln!();

    // 3. Validate each declared table
    let mut issues: Vec<ValidationIssue> = Vec::new();
    let mut validated_count = 0;

    for table_name in &declared_tables {
        eprintln!("   Validating {table_name}...");

        // Check if table exists in database
        if !actual_tables.contains(table_name) {
            issues.push(ValidationIssue::error(
                table_name.clone(),
                "Table does not exist in database".to_string(),
            ));
            continue;
        }

        // Introspect actual table structure
        match FactTableDetector::introspect(&introspector, table_name).await {
            Ok(actual_metadata) => {
                // Get declared metadata
                if let Some(declared_json) = ir.fact_tables.get(table_name) {
                    // Compare structures
                    let comparison_issues =
                        compare_metadata(table_name, declared_json, &actual_metadata);
                    issues.extend(comparison_issues);
                }
                validated_count += 1;
            },
            Err(e) => {
                issues.push(ValidationIssue::error(
                    table_name.clone(),
                    format!("Failed to introspect: {e}"),
                ));
            },
        }
    }

    // 4. Check for undeclared tables in database
    for table_name in &actual_tables {
        if !declared_tables.contains(table_name) {
            issues.push(ValidationIssue::warning(
                table_name.clone(),
                "Table exists in database but not declared in schema".to_string(),
            ));
        }
    }

    // 5. Report results
    eprintln!();
    let errors: Vec<&ValidationIssue> =
        issues.iter().filter(|i| i.severity == IssueSeverity::Error).collect();
    let warnings: Vec<&ValidationIssue> =
        issues.iter().filter(|i| i.severity == IssueSeverity::Warning).collect();

    if !errors.is_empty() {
        eprintln!("❌ Errors ({}):", errors.len());
        for issue in &errors {
            eprintln!("   {} - {}", issue.table_name, issue.message);
        }
        eprintln!();
    }

    if !warnings.is_empty() {
        eprintln!("⚠️  Warnings ({}):", warnings.len());
        for issue in &warnings {
            eprintln!("   {} - {}", issue.table_name, issue.message);
        }
        eprintln!();
    }

    if errors.is_empty() {
        eprintln!("✅ Validation passed");
        eprintln!("   {validated_count} table(s) validated successfully");
        if !warnings.is_empty() {
            eprintln!("   {} warning(s)", warnings.len());
        }
        Ok(())
    } else {
        Err(anyhow::anyhow!("Validation failed with {} error(s)", errors.len()))
    }
}

/// Compare declared metadata with actual database metadata
fn compare_metadata(
    table_name: &str,
    declared: &serde_json::Value,
    actual: &FactTableMetadata,
) -> Vec<ValidationIssue> {
    let mut issues = Vec::new();

    // Extract declared measures
    if let Some(declared_measures) = declared.get("measures").and_then(|m| m.as_array()) {
        let declared_measure_names: HashSet<String> = declared_measures
            .iter()
            .filter_map(|m| m.get("name").and_then(|n| n.as_str()))
            .map(String::from)
            .collect();

        let actual_measure_names: HashSet<String> =
            actual.measures.iter().map(|m| m.name.clone()).collect();

        // Check for missing measures in actual
        for name in &declared_measure_names {
            if !actual_measure_names.contains(name) {
                issues.push(ValidationIssue::error(
                    table_name.to_string(),
                    format!("Declared measure '{name}' not found in database"),
                ));
            }
        }

        // Check for extra measures in actual (warning)
        for name in &actual_measure_names {
            if !declared_measure_names.contains(name) {
                issues.push(ValidationIssue::warning(
                    table_name.to_string(),
                    format!("Database has measure '{name}' not declared in schema"),
                ));
            }
        }

        // Validate measure types
        for declared_measure in declared_measures {
            if let (Some(name), Some(declared_type)) = (
                declared_measure.get("name").and_then(|n| n.as_str()),
                declared_measure.get("sql_type").and_then(|t| t.as_str()),
            ) {
                if let Some(actual_measure) = actual.measures.iter().find(|m| m.name == name) {
                    let actual_type_str = format!("{:?}", actual_measure.sql_type);
                    if !types_compatible(declared_type, &actual_type_str) {
                        issues.push(ValidationIssue::warning(
                            table_name.to_string(),
                            format!(
                                "Measure '{name}' type mismatch: declared '{declared_type}', actual '{actual_type_str}'"
                            ),
                        ));
                    }
                }
            }
        }
    }

    // Validate dimensions column
    if let Some(declared_dims) = declared.get("dimensions") {
        if let Some(declared_name) = declared_dims.get("name").and_then(|n| n.as_str()) {
            if declared_name != actual.dimensions.name {
                issues.push(ValidationIssue::error(
                    table_name.to_string(),
                    format!(
                        "Dimensions column mismatch: declared '{}', actual '{}'",
                        declared_name, actual.dimensions.name
                    ),
                ));
            }
        }
    }

    // Validate denormalized filters
    if let Some(declared_filters) = declared.get("denormalized_filters").and_then(|f| f.as_array())
    {
        let declared_filter_names: HashSet<String> = declared_filters
            .iter()
            .filter_map(|f| f.get("name").and_then(|n| n.as_str()))
            .map(String::from)
            .collect();

        let actual_filter_names: HashSet<String> =
            actual.denormalized_filters.iter().map(|f| f.name.clone()).collect();

        for name in &declared_filter_names {
            if !actual_filter_names.contains(name) {
                issues.push(ValidationIssue::warning(
                    table_name.to_string(),
                    format!("Declared filter '{name}' not found in database"),
                ));
            }
        }
    }

    issues
}

/// Check if two SQL types are compatible
fn types_compatible(declared: &str, actual: &str) -> bool {
    let declared_lower = declared.to_lowercase();
    let actual_lower = actual.to_lowercase();

    // Exact match
    if declared_lower == actual_lower {
        return true;
    }

    // Common aliases
    let aliases: &[(&[&str], &[&str])] = &[
        (&["int", "integer", "int4"], &["int", "integer", "int4"]),
        (&["bigint", "int8"], &["bigint", "int8"]),
        (&["decimal", "numeric", "money"], &["decimal", "numeric", "money"]),
        (&["float", "double", "real", "float8"], &["float", "double", "real", "float8"]),
        (&["text", "varchar", "string"], &["text", "varchar", "string"]),
        (&["uuid"], &["uuid"]),
        (
            &["timestamp", "timestamptz", "datetime"],
            &["timestamp", "timestamptz", "datetime"],
        ),
        (&["json", "jsonb"], &["json", "jsonb"]),
        (&["bool", "boolean"], &["bool", "boolean"]),
    ];

    for (group1, group2) in aliases {
        let in_group1 = group1.iter().any(|t| declared_lower.contains(t));
        let in_group2 = group2.iter().any(|t| actual_lower.contains(t));
        if in_group1 && in_group2 {
            return true;
        }
    }

    false
}

#[cfg(test)]
mod tests {
    use fraiseql_core::compiler::fact_table::{
        DimensionColumn, FilterColumn, MeasureColumn, SqlType,
    };

    use super::*;

    #[test]
    fn test_validation_issue_error() {
        let issue = ValidationIssue::error("tf_sales".to_string(), "Table not found".to_string());
        assert_eq!(issue.severity, IssueSeverity::Error);
        assert_eq!(issue.table_name, "tf_sales");
    }

    #[test]
    fn test_validation_issue_warning() {
        let issue = ValidationIssue::warning(
            "tf_orders".to_string(),
            "Table exists but not declared".to_string(),
        );
        assert_eq!(issue.severity, IssueSeverity::Warning);
    }

    #[test]
    fn test_types_compatible() {
        // Exact match
        assert!(types_compatible("Int", "Int"));
        assert!(types_compatible("Decimal", "Decimal"));

        // Aliases
        assert!(types_compatible("integer", "Int"));
        assert!(types_compatible("int4", "Int"));
        assert!(types_compatible("bigint", "BigInt"));
        assert!(types_compatible("numeric", "Decimal"));
        assert!(types_compatible("float", "Float"));
        assert!(types_compatible("double", "Float"));
        assert!(types_compatible("text", "Text"));
        assert!(types_compatible("varchar", "Text"));

        // Incompatible
        assert!(!types_compatible("Int", "Text"));
        assert!(!types_compatible("Decimal", "Boolean"));
    }

    #[test]
    fn test_compare_metadata_matching() {
        let declared = serde_json::json!({
            "measures": [
                {"name": "revenue", "sql_type": "Decimal"},
                {"name": "quantity", "sql_type": "Int"}
            ],
            "dimensions": {"name": "data"},
            "denormalized_filters": [
                {"name": "customer_id"}
            ]
        });

        let actual = FactTableMetadata {
            table_name:           "tf_sales".to_string(),
            measures:             vec![
                MeasureColumn {
                    name:     "revenue".to_string(),
                    sql_type: SqlType::Decimal,
                    nullable: false,
                },
                MeasureColumn {
                    name:     "quantity".to_string(),
                    sql_type: SqlType::Int,
                    nullable: false,
                },
            ],
            dimensions:           DimensionColumn {
                name:  "data".to_string(),
                paths: vec![],
            },
            denormalized_filters: vec![FilterColumn {
                name:     "customer_id".to_string(),
                sql_type: SqlType::Uuid,
                indexed:  true,
            }],
            calendar_dimensions:  vec![],
        };

        let issues = compare_metadata("tf_sales", &declared, &actual);

        // No errors expected for matching metadata
        let errors: Vec<_> = issues.iter().filter(|i| i.severity == IssueSeverity::Error).collect();
        assert!(errors.is_empty(), "Unexpected errors: {errors:?}");
    }

    #[test]
    fn test_compare_metadata_missing_measure() {
        let declared = serde_json::json!({
            "measures": [
                {"name": "revenue", "sql_type": "Decimal"},
                {"name": "profit", "sql_type": "Decimal"}  // Not in actual
            ],
            "dimensions": {"name": "data"}
        });

        let actual = FactTableMetadata {
            table_name:           "tf_sales".to_string(),
            measures:             vec![MeasureColumn {
                name:     "revenue".to_string(),
                sql_type: SqlType::Decimal,
                nullable: false,
            }],
            dimensions:           DimensionColumn {
                name:  "data".to_string(),
                paths: vec![],
            },
            denormalized_filters: vec![],
            calendar_dimensions:  vec![],
        };

        let issues = compare_metadata("tf_sales", &declared, &actual);

        // Should have error for missing 'profit' measure
        let errors: Vec<_> = issues.iter().filter(|i| i.severity == IssueSeverity::Error).collect();
        assert_eq!(errors.len(), 1);
        assert!(errors[0].message.contains("profit"));
    }
}