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
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
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
//! Integration tests for the sample command using test_data_gen fixtures.

use sql_splitter::parser::SqlDialect;
use sql_splitter::sample::{run, SampleConfig, SampleMode};
use std::fs;
use std::io::Write;
use tempfile::{NamedTempFile, TempDir};
use test_data_gen::{Generator, RenderConfig, Renderer, Scale};

/// Generate a MySQL dump with FK relationships for testing
fn generate_test_dump() -> NamedTempFile {
    let mut gen = Generator::new(42, Scale::Small);
    let data = gen.generate();
    let renderer = Renderer::new(RenderConfig::mysql());
    let output = renderer.render_to_string(&data).unwrap();

    let mut file = NamedTempFile::new().unwrap();
    file.write_all(output.as_bytes()).unwrap();
    file.flush().unwrap();
    file
}

#[test]
fn test_sample_with_generated_fixtures() {
    let dump = generate_test_dump();
    let output_dir = TempDir::new().unwrap();
    let output_file = output_dir.path().join("sampled.sql");

    let config = SampleConfig {
        input: dump.path().to_path_buf(),
        output: Some(output_file.clone()),
        dialect: SqlDialect::MySql,
        mode: SampleMode::Percent(50),
        preserve_relations: false,
        seed: 42,
        ..Default::default()
    };

    let stats = run(config).unwrap();

    assert!(
        stats.tables_sampled > 0,
        "Should have sampled at least one table"
    );
    assert!(
        stats.total_rows_selected > 0,
        "Should have selected some rows"
    );
    assert!(
        stats.total_rows_selected <= stats.total_rows_seen,
        "Selected rows should not exceed total rows"
    );

    // Check output file was created
    assert!(output_file.exists(), "Output file should exist");

    // Read output and verify it has content
    let content = fs::read_to_string(&output_file).unwrap();
    assert!(
        content.contains("INSERT INTO"),
        "Output should contain INSERT statements"
    );
    assert!(
        content.contains("CREATE TABLE"),
        "Output should contain CREATE TABLE statements"
    );
}

#[test]
fn test_sample_with_preserve_relations() {
    let dump = generate_test_dump();
    let output_dir = TempDir::new().unwrap();
    let output_file = output_dir.path().join("sampled_fk.sql");

    let config = SampleConfig {
        input: dump.path().to_path_buf(),
        output: Some(output_file.clone()),
        dialect: SqlDialect::MySql,
        mode: SampleMode::Rows(10),
        preserve_relations: true,
        seed: 42,
        ..Default::default()
    };

    let stats = run(config).unwrap();

    assert!(
        stats.tables_sampled > 0,
        "Should have sampled at least one table"
    );

    // When preserving relations, dependent tables may have fewer rows
    // because rows referencing unsampled parents are filtered out
    let content = fs::read_to_string(&output_file).unwrap();
    assert!(
        content.contains("INSERT INTO"),
        "Output should contain INSERT statements"
    );
}

#[test]
fn test_sample_fixed_rows() {
    let dump = generate_test_dump();
    let output_dir = TempDir::new().unwrap();
    let output_file = output_dir.path().join("sampled_rows.sql");

    let config = SampleConfig {
        input: dump.path().to_path_buf(),
        output: Some(output_file.clone()),
        dialect: SqlDialect::MySql,
        mode: SampleMode::Rows(5),
        preserve_relations: false,
        seed: 42,
        ..Default::default()
    };

    let stats = run(config).unwrap();

    // Each table should have at most 5 rows (or less if table has fewer)
    for table_stat in &stats.table_stats {
        assert!(
            table_stat.rows_selected <= 5,
            "Table {} should have at most 5 rows, got {}",
            table_stat.name,
            table_stat.rows_selected
        );
    }
}

#[test]
fn test_sample_with_table_filter() {
    let dump = generate_test_dump();
    let output_dir = TempDir::new().unwrap();
    let output_file = output_dir.path().join("sampled_filtered.sql");

    let config = SampleConfig {
        input: dump.path().to_path_buf(),
        output: Some(output_file.clone()),
        dialect: SqlDialect::MySql,
        mode: SampleMode::Percent(100),
        tables_filter: Some(vec!["tenants".to_string()]),
        preserve_relations: false,
        seed: 42,
        ..Default::default()
    };

    let stats = run(config).unwrap();

    assert_eq!(
        stats.tables_sampled, 1,
        "Should have sampled only one table"
    );
    assert!(
        stats.table_stats.iter().any(|t| t.name == "tenants"),
        "Should have sampled tenants table"
    );
}

#[test]
fn test_sample_with_exclude() {
    let dump = generate_test_dump();
    let output_dir = TempDir::new().unwrap();
    let output_file = output_dir.path().join("sampled_excluded.sql");

    let config = SampleConfig {
        input: dump.path().to_path_buf(),
        output: Some(output_file.clone()),
        dialect: SqlDialect::MySql,
        mode: SampleMode::Percent(100),
        exclude: vec!["tenants".to_string()],
        preserve_relations: false,
        seed: 42,
        ..Default::default()
    };

    let stats = run(config).unwrap();

    assert!(
        !stats.table_stats.iter().any(|t| t.name == "tenants"),
        "Should not have sampled tenants table"
    );
}

#[test]
fn test_sample_reproducible_with_seed() {
    let dump = generate_test_dump();
    let output_dir = TempDir::new().unwrap();
    let output_file1 = output_dir.path().join("sampled1.sql");
    let output_file2 = output_dir.path().join("sampled2.sql");

    // Run twice with same seed
    for output_file in [&output_file1, &output_file2] {
        let config = SampleConfig {
            input: dump.path().to_path_buf(),
            output: Some(output_file.clone()),
            dialect: SqlDialect::MySql,
            mode: SampleMode::Rows(5),
            preserve_relations: false,
            seed: 12345,
            ..Default::default()
        };

        run(config).unwrap();
    }

    // Read both outputs
    let content1 = fs::read_to_string(&output_file1).unwrap();
    let content2 = fs::read_to_string(&output_file2).unwrap();

    // Skip the header (which contains timestamp) and compare the rest
    let data1: String = content1
        .lines()
        .filter(|l| !l.starts_with("-- Date:"))
        .collect();
    let data2: String = content2
        .lines()
        .filter(|l| !l.starts_with("-- Date:"))
        .collect();

    assert_eq!(
        data1, data2,
        "Outputs with same seed should be identical (except timestamp)"
    );
}

#[test]
fn test_sample_dry_run() {
    let dump = generate_test_dump();

    let config = SampleConfig {
        input: dump.path().to_path_buf(),
        output: None,
        dialect: SqlDialect::MySql,
        mode: SampleMode::Percent(50),
        preserve_relations: false,
        seed: 42,
        dry_run: true,
        ..Default::default()
    };

    let stats = run(config).unwrap();

    // Dry run should still compute stats
    assert!(stats.tables_sampled > 0);
    assert!(stats.total_rows_seen > 0);
}

#[test]
fn test_sample_with_yaml_config() {
    use std::io::Write;

    let dump = generate_test_dump();
    let output_dir = TempDir::new().unwrap();
    let output_file = output_dir.path().join("sampled_config.sql");

    // Create YAML config
    let mut config_file = tempfile::NamedTempFile::new().unwrap();
    writeln!(
        config_file,
        r#"
default:
  percent: 50

classification:
  lookup:
    - currencies
    - permissions

tables:
  tenants:
    percent: 100
  users:
    rows: 5
"#
    )
    .unwrap();
    config_file.flush().unwrap();

    let config = SampleConfig {
        input: dump.path().to_path_buf(),
        output: Some(output_file.clone()),
        dialect: SqlDialect::MySql,
        mode: SampleMode::Percent(10), // Will be overridden by config
        preserve_relations: false,
        seed: 42,
        config_file: Some(config_file.path().to_path_buf()),
        ..Default::default()
    };

    let stats = run(config).unwrap();

    // Check that config was applied
    assert!(stats.tables_sampled > 0);

    // Find tenants stats - should have 100% (3/3 rows)
    let tenants_stats = stats.table_stats.iter().find(|t| t.name == "tenants");
    if let Some(t) = tenants_stats {
        assert_eq!(
            t.rows_seen, t.rows_selected,
            "tenants should have 100% of rows"
        );
    }
}

#[test]
fn test_sample_with_root_tables() {
    let dump = generate_test_dump();
    let output_dir = TempDir::new().unwrap();
    let output_file = output_dir.path().join("sampled_roots.sql");

    let config = SampleConfig {
        input: dump.path().to_path_buf(),
        output: Some(output_file.clone()),
        dialect: SqlDialect::MySql,
        mode: SampleMode::Rows(10),
        preserve_relations: true,
        seed: 42,
        root_tables: vec!["orders".to_string()],
        ..Default::default()
    };

    let stats = run(config).unwrap();

    assert!(stats.tables_sampled > 0);
}

#[test]
fn test_sample_with_max_total_rows() {
    let dump = generate_test_dump();
    let output_dir = TempDir::new().unwrap();
    let output_file = output_dir.path().join("sampled_max.sql");

    let config = SampleConfig {
        input: dump.path().to_path_buf(),
        output: Some(output_file.clone()),
        dialect: SqlDialect::MySql,
        mode: SampleMode::Percent(100),
        preserve_relations: false,
        seed: 42,
        max_total_rows: Some(50),
        ..Default::default()
    };

    let stats = run(config).unwrap();

    // Should hit the limit or have a warning
    assert!(stats.total_rows_selected <= 100 || !stats.warnings.is_empty());
}

#[test]
fn test_sample_include_global_none() {
    use sql_splitter::sample::GlobalTableMode;

    let dump = generate_test_dump();
    let output_dir = TempDir::new().unwrap();
    let output_file = output_dir.path().join("sampled_no_global.sql");

    let config = SampleConfig {
        input: dump.path().to_path_buf(),
        output: Some(output_file.clone()),
        dialect: SqlDialect::MySql,
        mode: SampleMode::Percent(100),
        preserve_relations: false,
        seed: 42,
        include_global: GlobalTableMode::None,
        ..Default::default()
    };

    let stats = run(config).unwrap();

    assert!(stats.tables_sampled > 0);
}

#[test]
fn test_sample_no_schema() {
    let dump = generate_test_dump();
    let output_dir = TempDir::new().unwrap();
    let output_file = output_dir.path().join("sampled_no_schema.sql");

    let config = SampleConfig {
        input: dump.path().to_path_buf(),
        output: Some(output_file.clone()),
        dialect: SqlDialect::MySql,
        mode: SampleMode::Percent(100),
        preserve_relations: false,
        seed: 42,
        include_schema: false,
        ..Default::default()
    };

    let stats = run(config).unwrap();

    assert!(stats.tables_sampled > 0);

    // Verify no CREATE TABLE in output
    let content = fs::read_to_string(&output_file).unwrap();
    assert!(
        !content.contains("CREATE TABLE"),
        "Should not contain CREATE TABLE"
    );
    assert!(
        content.contains("INSERT INTO"),
        "Should contain INSERT statements"
    );
}

/// Generate a PostgreSQL dump for testing
fn generate_postgres_dump() -> NamedTempFile {
    let mut gen = Generator::new(42, Scale::Small);
    let data = gen.generate();
    let renderer = Renderer::new(RenderConfig::postgres());
    let output = renderer.render_to_string(&data).unwrap();

    let mut file = NamedTempFile::new().unwrap();
    file.write_all(output.as_bytes()).unwrap();
    file.flush().unwrap();
    file
}

/// Generate a SQLite dump for testing
fn generate_sqlite_dump() -> NamedTempFile {
    let mut gen = Generator::new(42, Scale::Small);
    let data = gen.generate();
    let renderer = Renderer::new(RenderConfig::sqlite());
    let output = renderer.render_to_string(&data).unwrap();

    let mut file = NamedTempFile::new().unwrap();
    file.write_all(output.as_bytes()).unwrap();
    file.flush().unwrap();
    file
}

#[test]
fn test_sample_postgres_dialect() {
    let dump = generate_postgres_dump();
    let output_dir = TempDir::new().unwrap();
    let output_file = output_dir.path().join("sampled_postgres.sql");

    let config = SampleConfig {
        input: dump.path().to_path_buf(),
        output: Some(output_file.clone()),
        dialect: SqlDialect::Postgres,
        mode: SampleMode::Percent(50),
        preserve_relations: false,
        seed: 42,
        ..Default::default()
    };

    let stats = run(config).unwrap();

    assert!(
        stats.tables_sampled > 0,
        "Should have sampled at least one table"
    );
    assert!(
        stats.total_rows_selected > 0,
        "Should have selected some rows"
    );

    // Check output file was created with valid PostgreSQL syntax
    let content = fs::read_to_string(&output_file).unwrap();
    assert!(
        content.contains("INSERT INTO"),
        "Output should contain INSERT statements"
    );
    assert!(
        content.contains("CREATE TABLE"),
        "Output should contain CREATE TABLE statements"
    );
    // PostgreSQL uses double quotes for identifiers
    assert!(
        content.contains("\""),
        "Output should use double quotes for identifiers"
    );
    // Check that COPY format was properly converted to INSERT VALUES format
    assert!(
        content.contains("VALUES"),
        "Output should contain VALUES clauses"
    );
    // Values should be in parentheses, not tab-separated
    assert!(
        !content.contains("\t1\t") && !content.contains("\t2\t"),
        "Output should not contain tab-separated COPY format"
    );
}

#[test]
fn test_sample_sqlite_dialect() {
    let dump = generate_sqlite_dump();
    let output_dir = TempDir::new().unwrap();
    let output_file = output_dir.path().join("sampled_sqlite.sql");

    let config = SampleConfig {
        input: dump.path().to_path_buf(),
        output: Some(output_file.clone()),
        dialect: SqlDialect::Sqlite,
        mode: SampleMode::Percent(50),
        preserve_relations: false,
        seed: 42,
        ..Default::default()
    };

    let stats = run(config).unwrap();

    assert!(
        stats.tables_sampled > 0,
        "Should have sampled at least one table"
    );
    assert!(
        stats.total_rows_selected > 0,
        "Should have selected some rows"
    );

    // Check output file was created with valid SQLite syntax
    let content = fs::read_to_string(&output_file).unwrap();
    assert!(
        content.contains("INSERT INTO"),
        "Output should contain INSERT statements"
    );
    assert!(
        content.contains("CREATE TABLE"),
        "Output should contain CREATE TABLE statements"
    );
    // SQLite uses double quotes for identifiers
    assert!(
        content.contains("\""),
        "Output should use double quotes for identifiers"
    );
}

#[test]
fn test_sample_postgres_preserve_relations() {
    let dump = generate_postgres_dump();
    let output_dir = TempDir::new().unwrap();
    let output_file = output_dir.path().join("sampled_postgres_fk.sql");

    let config = SampleConfig {
        input: dump.path().to_path_buf(),
        output: Some(output_file.clone()),
        dialect: SqlDialect::Postgres,
        mode: SampleMode::Rows(10),
        preserve_relations: true,
        seed: 42,
        ..Default::default()
    };

    let stats = run(config).unwrap();

    assert!(
        stats.tables_sampled > 0,
        "Should have sampled at least one table"
    );

    // When preserving relations, the output should still be valid
    let content = fs::read_to_string(&output_file).unwrap();
    assert!(
        content.contains("INSERT INTO"),
        "Output should contain INSERT statements"
    );
}

// =============================================================================
// Extended Postgres Sample Tests
// =============================================================================

#[test]
fn test_sample_postgres_no_schema() {
    let dump = generate_postgres_dump();
    let output_dir = TempDir::new().unwrap();
    let output_file = output_dir.path().join("sampled_postgres_no_schema.sql");

    let config = SampleConfig {
        input: dump.path().to_path_buf(),
        output: Some(output_file.clone()),
        dialect: SqlDialect::Postgres,
        mode: SampleMode::Percent(100),
        preserve_relations: false,
        seed: 42,
        include_schema: false,
        ..Default::default()
    };

    let stats = run(config).unwrap();

    assert!(stats.tables_sampled > 0);

    let content = fs::read_to_string(&output_file).unwrap();
    assert!(
        !content.contains("CREATE TABLE"),
        "Should not contain CREATE TABLE"
    );
    assert!(
        content.contains("INSERT INTO"),
        "Should contain INSERT statements"
    );
}

#[test]
fn test_sample_postgres_dry_run() {
    let dump = generate_postgres_dump();

    let config = SampleConfig {
        input: dump.path().to_path_buf(),
        output: None,
        dialect: SqlDialect::Postgres,
        mode: SampleMode::Percent(50),
        preserve_relations: false,
        seed: 42,
        dry_run: true,
        ..Default::default()
    };

    let stats = run(config).unwrap();

    assert!(stats.tables_sampled > 0);
    assert!(stats.total_rows_seen > 0);
}

#[test]
fn test_sample_postgres_with_table_filter() {
    let dump = generate_postgres_dump();
    let output_dir = TempDir::new().unwrap();
    let output_file = output_dir.path().join("sampled_postgres_filtered.sql");

    let config = SampleConfig {
        input: dump.path().to_path_buf(),
        output: Some(output_file.clone()),
        dialect: SqlDialect::Postgres,
        mode: SampleMode::Percent(100),
        tables_filter: Some(vec!["tenants".to_string()]),
        preserve_relations: false,
        seed: 42,
        ..Default::default()
    };

    let stats = run(config).unwrap();

    assert_eq!(
        stats.tables_sampled, 1,
        "Should have sampled only one table"
    );
}

// =============================================================================
// Extended SQLite Sample Tests
// =============================================================================

#[test]
fn test_sample_sqlite_preserve_relations() {
    let dump = generate_sqlite_dump();
    let output_dir = TempDir::new().unwrap();
    let output_file = output_dir.path().join("sampled_sqlite_fk.sql");

    let config = SampleConfig {
        input: dump.path().to_path_buf(),
        output: Some(output_file.clone()),
        dialect: SqlDialect::Sqlite,
        mode: SampleMode::Rows(10),
        preserve_relations: true,
        seed: 42,
        ..Default::default()
    };

    let stats = run(config).unwrap();

    assert!(
        stats.tables_sampled > 0,
        "Should have sampled at least one table"
    );

    let content = fs::read_to_string(&output_file).unwrap();
    assert!(
        content.contains("INSERT INTO"),
        "Output should contain INSERT statements"
    );
}

#[test]
fn test_sample_sqlite_no_schema() {
    let dump = generate_sqlite_dump();
    let output_dir = TempDir::new().unwrap();
    let output_file = output_dir.path().join("sampled_sqlite_no_schema.sql");

    let config = SampleConfig {
        input: dump.path().to_path_buf(),
        output: Some(output_file.clone()),
        dialect: SqlDialect::Sqlite,
        mode: SampleMode::Percent(100),
        preserve_relations: false,
        seed: 42,
        include_schema: false,
        ..Default::default()
    };

    let stats = run(config).unwrap();

    assert!(stats.tables_sampled > 0);

    let content = fs::read_to_string(&output_file).unwrap();
    assert!(
        !content.contains("CREATE TABLE"),
        "Should not contain CREATE TABLE"
    );
    assert!(
        content.contains("INSERT INTO"),
        "Should contain INSERT statements"
    );
}

#[test]
fn test_sample_sqlite_dry_run() {
    let dump = generate_sqlite_dump();

    let config = SampleConfig {
        input: dump.path().to_path_buf(),
        output: None,
        dialect: SqlDialect::Sqlite,
        mode: SampleMode::Percent(50),
        preserve_relations: false,
        seed: 42,
        dry_run: true,
        ..Default::default()
    };

    let stats = run(config).unwrap();

    assert!(stats.tables_sampled > 0);
    assert!(stats.total_rows_seen > 0);
}

// =============================================================================
// Extended MSSQL Sample Tests
// =============================================================================

fn mssql_simple_fixture() -> std::path::PathBuf {
    std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
        .join("tests/fixtures/static/mssql/simple.sql")
}

fn mssql_multi_tenant_fixture() -> std::path::PathBuf {
    std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
        .join("tests/fixtures/static/mssql/multi_tenant.sql")
}

#[test]
fn test_sample_mssql_basic() {
    let output_dir = TempDir::new().unwrap();
    let output_file = output_dir.path().join("sampled_mssql.sql");

    let config = SampleConfig {
        input: mssql_simple_fixture(),
        output: Some(output_file.clone()),
        dialect: SqlDialect::Mssql,
        mode: SampleMode::Percent(100),
        preserve_relations: false,
        seed: 42,
        ..Default::default()
    };

    let stats = run(config).unwrap();

    assert!(
        stats.tables_sampled > 0,
        "Should have sampled at least one table"
    );
    assert!(
        stats.total_rows_selected > 0,
        "Should have selected some rows"
    );

    let content = fs::read_to_string(&output_file).unwrap();
    assert!(
        content.contains("INSERT INTO"),
        "Output should contain INSERT statements"
    );
}

#[test]
fn test_sample_mssql_preserve_relations() {
    let output_dir = TempDir::new().unwrap();
    let output_file = output_dir.path().join("sampled_mssql_fk.sql");

    let config = SampleConfig {
        input: mssql_multi_tenant_fixture(),
        output: Some(output_file.clone()),
        dialect: SqlDialect::Mssql,
        mode: SampleMode::Rows(10),
        preserve_relations: true,
        seed: 42,
        ..Default::default()
    };

    let stats = run(config).unwrap();

    assert!(
        stats.tables_sampled > 0,
        "Should have sampled at least one table"
    );

    let content = fs::read_to_string(&output_file).unwrap();
    assert!(
        content.contains("INSERT INTO"),
        "Output should contain INSERT statements"
    );
}

#[test]
fn test_sample_mssql_no_schema() {
    let output_dir = TempDir::new().unwrap();
    let output_file = output_dir.path().join("sampled_mssql_no_schema.sql");

    let config = SampleConfig {
        input: mssql_simple_fixture(),
        output: Some(output_file.clone()),
        dialect: SqlDialect::Mssql,
        mode: SampleMode::Percent(100),
        preserve_relations: false,
        seed: 42,
        include_schema: false,
        ..Default::default()
    };

    let stats = run(config).unwrap();

    assert!(stats.tables_sampled > 0);

    let content = fs::read_to_string(&output_file).unwrap();
    assert!(
        !content.contains("CREATE TABLE"),
        "Should not contain CREATE TABLE"
    );
    assert!(
        content.contains("INSERT INTO"),
        "Should contain INSERT statements"
    );
}

#[test]
fn test_sample_mssql_dry_run() {
    let config = SampleConfig {
        input: mssql_simple_fixture(),
        output: None,
        dialect: SqlDialect::Mssql,
        mode: SampleMode::Percent(50),
        preserve_relations: false,
        seed: 42,
        dry_run: true,
        ..Default::default()
    };

    let stats = run(config).unwrap();

    assert!(stats.tables_sampled > 0);
    assert!(stats.total_rows_seen > 0);
}

#[test]
fn test_sample_mssql_with_table_filter() {
    let output_dir = TempDir::new().unwrap();
    let output_file = output_dir.path().join("sampled_mssql_filtered.sql");

    let config = SampleConfig {
        input: mssql_simple_fixture(),
        output: Some(output_file.clone()),
        dialect: SqlDialect::Mssql,
        mode: SampleMode::Percent(100),
        tables_filter: Some(vec!["users".to_string()]),
        preserve_relations: false,
        seed: 42,
        ..Default::default()
    };

    let stats = run(config).unwrap();

    assert_eq!(
        stats.tables_sampled, 1,
        "Should have sampled only one table"
    );
    assert!(
        stats.table_stats.iter().any(|t| t.name == "users"),
        "Should have sampled users table"
    );
}