sqlmodel-schema 0.2.2

Schema definition and migration support for SQLModel Rust
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
//! DDL (Data Definition Language) generation from schema operations.
//!
//! This module converts `SchemaOperation`s from the diff engine into executable
//! SQL statements for each supported database dialect (SQLite, MySQL, PostgreSQL).

mod mysql;
mod postgres;
mod sqlite;

pub use mysql::MysqlDdlGenerator;
pub use postgres::PostgresDdlGenerator;
pub use sqlite::SqliteDdlGenerator;

use crate::diff::SchemaOperation;
use crate::introspect::{
    ColumnInfo, Dialect, ForeignKeyInfo, IndexInfo, TableInfo, UniqueConstraintInfo,
};

/// Generates DDL SQL statements from schema operations.
pub trait DdlGenerator {
    /// The database dialect name.
    fn dialect(&self) -> &'static str;

    /// Generate DDL statement(s) for a single schema operation.
    ///
    /// Some operations (like SQLite DROP COLUMN) may produce multiple statements.
    fn generate(&self, op: &SchemaOperation) -> Vec<String>;

    /// Generate DDL statements for multiple operations.
    fn generate_all(&self, ops: &[SchemaOperation]) -> Vec<String> {
        ops.iter().flat_map(|op| self.generate(op)).collect()
    }

    /// Generate rollback DDL statements (inverse operations).
    ///
    /// Returns statements in reverse order, suitable for undoing the original operations.
    /// Operations without an inverse (like DROP TABLE) are skipped.
    fn generate_rollback(&self, ops: &[SchemaOperation]) -> Vec<String> {
        ops.iter()
            .rev()
            .filter_map(|op| op.inverse())
            .flat_map(|op| self.generate(&op))
            .collect()
    }
}

/// Create a DDL generator for the given dialect.
pub fn generator_for_dialect(dialect: Dialect) -> Box<dyn DdlGenerator> {
    match dialect {
        Dialect::Sqlite => Box::new(SqliteDdlGenerator),
        Dialect::Mysql => Box::new(MysqlDdlGenerator),
        Dialect::Postgres => Box::new(PostgresDdlGenerator),
    }
}

// ============================================================================
// Shared Helpers
// ============================================================================

/// Quote an identifier (table/column name) for SQL.
///
/// Different dialects use different quote characters:
/// - SQLite/PostgreSQL: double quotes
/// - MySQL: backticks
fn quote_identifier(name: &str, dialect: Dialect) -> String {
    match dialect {
        Dialect::Mysql => format!("`{}`", name.replace('`', "``")),
        Dialect::Sqlite | Dialect::Postgres => format!("\"{}\"", name.replace('"', "\"\"")),
    }
}

/// Format a column definition for CREATE TABLE or ADD COLUMN.
fn format_column_def(col: &ColumnInfo, dialect: Dialect) -> String {
    let mut parts = vec![quote_identifier(&col.name, dialect), col.sql_type.clone()];

    if !col.nullable {
        parts.push("NOT NULL".to_string());
    }

    if let Some(ref default) = col.default {
        parts.push(format!("DEFAULT {}", default));
    }

    // Auto-increment handling varies by dialect
    match dialect {
        Dialect::Sqlite => {
            // SQLite: INTEGER PRIMARY KEY implies AUTOINCREMENT
            // Explicit AUTOINCREMENT keyword is rarely needed
        }
        Dialect::Mysql => {
            if col.auto_increment {
                parts.push("AUTO_INCREMENT".to_string());
            }
        }
        Dialect::Postgres => {
            // PostgreSQL uses SERIAL types or GENERATED AS IDENTITY
            // The sql_type should already contain SERIAL/BIGSERIAL if auto-increment
        }
    }

    parts.join(" ")
}

/// Format the ON DELETE/UPDATE action for foreign keys.
fn format_referential_action(action: Option<&String>) -> &str {
    match action.map(|s| s.to_uppercase()).as_deref() {
        Some("CASCADE") => "CASCADE",
        Some("SET NULL") => "SET NULL",
        Some("SET DEFAULT") => "SET DEFAULT",
        Some("RESTRICT") => "RESTRICT",
        _ => "NO ACTION",
    }
}

/// Format a foreign key constraint clause.
fn format_fk_constraint(fk: &ForeignKeyInfo, dialect: Dialect) -> String {
    let mut sql = format!(
        "FOREIGN KEY ({}) REFERENCES {}({})",
        quote_identifier(&fk.column, dialect),
        quote_identifier(&fk.foreign_table, dialect),
        quote_identifier(&fk.foreign_column, dialect),
    );

    let on_delete = format_referential_action(fk.on_delete.as_ref());
    let on_update = format_referential_action(fk.on_update.as_ref());

    if on_delete != "NO ACTION" {
        sql.push_str(&format!(" ON DELETE {}", on_delete));
    }
    if on_update != "NO ACTION" {
        sql.push_str(&format!(" ON UPDATE {}", on_update));
    }

    sql
}

/// Format a unique constraint clause.
fn format_unique_constraint(unique: &UniqueConstraintInfo, dialect: Dialect) -> String {
    let cols: Vec<String> = unique
        .columns
        .iter()
        .map(|c| quote_identifier(c, dialect))
        .collect();

    if let Some(ref name) = unique.name {
        format!(
            "CONSTRAINT {} UNIQUE ({})",
            quote_identifier(name, dialect),
            cols.join(", ")
        )
    } else {
        format!("UNIQUE ({})", cols.join(", "))
    }
}

/// Generate CREATE TABLE SQL with configurable `IF NOT EXISTS`.
///
/// Kept private to `ddl` and its submodules (SQLite drop-column needs a
/// strict create without IF NOT EXISTS for table recreation).
fn generate_create_table_with_if_not_exists(
    table: &TableInfo,
    dialect: Dialect,
    if_not_exists: bool,
) -> String {
    tracing::debug!(
        dialect = %match dialect {
            Dialect::Sqlite => "sqlite",
            Dialect::Mysql => "mysql",
            Dialect::Postgres => "postgres",
        },
        table = %table.name,
        columns = table.columns.len(),
        "Generating CREATE TABLE DDL"
    );

    let mut parts = Vec::new();

    // Column definitions
    for col in &table.columns {
        parts.push(format!("  {}", format_column_def(col, dialect)));
    }

    // Primary key constraint (if not embedded in column definition)
    if !table.primary_key.is_empty() {
        let pk_cols: Vec<String> = table
            .primary_key
            .iter()
            .map(|c| quote_identifier(c, dialect))
            .collect();
        parts.push(format!("  PRIMARY KEY ({})", pk_cols.join(", ")));
    }

    // Unique constraints
    for unique in &table.unique_constraints {
        parts.push(format!("  {}", format_unique_constraint(unique, dialect)));
    }

    // Foreign key constraints
    for fk in &table.foreign_keys {
        parts.push(format!("  {}", format_fk_constraint(fk, dialect)));
    }

    let table_name = quote_identifier(&table.name, dialect);
    let ine = if if_not_exists { " IF NOT EXISTS" } else { "" };
    let sql = format!(
        "CREATE TABLE{} {} (\n{}\n)",
        ine,
        table_name,
        parts.join(",\n")
    );

    tracing::trace!(sql = %sql, "Generated CREATE TABLE statement");
    sql
}

/// Generate CREATE TABLE SQL (defaulting to `IF NOT EXISTS`).
fn generate_create_table(table: &TableInfo, dialect: Dialect) -> String {
    generate_create_table_with_if_not_exists(table, dialect, true)
}

/// Generate DROP TABLE SQL.
fn generate_drop_table(table_name: &str, dialect: Dialect) -> String {
    tracing::debug!(table = %table_name, "Generating DROP TABLE DDL");
    format!(
        "DROP TABLE IF EXISTS {}",
        quote_identifier(table_name, dialect)
    )
}

/// Generate RENAME TABLE SQL.
fn generate_rename_table(from: &str, to: &str, dialect: Dialect) -> String {
    tracing::debug!(from = %from, to = %to, "Generating RENAME TABLE DDL");
    match dialect {
        Dialect::Sqlite => format!(
            "ALTER TABLE {} RENAME TO {}",
            quote_identifier(from, dialect),
            quote_identifier(to, dialect)
        ),
        Dialect::Mysql => format!(
            "RENAME TABLE {} TO {}",
            quote_identifier(from, dialect),
            quote_identifier(to, dialect)
        ),
        Dialect::Postgres => format!(
            "ALTER TABLE {} RENAME TO {}",
            quote_identifier(from, dialect),
            quote_identifier(to, dialect)
        ),
    }
}

/// Generate ADD COLUMN SQL.
fn generate_add_column(table: &str, column: &ColumnInfo, dialect: Dialect) -> String {
    tracing::debug!(table = %table, column = %column.name, "Generating ADD COLUMN DDL");
    format!(
        "ALTER TABLE {} ADD COLUMN {}",
        quote_identifier(table, dialect),
        format_column_def(column, dialect)
    )
}

/// Generate RENAME COLUMN SQL.
fn generate_rename_column(table: &str, from: &str, to: &str, dialect: Dialect) -> String {
    tracing::debug!(table = %table, from = %from, to = %to, "Generating RENAME COLUMN DDL");
    match dialect {
        Dialect::Sqlite => {
            // SQLite 3.25.0+ supports RENAME COLUMN
            format!(
                "ALTER TABLE {} RENAME COLUMN {} TO {}",
                quote_identifier(table, dialect),
                quote_identifier(from, dialect),
                quote_identifier(to, dialect)
            )
        }
        Dialect::Mysql => format!(
            "ALTER TABLE {} RENAME COLUMN {} TO {}",
            quote_identifier(table, dialect),
            quote_identifier(from, dialect),
            quote_identifier(to, dialect)
        ),
        Dialect::Postgres => format!(
            "ALTER TABLE {} RENAME COLUMN {} TO {}",
            quote_identifier(table, dialect),
            quote_identifier(from, dialect),
            quote_identifier(to, dialect)
        ),
    }
}

/// Generate CREATE INDEX SQL.
fn generate_create_index(table: &str, index: &IndexInfo, dialect: Dialect) -> String {
    tracing::debug!(
        table = %table,
        index = %index.name,
        columns = ?index.columns,
        unique = index.unique,
        "Generating CREATE INDEX DDL"
    );

    let unique = if index.unique { "UNIQUE " } else { "" };
    let cols: Vec<String> = index
        .columns
        .iter()
        .map(|c| quote_identifier(c, dialect))
        .collect();

    // Include index type for databases that support it
    let using = match dialect {
        Dialect::Postgres => {
            if let Some(ref idx_type) = index.index_type {
                format!(" USING {}", idx_type)
            } else {
                String::new()
            }
        }
        Dialect::Mysql => {
            if let Some(ref idx_type) = index.index_type {
                if idx_type.eq_ignore_ascii_case("BTREE") {
                    String::new()
                } else {
                    format!(" USING {}", idx_type)
                }
            } else {
                String::new()
            }
        }
        Dialect::Sqlite => String::new(),
    };

    format!(
        "CREATE {}INDEX {} ON {}{}({})",
        unique,
        quote_identifier(&index.name, dialect),
        quote_identifier(table, dialect),
        using,
        cols.join(", ")
    )
}

/// Generate DROP INDEX SQL.
fn generate_drop_index(table: &str, index_name: &str, dialect: Dialect) -> String {
    tracing::debug!(table = %table, index = %index_name, "Generating DROP INDEX DDL");
    match dialect {
        Dialect::Sqlite => format!(
            "DROP INDEX IF EXISTS {}",
            quote_identifier(index_name, dialect)
        ),
        Dialect::Mysql => format!(
            "DROP INDEX {} ON {}",
            quote_identifier(index_name, dialect),
            quote_identifier(table, dialect)
        ),
        Dialect::Postgres => format!(
            "DROP INDEX IF EXISTS {}",
            quote_identifier(index_name, dialect)
        ),
    }
}

// ============================================================================
// Unit Tests
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;
    use crate::introspect::ParsedSqlType;

    fn make_column(name: &str, sql_type: &str, nullable: bool) -> ColumnInfo {
        ColumnInfo {
            name: name.to_string(),
            sql_type: sql_type.to_string(),
            parsed_type: ParsedSqlType::parse(sql_type),
            nullable,
            default: None,
            primary_key: false,
            auto_increment: false,
            comment: None,
        }
    }

    fn make_table(name: &str, columns: Vec<ColumnInfo>, pk: Vec<&str>) -> TableInfo {
        TableInfo {
            name: name.to_string(),
            columns,
            primary_key: pk.into_iter().map(String::from).collect(),
            foreign_keys: Vec::new(),
            unique_constraints: Vec::new(),
            check_constraints: Vec::new(),
            indexes: Vec::new(),
            comment: None,
        }
    }

    #[test]
    fn test_quote_identifier_sqlite() {
        assert_eq!(quote_identifier("name", Dialect::Sqlite), "\"name\"");
        assert_eq!(quote_identifier("table", Dialect::Sqlite), "\"table\"");
        assert_eq!(
            quote_identifier("col\"name", Dialect::Sqlite),
            "\"col\"\"name\""
        );
    }

    #[test]
    fn test_quote_identifier_mysql() {
        assert_eq!(quote_identifier("name", Dialect::Mysql), "`name`");
        assert_eq!(quote_identifier("table", Dialect::Mysql), "`table`");
        assert_eq!(quote_identifier("col`name", Dialect::Mysql), "`col``name`");
    }

    #[test]
    fn test_format_column_def_basic() {
        let col = make_column("name", "TEXT", false);
        let def = format_column_def(&col, Dialect::Sqlite);
        assert!(def.contains("\"name\""));
        assert!(def.contains("TEXT"));
        assert!(def.contains("NOT NULL"));
    }

    #[test]
    fn test_format_column_def_nullable() {
        let col = make_column("name", "TEXT", true);
        let def = format_column_def(&col, Dialect::Sqlite);
        assert!(!def.contains("NOT NULL"));
    }

    #[test]
    fn test_format_column_def_with_default() {
        let mut col = make_column("status", "TEXT", false);
        col.default = Some("'active'".to_string());
        let def = format_column_def(&col, Dialect::Sqlite);
        assert!(def.contains("DEFAULT 'active'"));
    }

    #[test]
    fn test_format_column_def_auto_increment_mysql() {
        let mut col = make_column("id", "INT", false);
        col.auto_increment = true;
        let def = format_column_def(&col, Dialect::Mysql);
        assert!(def.contains("AUTO_INCREMENT"));
    }

    #[test]
    fn test_generate_create_table_basic() {
        let table = make_table(
            "heroes",
            vec![
                make_column("id", "INTEGER", false),
                make_column("name", "TEXT", false),
            ],
            vec!["id"],
        );
        let sql = generate_create_table(&table, Dialect::Sqlite);
        assert!(sql.contains("CREATE TABLE IF NOT EXISTS"));
        assert!(sql.contains("\"heroes\""));
        assert!(sql.contains("\"id\""));
        assert!(sql.contains("\"name\""));
        assert!(sql.contains("PRIMARY KEY"));
    }

    #[test]
    fn test_generate_create_table_with_fk() {
        let mut table = make_table(
            "heroes",
            vec![
                make_column("id", "INTEGER", false),
                make_column("team_id", "INTEGER", true),
            ],
            vec!["id"],
        );
        table.foreign_keys.push(ForeignKeyInfo {
            name: Some("fk_heroes_team".to_string()),
            column: "team_id".to_string(),
            foreign_table: "teams".to_string(),
            foreign_column: "id".to_string(),
            on_delete: Some("CASCADE".to_string()),
            on_update: None,
        });

        let sql = generate_create_table(&table, Dialect::Sqlite);
        assert!(sql.contains("FOREIGN KEY"));
        assert!(sql.contains("REFERENCES"));
        assert!(sql.contains("ON DELETE CASCADE"));
    }

    #[test]
    fn test_generate_drop_table() {
        let sql = generate_drop_table("heroes", Dialect::Sqlite);
        assert_eq!(sql, "DROP TABLE IF EXISTS \"heroes\"");
    }

    #[test]
    fn test_generate_rename_table_sqlite() {
        let sql = generate_rename_table("old_name", "new_name", Dialect::Sqlite);
        assert!(sql.contains("ALTER TABLE"));
        assert!(sql.contains("RENAME TO"));
    }

    #[test]
    fn test_generate_rename_table_mysql() {
        let sql = generate_rename_table("old_name", "new_name", Dialect::Mysql);
        assert!(sql.contains("RENAME TABLE"));
    }

    #[test]
    fn test_generate_add_column() {
        let col = make_column("age", "INTEGER", true);
        let sql = generate_add_column("heroes", &col, Dialect::Sqlite);
        assert!(sql.contains("ALTER TABLE"));
        assert!(sql.contains("ADD COLUMN"));
        assert!(sql.contains("\"age\""));
    }

    #[test]
    fn test_generate_rename_column() {
        let sql = generate_rename_column("heroes", "old_name", "new_name", Dialect::Postgres);
        assert!(sql.contains("ALTER TABLE"));
        assert!(sql.contains("RENAME COLUMN"));
    }

    #[test]
    fn test_generate_create_index() {
        let index = IndexInfo {
            name: "idx_heroes_name".to_string(),
            columns: vec!["name".to_string()],
            unique: false,
            index_type: None,
            primary: false,
        };
        let sql = generate_create_index("heroes", &index, Dialect::Sqlite);
        assert!(sql.contains("CREATE INDEX"));
        assert!(sql.contains("\"idx_heroes_name\""));
        assert!(sql.contains("ON \"heroes\""));
    }

    #[test]
    fn test_generate_create_unique_index() {
        let index = IndexInfo {
            name: "idx_heroes_name_unique".to_string(),
            columns: vec!["name".to_string()],
            unique: true,
            index_type: None,
            primary: false,
        };
        let sql = generate_create_index("heroes", &index, Dialect::Sqlite);
        assert!(sql.contains("CREATE UNIQUE INDEX"));
    }

    #[test]
    fn test_generate_drop_index_sqlite() {
        let sql = generate_drop_index("heroes", "idx_heroes_name", Dialect::Sqlite);
        assert_eq!(sql, "DROP INDEX IF EXISTS \"idx_heroes_name\"");
    }

    #[test]
    fn test_generate_drop_index_mysql() {
        let sql = generate_drop_index("heroes", "idx_heroes_name", Dialect::Mysql);
        assert!(sql.contains("DROP INDEX"));
        assert!(sql.contains("ON `heroes`"));
    }

    #[test]
    fn test_generator_for_dialect() {
        let sqlite = generator_for_dialect(Dialect::Sqlite);
        assert_eq!(sqlite.dialect(), "sqlite");

        let mysql = generator_for_dialect(Dialect::Mysql);
        assert_eq!(mysql.dialect(), "mysql");

        let postgres = generator_for_dialect(Dialect::Postgres);
        assert_eq!(postgres.dialect(), "postgres");
    }

    #[test]
    fn test_referential_action_formatting() {
        assert_eq!(
            format_referential_action(Some(&"CASCADE".to_string())),
            "CASCADE"
        );
        assert_eq!(
            format_referential_action(Some(&"cascade".to_string())),
            "CASCADE"
        );
        assert_eq!(
            format_referential_action(Some(&"SET NULL".to_string())),
            "SET NULL"
        );
        assert_eq!(format_referential_action(None), "NO ACTION");
    }
}