sql-splitter 1.17.0

High-performance CLI tool for splitting large SQL dump files into individual table files
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
//! Normalized `CREATE TABLE` rendering for the synthetic-data renderer.
//!
//! [`render_create_table`] renders a [`PortableTable`] from scratch: columns,
//! primary key, unique constraints, check constraints, and (as a following
//! `ALTER TABLE`, so a child table can reference an already-created parent)
//! foreign keys. [`should_preserve_raw_ddl`] decides when the caller can skip
//! this and reuse the table's checked-in `create_statement` instead: only
//! when the render target is the same dialect the DDL was captured in *and*
//! the table itself carries a `create_statement` to preserve. Excluded-object
//! DDL rewriting (a table selection that changes which columns/FKs a
//! same-dialect `create_statement` may still reference) belongs to the table
//! selection pipeline, not this normalized renderer.

use std::fmt::Write as _;

use crate::convert::map_column_type;
use crate::convert::WarningCollector;
use crate::parser::SqlDialect;
use crate::synthetic::schema::{PortableColumn, PortableTable};
use crate::transform_common::{quote_ident, quote_identifier};

/// Whether `table`'s raw `create_statement` may be emitted as-is: the render
/// target must match the dialect the DDL was captured in, and the table must
/// actually carry one (a hand-authored `kind: model` fixture usually will
/// not).
pub(crate) fn should_preserve_raw_ddl(
    table: &PortableTable,
    source_dialect: Option<SqlDialect>,
    target_dialect: SqlDialect,
) -> bool {
    table.create_statement.is_some() && source_dialect == Some(target_dialect)
}

/// The table identifier to render. Every component of a qualified name is
/// quoted separately, so `public.users` becomes `"public"."users"` rather
/// than one identifier named `public.users`. MSSQL production style supplies
/// `[dbo].` only for an unqualified name; an explicit schema remains
/// authoritative. A production-style request against a non-MSSQL render
/// target changes nothing.
pub(crate) fn qualified_table(
    dialect: SqlDialect,
    name: &str,
    mssql_production_style: bool,
) -> String {
    if mssql_production_style && dialect == SqlDialect::Mssql && !name.contains('.') {
        format!("[dbo].{}", quote_ident(dialect, name))
    } else {
        quote_ident(dialect, name)
    }
}

/// Render a normalized `CREATE TABLE` (plus a trailing `ALTER TABLE ADD
/// CONSTRAINT` per foreign key, and one `CREATE INDEX` per index) for
/// `table`, mapping every column's `source_type` from `from` to `to` via
/// [`map_column_type`]. Under `mssql_production_style` (MSSQL only): table
/// names are `[dbo].`-qualified, the primary key renders as a named
/// `CONSTRAINT [PK_<table>] PRIMARY KEY CLUSTERED` instead of an inline
/// `PRIMARY KEY`, and the `CREATE TABLE` closes with an `ON [PRIMARY]`
/// filegroup clause.
pub(crate) fn render_create_table(
    table: &PortableTable,
    from: SqlDialect,
    to: SqlDialect,
    warnings: &mut WarningCollector,
    mssql_production_style: bool,
) -> String {
    let mut sql = String::new();
    let quoted_table = qualified_table(to, &table.name, mssql_production_style);
    let mssql_production_style = mssql_production_style && to == SqlDialect::Mssql;

    let mut clauses: Vec<String> = Vec::with_capacity(
        table.columns.len()
            + usize::from(!table.primary_key.is_empty())
            + table.unique_constraints.len()
            + table.check_constraints.len(),
    );
    for column in &table.columns {
        let has_single_column_constraint = table.unique_constraints.iter().any(|constraint| {
            matches!(
                constraint.columns.as_slice(),
                [name] if name.eq_ignore_ascii_case(&column.name)
            )
        });
        clauses.push(render_column_def(
            column,
            from,
            to,
            warnings,
            column.unique && !has_single_column_constraint,
        ));
    }
    if !table.primary_key.is_empty() {
        let cols = join_idents(to, &table.primary_key);
        if mssql_production_style {
            let pk_name = quote_identifier(to, &format!("PK_{}", table.name));
            clauses.push(format!(
                "  CONSTRAINT {pk_name} PRIMARY KEY CLUSTERED ({cols})"
            ));
        } else {
            clauses.push(format!("  PRIMARY KEY ({cols})"));
        }
    }
    for unique in &table.unique_constraints {
        let cols = join_idents(to, &unique.columns);
        match &unique.name {
            Some(name) => clauses.push(format!(
                "  CONSTRAINT {} UNIQUE ({cols})",
                quote_identifier(to, name)
            )),
            None => clauses.push(format!("  UNIQUE ({cols})")),
        }
    }
    for check in &table.check_constraints {
        match &check.name {
            Some(name) => clauses.push(format!(
                "  CONSTRAINT {} CHECK ({})",
                quote_identifier(to, name),
                check.expression
            )),
            None => clauses.push(format!("  CHECK ({})", check.expression)),
        }
    }

    let _ = writeln!(sql, "CREATE TABLE {quoted_table} (");
    let _ = writeln!(sql, "{}", clauses.join(",\n"));
    if mssql_production_style {
        let _ = writeln!(sql, ") ON [PRIMARY];");
    } else {
        let _ = writeln!(sql, ");");
    }

    // Foreign keys are added after the CREATE TABLE (rather than inline) so a
    // child table can be created before every referenced parent exists in
    // the output stream and still reference it correctly once all tables in
    // dependency order have been emitted.
    for relationship in &table.relationships {
        let _ = writeln!(
            sql,
            "ALTER TABLE {quoted_table} ADD {}FOREIGN KEY ({}) REFERENCES {} ({});",
            match &relationship.name {
                Some(name) => format!("CONSTRAINT {} ", quote_identifier(to, name)),
                None => String::new(),
            },
            join_idents(to, &relationship.columns),
            qualified_table(to, &relationship.referenced_table, mssql_production_style),
            join_idents(to, &relationship.referenced_columns),
        );
    }

    for index in &table.indexes {
        let kind = if index.unique {
            "UNIQUE INDEX"
        } else {
            "INDEX"
        };
        let _ = writeln!(
            sql,
            "CREATE {kind} {} ON {quoted_table} ({});",
            quote_identifier(to, &index.name),
            join_idents(to, &index.columns),
        );
    }

    sql
}

/// Render one column definition: quoted name, mapped type, nullability,
/// raw `DEFAULT` expression (carried over verbatim; converting a default
/// expression's own dialect-specific syntax is out of scope here), and a
/// dialect-appropriate identity/auto-increment clause.
fn render_column_def(
    column: &PortableColumn,
    from: SqlDialect,
    to: SqlDialect,
    warnings: &mut WarningCollector,
    render_inline_unique: bool,
) -> String {
    let mapped_type = map_column_type(&column.source_type, from, to, warnings);
    let mut def = format!("  {} {mapped_type}", quote_identifier(to, &column.name),);
    if !column.nullable {
        def.push_str(" NOT NULL");
    }
    if render_inline_unique {
        def.push_str(" UNIQUE");
    }
    if let Some(default_sql) = &column.default_sql {
        let _ = write!(def, " DEFAULT {default_sql}");
    }
    if column.identity {
        def.push_str(identity_clause(to));
    }
    def
}

/// The dialect-specific clause marking a column as database-generated
/// (`IDENTITY`/`AUTO_INCREMENT`/`GENERATED ALWAYS AS IDENTITY`). SQLite has
/// no equivalent keyword (an `INTEGER PRIMARY KEY` column auto-increments
/// implicitly), so it renders nothing.
fn identity_clause(dialect: SqlDialect) -> &'static str {
    match dialect {
        SqlDialect::MySql => " AUTO_INCREMENT",
        SqlDialect::Postgres => " GENERATED ALWAYS AS IDENTITY",
        SqlDialect::Sqlite => "",
        SqlDialect::Mssql => " IDENTITY(1,1)",
    }
}

/// Quote and comma-join a list of column names for the given dialect.
fn join_idents(dialect: SqlDialect, names: &[String]) -> String {
    names
        .iter()
        .map(|name| quote_identifier(dialect, name))
        .collect::<Vec<_>>()
        .join(", ")
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::synthetic::schema::{PortableRelationship, PortableUniqueConstraint};
    use crate::synthetic::SqlTypeFamily;

    fn column(name: &str, source_type: &str, nullable: bool) -> PortableColumn {
        PortableColumn {
            name: name.to_string(),
            source_type: source_type.to_string(),
            family: SqlTypeFamily::Other,
            nullable,
            primary_key: false,
            unique: false,
            default_sql: None,
            generated: false,
            identity: false,
            collation: None,
        }
    }

    #[test]
    fn preserves_raw_ddl_only_when_dialect_matches_and_statement_exists() {
        let mut table = PortableTable {
            name: "t".into(),
            columns: vec![],
            primary_key: vec![],
            unique_constraints: vec![],
            check_constraints: vec![],
            indexes: vec![],
            create_statement: Some("CREATE TABLE t (id INT);".into()),
            relationships: vec![],
        };
        assert!(should_preserve_raw_ddl(
            &table,
            Some(SqlDialect::MySql),
            SqlDialect::MySql
        ));
        assert!(!should_preserve_raw_ddl(
            &table,
            Some(SqlDialect::MySql),
            SqlDialect::Postgres
        ));
        assert!(!should_preserve_raw_ddl(&table, None, SqlDialect::MySql));

        table.create_statement = None;
        assert!(!should_preserve_raw_ddl(
            &table,
            Some(SqlDialect::MySql),
            SqlDialect::MySql
        ));
    }

    #[test]
    fn renders_columns_primary_key_and_maps_types_cross_dialect() {
        let table = PortableTable {
            name: "orders".into(),
            columns: vec![
                column("id", "BIGINT", false),
                column("total", "DECIMAL(10,2)", false),
            ],
            primary_key: vec!["id".to_string()],
            unique_constraints: vec![],
            check_constraints: vec![],
            indexes: vec![],
            create_statement: None,
            relationships: vec![],
        };
        let mut warnings = WarningCollector::new();
        let sql = render_create_table(
            &table,
            SqlDialect::MySql,
            SqlDialect::Mssql,
            &mut warnings,
            false,
        );
        assert!(sql.starts_with("CREATE TABLE [orders] (\n"));
        assert!(sql.contains("[id] BIGINT NOT NULL"));
        assert!(sql.contains("PRIMARY KEY ([id])"));
    }

    #[test]
    fn mssql_production_style_qualifies_names_and_uses_a_named_clustered_pk() {
        let table = PortableTable {
            name: "orders".into(),
            columns: vec![column("id", "BIGINT", false)],
            primary_key: vec!["id".to_string()],
            unique_constraints: vec![],
            check_constraints: vec![],
            indexes: vec![],
            create_statement: None,
            relationships: vec![PortableRelationship {
                name: Some("fk_customer".into()),
                columns: vec!["id".to_string()],
                referenced_table: "customers".to_string(),
                referenced_columns: vec!["id".to_string()],
            }],
        };
        let mut warnings = WarningCollector::new();
        let sql = render_create_table(
            &table,
            SqlDialect::Mssql,
            SqlDialect::Mssql,
            &mut warnings,
            true,
        );
        assert!(sql.starts_with("CREATE TABLE [dbo].[orders] (\n"));
        assert!(sql.contains("CONSTRAINT [PK_orders] PRIMARY KEY CLUSTERED ([id])"));
        assert!(!sql.contains("  PRIMARY KEY ([id])"));
        assert!(sql.contains(") ON [PRIMARY];"));
        assert!(sql.contains("REFERENCES [dbo].[customers] ([id]);"));

        // Not MSSQL: production style has no effect on other dialects.
        let mut warnings = WarningCollector::new();
        let sql = render_create_table(
            &table,
            SqlDialect::Postgres,
            SqlDialect::Postgres,
            &mut warnings,
            true,
        );
        assert!(sql.starts_with("CREATE TABLE \"orders\" (\n"));
        assert!(sql.contains("  PRIMARY KEY (\"id\")"));
    }

    #[test]
    fn renders_each_component_of_qualified_table_and_fk_names() {
        let table = PortableTable {
            name: "tenant_a.orders".into(),
            columns: vec![column("customer_id", "BIGINT", false)],
            primary_key: vec![],
            unique_constraints: vec![],
            check_constraints: vec![],
            indexes: vec![],
            create_statement: None,
            relationships: vec![PortableRelationship {
                name: None,
                columns: vec!["customer_id".to_string()],
                referenced_table: "public.customers".to_string(),
                referenced_columns: vec!["id".to_string()],
            }],
        };
        let mut warnings = WarningCollector::new();
        let postgres = render_create_table(
            &table,
            SqlDialect::Postgres,
            SqlDialect::Postgres,
            &mut warnings,
            false,
        );
        assert!(postgres.starts_with("CREATE TABLE \"tenant_a\".\"orders\" (\n"));
        assert!(postgres.contains("REFERENCES \"public\".\"customers\" (\"id\")"));

        let mssql = qualified_table(SqlDialect::Mssql, "tenant_a.orders", true);
        assert_eq!(mssql, "[tenant_a].[orders]");
        assert_eq!(
            qualified_table(SqlDialect::Postgres, "public.\"user.log\"", false),
            "\"public\".\"user.log\""
        );
    }

    #[test]
    fn renders_unique_check_fk_and_index_clauses() {
        let mut code = column("code", "VARCHAR(16)", false);
        code.unique = true;
        let table = PortableTable {
            name: "orders".into(),
            columns: vec![column("customer_id", "BIGINT", false), code],
            primary_key: vec![],
            unique_constraints: vec![PortableUniqueConstraint {
                name: Some("uq_customer".into()),
                columns: vec!["customer_id".to_string()],
            }],
            check_constraints: vec![],
            indexes: vec![crate::synthetic::schema::PortableIndex {
                name: "idx_customer".into(),
                columns: vec!["customer_id".to_string()],
                unique: false,
                index_type: None,
            }],
            create_statement: None,
            relationships: vec![PortableRelationship {
                name: Some("fk_customer".into()),
                columns: vec!["customer_id".to_string()],
                referenced_table: "customers".to_string(),
                referenced_columns: vec!["id".to_string()],
            }],
        };
        let mut warnings = WarningCollector::new();
        let sql = render_create_table(
            &table,
            SqlDialect::Postgres,
            SqlDialect::Postgres,
            &mut warnings,
            false,
        );
        assert!(sql.contains("CONSTRAINT \"uq_customer\" UNIQUE (\"customer_id\")"));
        assert!(sql.contains("\"code\" VARCHAR(16) NOT NULL UNIQUE"));
        assert!(!sql.contains("\"customer_id\" BIGINT NOT NULL UNIQUE"));
        assert!(sql.contains(
            "ALTER TABLE \"orders\" ADD CONSTRAINT \"fk_customer\" FOREIGN KEY (\"customer_id\") REFERENCES \"customers\" (\"id\");"
        ));
        assert!(sql.contains("CREATE INDEX \"idx_customer\" ON \"orders\" (\"customer_id\");"));
    }
}