sql-splitter 1.18.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
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
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
//! 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_column_type_parsing_mssql() {
        assert_eq!(ColumnType::from_sql_type("NVARCHAR(255)"), ColumnType::Text);
        assert_eq!(ColumnType::from_sql_type("BIT"), ColumnType::Bool);
        assert_eq!(
            ColumnType::from_sql_type("BIT(8)"),
            ColumnType::Other("BIT(8)".to_string())
        );
    }

    /// Regression for MySQL 8 integer modifiers: real dumps write `bigint
    /// unsigned` without a display width, and the `unsigned`/`signed`/`zerofill`
    /// modifiers must not push an integer type into the `Other` fallback.
    /// Classifying these as `Other` mis-inferred `sequence`/`string` generators
    /// that the compiler then rejected with `GEN-GENERATOR-TYPE`.
    #[test]
    fn test_column_type_parsing_unsigned_modifiers() {
        assert_eq!(
            ColumnType::from_mysql_type("bigint unsigned"),
            ColumnType::BigInt
        );
        assert_eq!(ColumnType::from_mysql_type("int unsigned"), ColumnType::Int);
        assert_eq!(
            ColumnType::from_mysql_type("tinyint unsigned"),
            ColumnType::Int
        );
        assert_eq!(
            ColumnType::from_mysql_type("int(10) unsigned"),
            ColumnType::Int
        );
        assert_eq!(
            ColumnType::from_mysql_type("BIGINT UNSIGNED ZEROFILL"),
            ColumnType::BigInt
        );
        assert_eq!(
            ColumnType::from_mysql_type("decimal(10,2) unsigned"),
            ColumnType::Decimal
        );
        // The modifier guard keeps genuine multi-word type names intact, and
        // the classifier recognizes their full spelling.
        assert_eq!(
            ColumnType::from_mysql_type("double precision"),
            ColumnType::Decimal
        );
        assert_eq!(
            ColumnType::from_mysql_type("double precision unsigned"),
            ColumnType::Decimal
        );
        assert_eq!(
            ColumnType::from_mysql_type("interval day to second"),
            ColumnType::DateTime
        );
    }

    #[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,
            source_type: "INT".to_string(),
            ordinal: ColumnId(0),
            is_primary_key: true,
            is_nullable: false,
            is_unique: false,
            default_sql: None,
            is_generated: false,
            is_identity: false,
            collation: None,
        });
        table.columns.push(Column {
            name: "email".to_string(),
            col_type: ColumnType::Text,
            source_type: "VARCHAR(255)".to_string(),
            ordinal: ColumnId(1),
            is_primary_key: false,
            is_nullable: true,
            is_unique: false,
            default_sql: None,
            is_generated: false,
            is_identity: false,
            collation: None,
        });
        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 parse_column_preserves_generation_evidence() {
        let mut builder = SchemaBuilder::new();
        builder
            .parse_create_table(
                "CREATE TABLE users (\
                 id BIGINT IDENTITY(1,1) PRIMARY KEY, \
                 email VARCHAR(255) NOT NULL UNIQUE, \
                 state VARCHAR(20) DEFAULT 'active', \
                 slug VARCHAR(255) GENERATED ALWAYS AS (LOWER(email)) STORED);",
            )
            .unwrap();
        let schema = builder.build();
        let table = schema.get_table("users").expect("table");

        let id = table.get_column("id").unwrap();
        assert_eq!(id.source_type, "BIGINT");
        assert!(id.is_identity);

        let email = table.get_column("email").unwrap();
        assert_eq!(email.source_type, "VARCHAR(255)");
        assert!(email.is_unique);

        let state = table.get_column("state").unwrap();
        assert_eq!(state.default_sql.as_deref(), Some("'active'"));

        assert!(table.get_column("slug").unwrap().is_generated);
    }

    #[test]
    fn parse_column_preserves_multi_word_types() {
        let mut builder = SchemaBuilder::new();
        builder
            .parse_create_table(
                "CREATE TABLE metrics (\
                 score DOUBLE PRECISION NOT NULL, \
                 label CHARACTER VARYING(42), \
                 observed_at TIMESTAMP(6) WITH TIME ZONE, \
                 elapsed INTERVAL DAY TO SECOND, \
                 nickname VARCHAR(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci, \
                 secret INT INVISIBLE NOT NULL, \
                 subtotal DECIMAL(10,2) AS (quantity * price) STORED);",
            )
            .unwrap();
        let schema = builder.build();
        let table = schema.get_table("metrics").expect("table");

        let score = table.get_column("score").expect("score");
        assert_eq!(score.source_type, "DOUBLE PRECISION");
        assert_eq!(score.col_type, ColumnType::Decimal);

        let label = table.get_column("label").expect("label");
        assert_eq!(label.source_type, "CHARACTER VARYING(42)");
        assert_eq!(label.col_type, ColumnType::Text);

        let observed_at = table.get_column("observed_at").expect("observed_at");
        assert_eq!(observed_at.source_type, "TIMESTAMP(6) WITH TIME ZONE");
        assert_eq!(observed_at.col_type, ColumnType::DateTime);

        let elapsed = table.get_column("elapsed").expect("elapsed");
        assert_eq!(elapsed.source_type, "INTERVAL DAY TO SECOND");
        assert_eq!(elapsed.col_type, ColumnType::DateTime);

        let nickname = table.get_column("nickname").expect("nickname");
        assert_eq!(nickname.source_type, "VARCHAR(20)");
        assert_eq!(nickname.collation.as_deref(), Some("utf8mb4_unicode_ci"));

        let secret = table.get_column("secret").expect("secret");
        assert_eq!(secret.source_type, "INT");
        assert_eq!(secret.col_type, ColumnType::Int);
        assert!(!secret.is_nullable);

        let subtotal = table.get_column("subtotal").expect("subtotal");
        assert_eq!(subtotal.source_type, "DECIMAL(10,2)");
        assert_eq!(subtotal.col_type, ColumnType::Decimal);
        assert!(subtotal.is_generated);
    }

    #[test]
    fn mysql_auto_increment_marks_identity() {
        let mut builder = SchemaBuilder::new();
        builder.parse_create_table(
            "CREATE TABLE `widgets` (`id` int NOT NULL AUTO_INCREMENT PRIMARY KEY);",
        );
        let schema = builder.build();
        let table = schema.get_table("widgets").expect("table");

        assert!(table.get_column("id").unwrap().is_identity);
    }

    #[test]
    fn sqlite_autoincrement_marks_identity() {
        let mut builder = SchemaBuilder::new();
        builder.parse_create_table("CREATE TABLE widgets (id INTEGER PRIMARY KEY AUTOINCREMENT);");
        let schema = builder.build();
        let table = schema.get_table("widgets").expect("table");

        assert!(table.get_column("id").unwrap().is_identity);
    }

    #[test]
    fn postgres_serial_marks_identity() {
        let mut builder = SchemaBuilder::new();
        builder.parse_create_table("CREATE TABLE widgets (id SERIAL PRIMARY KEY);");
        let schema = builder.build();
        let table = schema.get_table("widgets").expect("table");

        assert!(table.get_column("id").unwrap().is_identity);
    }

    #[test]
    fn postgres_bigserial_marks_identity() {
        let mut builder = SchemaBuilder::new();
        builder.parse_create_table("CREATE TABLE widgets (id BIGSERIAL PRIMARY KEY);");
        let schema = builder.build();
        let table = schema.get_table("widgets").expect("table");

        assert!(table.get_column("id").unwrap().is_identity);
    }

    #[test]
    fn postgres_generated_always_as_identity_marks_identity() {
        let mut builder = SchemaBuilder::new();
        builder.parse_create_table(
            "CREATE TABLE widgets (id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY);",
        );
        let schema = builder.build();
        let table = schema.get_table("widgets").expect("table");

        let id = table.get_column("id").unwrap();
        assert!(id.is_identity);
        // This is identity, not a computed/generated column.
        assert!(!id.is_generated);
    }

    #[test]
    fn table_level_unique_constraint_marks_column_and_is_recorded() {
        let mut builder = SchemaBuilder::new();
        builder.parse_create_table(
            "CREATE TABLE users (id INT PRIMARY KEY, email VARCHAR(255), UNIQUE (email));",
        );
        let schema = builder.build();
        let table = schema.get_table("users").expect("table");

        assert!(table.get_column("email").unwrap().is_unique);
        assert_eq!(table.unique_constraints.len(), 1);
        assert_eq!(
            table.unique_constraints[0].columns,
            vec!["email".to_string()]
        );
    }

    #[test]
    fn table_level_check_constraint_preserves_raw_expression_with_nested_parens_and_comma() {
        let mut builder = SchemaBuilder::new();
        builder.parse_create_table(
            "CREATE TABLE orders (\
             qty INT, \
             price DECIMAL(10,2), \
             CONSTRAINT chk_price CHECK (price > 0 AND (qty > 0 OR qty IS NULL)));",
        );
        let schema = builder.build();
        let table = schema.get_table("orders").expect("table");

        assert_eq!(table.check_constraints.len(), 1);
        assert_eq!(
            table.check_constraints[0].expression,
            "price > 0 AND (qty > 0 OR qty IS NULL)"
        );
    }

    #[test]
    fn collate_populates_column_collation() {
        let mut builder = SchemaBuilder::new();
        builder.parse_create_table(
            "CREATE TABLE users (name VARCHAR(255) COLLATE utf8mb4_unicode_ci);",
        );
        let schema = builder.build();
        let table = schema.get_table("users").expect("table");

        assert_eq!(
            table.get_column("name").unwrap().collation.as_deref(),
            Some("utf8mb4_unicode_ci")
        );
    }

    #[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,
            source_type: "INT".to_string(),
            ordinal: ColumnId(1),
            is_primary_key: false,
            is_nullable: true,
            is_unique: false,
            default_sql: None,
            is_generated: false,
            is_identity: false,
            collation: None,
        });
        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
    }
}