sql-splitter 1.13.1

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
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
//! Unit tests for schema module, extracted from inline tests.

use sql_splitter::schema::{
    Column, ColumnId, ColumnType, ForeignKey, Schema, SchemaBuilder, SchemaGraph, TableId,
    TableSchema,
};

mod mod_tests {
    use super::*;

    #[test]
    fn test_column_type_parsing() {
        assert_eq!(ColumnType::from_mysql_type("INT"), ColumnType::Int);
        assert_eq!(ColumnType::from_mysql_type("int(11)"), ColumnType::Int);
        assert_eq!(ColumnType::from_mysql_type("BIGINT"), ColumnType::BigInt);
        assert_eq!(
            ColumnType::from_mysql_type("VARCHAR(255)"),
            ColumnType::Text
        );
        assert_eq!(ColumnType::from_mysql_type("TEXT"), ColumnType::Text);
        assert_eq!(
            ColumnType::from_mysql_type("DATETIME"),
            ColumnType::DateTime
        );
        assert_eq!(
            ColumnType::from_mysql_type("DECIMAL(10,2)"),
            ColumnType::Decimal
        );
    }

    #[test]
    fn test_schema_table_lookup() {
        let mut schema = Schema::new();
        let table = TableSchema::new("users".to_string(), TableId(0));
        let id = schema.add_table(table);

        assert_eq!(schema.get_table_id("users"), Some(id));
        assert_eq!(schema.get_table_id("USERS"), Some(id)); // case-insensitive
        assert_eq!(schema.get_table_id("nonexistent"), None);
    }

    #[test]
    fn test_table_schema_column_lookup() {
        let mut table = TableSchema::new("users".to_string(), TableId(0));
        table.columns.push(Column {
            name: "id".to_string(),
            col_type: ColumnType::Int,
            ordinal: ColumnId(0),
            is_primary_key: true,
            is_nullable: false,
        });
        table.columns.push(Column {
            name: "email".to_string(),
            col_type: ColumnType::Text,
            ordinal: ColumnId(1),
            is_primary_key: false,
            is_nullable: true,
        });
        table.primary_key = vec![ColumnId(0)];

        assert!(table.get_column("id").is_some());
        assert!(table.get_column("ID").is_some()); // case-insensitive
        assert_eq!(table.get_column_id("email"), Some(ColumnId(1)));
        assert!(table.is_pk_column(ColumnId(0)));
        assert!(!table.is_pk_column(ColumnId(1)));
    }
}

mod ddl_tests {
    use super::*;
    use sql_splitter::schema::{extract_alter_table_name, extract_create_table_name};

    #[test]
    fn test_extract_create_table_name() {
        assert_eq!(
            extract_create_table_name("CREATE TABLE users (id INT);"),
            Some("users".to_string())
        );
        assert_eq!(
            extract_create_table_name("CREATE TABLE `my_table` (id INT);"),
            Some("my_table".to_string())
        );
        assert_eq!(
            extract_create_table_name("CREATE TABLE IF NOT EXISTS `users` (id INT);"),
            Some("users".to_string())
        );
    }

    #[test]
    fn test_extract_alter_table_name() {
        assert_eq!(
            extract_alter_table_name("ALTER TABLE users ADD COLUMN email VARCHAR(255);"),
            Some("users".to_string())
        );
        assert_eq!(
            extract_alter_table_name("ALTER TABLE `orders` ADD CONSTRAINT ..."),
            Some("orders".to_string())
        );
    }

    #[test]
    fn test_parse_create_table_simple() {
        let mut builder = SchemaBuilder::new();
        let stmt = r#"CREATE TABLE `users` (
            `id` int NOT NULL AUTO_INCREMENT,
            `email` varchar(255) DEFAULT NULL,
            PRIMARY KEY (`id`)
        ) ENGINE=InnoDB;"#;

        let id = builder.parse_create_table(stmt);
        assert!(id.is_some());

        let schema = builder.build();
        let table = schema.table(id.unwrap()).unwrap();

        assert_eq!(table.name, "users");
        assert_eq!(table.columns.len(), 2);
        assert_eq!(table.columns[0].name, "id");
        assert_eq!(table.columns[0].col_type, ColumnType::Int);
        assert!(table.columns[0].is_primary_key);
        assert!(!table.columns[0].is_nullable);
        assert_eq!(table.columns[1].name, "email");
        assert!(table.columns[1].is_nullable);
        assert_eq!(table.primary_key.len(), 1);
    }

    #[test]
    fn test_parse_create_table_with_fk() {
        let mut builder = SchemaBuilder::new();

        // First create the referenced table
        builder.parse_create_table("CREATE TABLE `companies` (`id` int PRIMARY KEY);");

        let stmt = r#"CREATE TABLE `users` (
            `id` int NOT NULL AUTO_INCREMENT,
            `company_id` int DEFAULT NULL,
            PRIMARY KEY (`id`),
            CONSTRAINT `fk_company` FOREIGN KEY (`company_id`) REFERENCES `companies` (`id`)
        ) ENGINE=InnoDB;"#;

        let id = builder.parse_create_table(stmt);
        let schema = builder.build();
        let table = schema.table(id.unwrap()).unwrap();

        assert_eq!(table.foreign_keys.len(), 1);
        let fk = &table.foreign_keys[0];
        assert_eq!(fk.name, Some("fk_company".to_string()));
        assert_eq!(fk.column_names, vec!["company_id".to_string()]);
        assert_eq!(fk.referenced_table, "companies");
        assert_eq!(fk.referenced_columns, vec!["id".to_string()]);
        assert!(fk.referenced_table_id.is_some());
    }

    #[test]
    fn test_parse_foreign_key_without_constraint_name() {
        let mut builder = SchemaBuilder::new();
        builder.parse_create_table("CREATE TABLE `orders` (`id` int PRIMARY KEY);");

        let stmt = r#"CREATE TABLE `order_items` (
            `id` int NOT NULL AUTO_INCREMENT,
            `order_id` int NOT NULL,
            PRIMARY KEY (`id`),
            FOREIGN KEY (`order_id`) REFERENCES `orders` (`id`)
        );"#;

        let id = builder.parse_create_table(stmt);
        let schema = builder.build();
        let table = schema.table(id.unwrap()).unwrap();

        assert_eq!(table.foreign_keys.len(), 1);
        let fk = &table.foreign_keys[0];
        assert!(fk.name.is_none());
        assert_eq!(fk.referenced_table, "orders");
    }

    #[test]
    fn test_parse_composite_primary_key() {
        let mut builder = SchemaBuilder::new();
        let stmt = r#"CREATE TABLE `order_items` (
            `order_id` int NOT NULL,
            `product_id` int NOT NULL,
            `quantity` int DEFAULT 1,
            PRIMARY KEY (`order_id`, `product_id`)
        );"#;

        let id = builder.parse_create_table(stmt);
        let schema = builder.build();
        let table = schema.table(id.unwrap()).unwrap();

        assert_eq!(table.primary_key.len(), 2);
        assert!(table.columns[0].is_primary_key);
        assert!(table.columns[1].is_primary_key);
        assert!(!table.columns[2].is_primary_key);
    }

    #[test]
    fn test_parse_inline_primary_key() {
        let mut builder = SchemaBuilder::new();
        let stmt = "CREATE TABLE `simple` (`id` int PRIMARY KEY, `name` varchar(100));";

        let id = builder.parse_create_table(stmt);
        let schema = builder.build();
        let table = schema.table(id.unwrap()).unwrap();

        assert_eq!(table.primary_key.len(), 1);
        assert!(table.columns[0].is_primary_key);
    }

    #[test]
    fn test_parse_postgres_inline_primary_key() {
        let mut builder = SchemaBuilder::new();
        let stmt = r#"CREATE TABLE users (
    id INTEGER PRIMARY KEY,
    name VARCHAR(100)
)"#;

        let id = builder.parse_create_table(stmt);
        let schema = builder.build();
        let table = schema.table(id.unwrap()).unwrap();

        assert_eq!(table.primary_key.len(), 1, "Should have 1 PK column");
        assert!(table.columns[0].is_primary_key, "First column should be PK");
        assert_eq!(table.columns[0].name, "id");
    }

    #[test]
    fn test_split_table_body() {
        use sql_splitter::schema::split_table_body;

        let body = "`id` int, `name` varchar(255), PRIMARY KEY (`id`)";
        let parts = split_table_body(body);
        assert_eq!(parts.len(), 3);
        assert!(parts[0].contains("id"));
        assert!(parts[1].contains("name"));
        assert!(parts[2].contains("PRIMARY KEY"));
    }

    #[test]
    fn test_parse_column_list() {
        use sql_splitter::schema::parse_column_list;

        assert_eq!(
            parse_column_list("`id`, `name`"),
            vec!["id".to_string(), "name".to_string()]
        );
        assert_eq!(
            parse_column_list("id,name,email"),
            vec!["id".to_string(), "name".to_string(), "email".to_string()]
        );
    }
}

mod graph_tests {
    use super::*;

    fn create_test_schema() -> Schema {
        let mut schema = Schema::new();

        // companies (root)
        let companies = TableSchema::new("companies".to_string(), TableId(0));
        schema.add_table(companies);

        // users -> companies
        let mut users = TableSchema::new("users".to_string(), TableId(0));
        users.columns.push(Column {
            name: "company_id".to_string(),
            col_type: ColumnType::Int,
            ordinal: ColumnId(1),
            is_primary_key: false,
            is_nullable: true,
        });
        users.foreign_keys.push(ForeignKey {
            name: None,
            columns: vec![ColumnId(1)],
            column_names: vec!["company_id".to_string()],
            referenced_table: "companies".to_string(),
            referenced_columns: vec!["id".to_string()],
            referenced_table_id: Some(TableId(0)),
        });
        schema.add_table(users);

        // orders -> users
        let mut orders = TableSchema::new("orders".to_string(), TableId(0));
        orders.foreign_keys.push(ForeignKey {
            name: None,
            columns: vec![ColumnId(1)],
            column_names: vec!["user_id".to_string()],
            referenced_table: "users".to_string(),
            referenced_columns: vec!["id".to_string()],
            referenced_table_id: Some(TableId(1)),
        });
        schema.add_table(orders);

        schema
    }

    #[test]
    fn test_graph_construction() {
        let schema = create_test_schema();
        let graph = SchemaGraph::from_schema(schema);

        assert_eq!(graph.len(), 3);

        // companies (0) has no parents
        assert!(graph.parents[0].is_empty());
        // companies (0) has users (1) as child
        assert_eq!(graph.children[0], vec![TableId(1)]);

        // users (1) has companies (0) as parent
        assert_eq!(graph.parents[1], vec![TableId(0)]);
        // users (1) has orders (2) as child
        assert_eq!(graph.children[1], vec![TableId(2)]);

        // orders (2) has users (1) as parent
        assert_eq!(graph.parents[2], vec![TableId(1)]);
        // orders (2) has no children
        assert!(graph.children[2].is_empty());
    }

    #[test]
    fn test_topo_sort() {
        let schema = create_test_schema();
        let graph = SchemaGraph::from_schema(schema);
        let result = graph.topo_sort();

        assert!(result.cyclic_tables.is_empty());
        assert_eq!(result.order.len(), 3);

        // companies must come before users
        let companies_pos = result
            .order
            .iter()
            .position(|&id| id == TableId(0))
            .unwrap();
        let users_pos = result
            .order
            .iter()
            .position(|&id| id == TableId(1))
            .unwrap();
        let orders_pos = result
            .order
            .iter()
            .position(|&id| id == TableId(2))
            .unwrap();

        assert!(companies_pos < users_pos);
        assert!(users_pos < orders_pos);
    }

    #[test]
    fn test_cycle_detection() {
        let mut schema = Schema::new();

        // Create a cycle: A -> B -> A
        let mut table_a = TableSchema::new("table_a".to_string(), TableId(0));
        table_a.foreign_keys.push(ForeignKey {
            name: None,
            columns: vec![],
            column_names: vec![],
            referenced_table: "table_b".to_string(),
            referenced_columns: vec![],
            referenced_table_id: Some(TableId(1)),
        });
        schema.add_table(table_a);

        let mut table_b = TableSchema::new("table_b".to_string(), TableId(0));
        table_b.foreign_keys.push(ForeignKey {
            name: None,
            columns: vec![],
            column_names: vec![],
            referenced_table: "table_a".to_string(),
            referenced_columns: vec![],
            referenced_table_id: Some(TableId(0)),
        });
        schema.add_table(table_b);

        let graph = SchemaGraph::from_schema(schema);
        let result = graph.topo_sort();

        assert!(result.order.is_empty());
        assert_eq!(result.cyclic_tables.len(), 2);
    }

    #[test]
    fn test_self_reference() {
        let mut schema = Schema::new();

        // Create a self-referential table (e.g., categories with parent_id)
        let mut categories = TableSchema::new("categories".to_string(), TableId(0));
        categories.foreign_keys.push(ForeignKey {
            name: None,
            columns: vec![ColumnId(1)],
            column_names: vec!["parent_id".to_string()],
            referenced_table: "categories".to_string(),
            referenced_columns: vec!["id".to_string()],
            referenced_table_id: Some(TableId(0)), // Self-reference
        });
        schema.add_table(categories);

        let graph = SchemaGraph::from_schema(schema);

        // Self-references should be detected
        assert!(graph.has_self_reference(TableId(0)));
        assert_eq!(graph.self_referential_tables(), vec![TableId(0)]);

        // Self-references should NOT create cycles in the graph
        // (we filter them out during graph construction)
        let result = graph.topo_sort();
        assert!(result.cyclic_tables.is_empty());
        assert_eq!(result.order.len(), 1);
    }

    #[test]
    fn test_root_and_leaf_tables() {
        let schema = create_test_schema();
        let graph = SchemaGraph::from_schema(schema);

        let roots = graph.root_tables();
        assert_eq!(roots, vec![TableId(0)]); // companies

        let leaves = graph.leaf_tables();
        assert_eq!(leaves, vec![TableId(2)]); // orders
    }

    #[test]
    fn test_ancestors_and_descendants() {
        let schema = create_test_schema();
        let graph = SchemaGraph::from_schema(schema);

        // orders' ancestors: users, companies
        let order_ancestors = graph.ancestors(TableId(2));
        assert!(order_ancestors.contains(&TableId(1))); // users
        assert!(order_ancestors.contains(&TableId(0))); // companies

        // companies' descendants: users, orders
        let company_descendants = graph.descendants(TableId(0));
        assert!(company_descendants.contains(&TableId(1))); // users
        assert!(company_descendants.contains(&TableId(2))); // orders
    }

    #[test]
    fn test_is_ancestor() {
        let schema = create_test_schema();
        let graph = SchemaGraph::from_schema(schema);

        assert!(graph.is_ancestor(TableId(0), TableId(2))); // companies is ancestor of orders
        assert!(graph.is_ancestor(TableId(1), TableId(2))); // users is ancestor of orders
        assert!(!graph.is_ancestor(TableId(2), TableId(0))); // orders is not ancestor of companies
    }
}