parsql-cli 0.5.0

Command-line interface for parsql database toolkit
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
//! Migration command implementations

use crate::config::Config;
use crate::utils::{self, DatabaseType, Progress};
use crate::MigrateCommands;
use anyhow::{Context, Result};
use colored::Colorize;
use parsql_migrations::prelude::*;
use sha2::{Sha256, Digest};
use std::fs;
use std::path::Path;

pub fn handle_command(
    command: MigrateCommands,
    database_url: &str,
    config: &Config,
    verbose: bool,
) -> Result<()> {
    match command {
        MigrateCommands::Create { name, migration_type } => {
            create_migration(&name, &migration_type, &config.migrations.directory)?;
        }
        
        MigrateCommands::Run { database_url: cmd_url, dry_run, target } => {
            let url = cmd_url.as_deref().unwrap_or(database_url);
            run_migrations(url, config, dry_run, target, verbose)?;
        }
        
        MigrateCommands::Rollback { to, database_url: cmd_url, dry_run } => {
            let url = cmd_url.as_deref().unwrap_or(database_url);
            rollback_migrations(url, config, to, dry_run, verbose)?;
        }
        
        MigrateCommands::Status { database_url: cmd_url, detailed } => {
            let url = cmd_url.as_deref().unwrap_or(database_url);
            show_status(url, config, detailed)?;
        }
        
        MigrateCommands::Validate { check_gaps, verify_checksums } => {
            validate_migrations(&config.migrations.directory, check_gaps, verify_checksums, verbose)?;
        }
        
        MigrateCommands::List { pending, applied } => {
            list_migrations(&config.migrations.directory, pending, applied)?;
        }
    }
    
    Ok(())
}

fn create_migration(name: &str, migration_type: &str, directory: &str) -> Result<()> {
    let timestamp = utils::get_timestamp();
    
    let dir_path = Path::new(directory);
    fs::create_dir_all(dir_path)
        .context("Failed to create migrations directory")?;
    
    let safe_name = name.replace(' ', "_").to_lowercase();
    
    match migration_type {
        "sql" => {
            // Standardized format: {timestamp}_{name}
            let up_file = dir_path.join(format!("{}_{}.up.sql", timestamp, safe_name));
            let down_file = dir_path.join(format!("{}_{}.down.sql", timestamp, safe_name));
            
            let up_content = format!(
                "-- Migration: {}\n-- Version: {}\n-- Created: {}\n\n-- Add your UP migration SQL here\n",
                name,
                timestamp,
                chrono::Local::now().format("%Y-%m-%d %H:%M:%S")
            );
            
            let down_content = format!(
                "-- Migration: {} (rollback)\n-- Version: {}\n-- Created: {}\n\n-- Add your DOWN migration SQL here\n",
                name,
                timestamp,
                chrono::Local::now().format("%Y-%m-%d %H:%M:%S")
            );
            
            fs::write(&up_file, up_content)
                .context("Failed to create up migration file")?;
            fs::write(&down_file, down_content)
                .context("Failed to create down migration file")?;
            
            utils::print_success(&format!("Created SQL migration: {}", safe_name));
            println!("  {}: {}", "UP".green(), up_file.display());
            println!("  {}: {}", "DOWN".red(), down_file.display());
        }
        
        "rust" => {
            // Standardized format: {timestamp}_{name} (remove duplicate timestamp)
            let rust_file = dir_path.join(format!("{}_{}.rs", timestamp, safe_name));
            
            let rust_content = format!(
                r#"//! Migration: {}
//! Version: {}
//! Created: {}

use parsql_migrations::prelude::*;

pub struct Migration{};

impl Migration for Migration{} {{
    fn version(&self) -> i64 {{
        {}
    }}
    
    fn name(&self) -> &str {{
        "{}"
    }}
    
    fn up(&self, conn: &mut dyn MigrationConnection) -> Result<(), MigrationError> {{
        // Add your UP migration logic here
        conn.execute(
            "CREATE TABLE example (
                id SERIAL PRIMARY KEY,
                name VARCHAR(255) NOT NULL
            )"
        )
    }}
    
    fn down(&self, conn: &mut dyn MigrationConnection) -> Result<(), MigrationError> {{
        // Add your DOWN migration logic here
        conn.execute("DROP TABLE IF EXISTS example")
    }}
}}
"#,
                name,               // Migration comment
                timestamp,          // Version comment  
                chrono::Local::now().format("%Y-%m-%d %H:%M:%S"), // Created comment
                timestamp,          // struct name
                timestamp,          // impl name
                timestamp,          // version() return value
                safe_name           // name() return value
            );
            
            fs::write(&rust_file, rust_content)
                .context("Failed to create Rust migration file")?;
            
            utils::print_success(&format!("Created Rust migration: {}", safe_name));
            println!("  {}: {}", "File".cyan(), rust_file.display());
            
            utils::print_info("Remember to add this migration to your build.rs or mod.rs");
        }
        
        _ => anyhow::bail!("Unknown migration type: {}", migration_type),
    }
    
    Ok(())
}

fn run_migrations(
    database_url: &str,
    config: &Config,
    dry_run: bool,
    target: Option<i64>,
    verbose: bool,
) -> Result<()> {
    let db_type = utils::parse_database_url(database_url)?;
    
    if verbose {
        utils::print_info(&format!("Database: {} ({})", database_url, db_type.name()));
    }
    
    let progress = Progress::new("Loading migrations");
    let migrations = load_migrations_from_directory(&config.migrations.directory)?;
    progress.finish_with_message(&format!("{} migrations found", migrations.len()));
    
    if migrations.is_empty() {
        utils::print_warning("No migrations found");
        return Ok(());
    }
    
    if dry_run {
        utils::print_info("DRY RUN - No changes will be applied");
        
        for migration in &migrations {
            println!("Would run: {} - {}", migration.version, migration.name);
        }
        
        return Ok(());
    }
    
    // Run migrations based on database type
    match db_type {
        DatabaseType::PostgreSQL => {
            run_postgres_migrations(database_url, config, migrations, target)?;
        }
        DatabaseType::SQLite => {
            run_sqlite_migrations(database_url, config, migrations, target)?;
        }
    }
    
    Ok(())
}

#[cfg(feature = "postgres")]
fn run_postgres_migrations(
    database_url: &str,
    config: &Config,
    migrations: Vec<FileMigration>,
    target: Option<i64>,
) -> Result<()> {
    use postgres::{Client, NoTls};
    use parsql_migrations::postgres_simple::PostgresMigrationConnection;
    
    let progress = Progress::new("Connecting to PostgreSQL");
    let mut client = Client::connect(database_url, NoTls)
        .context("Failed to connect to PostgreSQL")?;
    progress.finish();
    
    let mut migration_conn = PostgresMigrationConnection::new(&mut client);
    let migration_config = config.to_parsql_migration_config();
    let mut runner = MigrationRunner::with_config(migration_config);
    
    // Add migrations
    for migration in migrations {
        if let Some(target) = target {
            if migration.version > target {
                continue;
            }
        }
        runner.add_migration(Box::new(migration));
    }
    
    // Run migrations
    let progress = Progress::new("Running migrations");
    let report = runner.run(&mut migration_conn)
        .context("Failed to run migrations")?;
    progress.finish();
    
    // Print report
    if report.successful_count() > 0 {
        utils::print_success(&format!("Applied {} migration(s)", report.successful_count()));
    }
    
    if !report.skipped.is_empty() {
        utils::print_info(&format!("Skipped {} migration(s) (already applied)", report.skipped.len()));
    }
    
    if report.failed_count() > 0 {
        utils::print_error(&format!("Failed {} migration(s)", report.failed_count()));
        for result in &report.failed {
            println!("  {} Version {}: {}", 
                "".red(), 
                result.version, 
                result.error.as_ref().unwrap_or(&"Unknown error".to_string())
            );
        }
        anyhow::bail!("Some migrations failed");
    }
    
    Ok(())
}

#[cfg(feature = "sqlite")]
fn run_sqlite_migrations(
    database_url: &str,
    config: &Config,
    migrations: Vec<FileMigration>,
    target: Option<i64>,
) -> Result<()> {
    use rusqlite::Connection;
    use parsql_migrations::sqlite_simple::SqliteMigrationConnection;
    
    let db_path = database_url.strip_prefix("sqlite:").unwrap_or(database_url);
    
    let progress = Progress::new("Opening SQLite database");
    let mut conn = Connection::open(db_path)
        .context("Failed to open SQLite database")?;
    progress.finish();
    
    let mut migration_conn = SqliteMigrationConnection::new(&mut conn);
    let migration_config = config.to_parsql_migration_config();
    let mut runner = MigrationRunner::with_config(migration_config);
    
    // Add migrations
    for migration in migrations {
        if let Some(target) = target {
            if migration.version > target {
                continue;
            }
        }
        runner.add_migration(Box::new(migration));
    }
    
    // Run migrations
    let progress = Progress::new("Running migrations");
    let report = runner.run(&mut migration_conn)
        .context("Failed to run migrations")?;
    progress.finish();
    
    // Print report
    if report.successful_count() > 0 {
        utils::print_success(&format!("Applied {} migration(s)", report.successful_count()));
    }
    
    if !report.skipped.is_empty() {
        utils::print_info(&format!("Skipped {} migration(s) (already applied)", report.skipped.len()));
    }
    
    if report.failed_count() > 0 {
        utils::print_error(&format!("Failed {} migration(s)", report.failed_count()));
        for result in &report.failed {
            println!("  {} Version {}: {}", 
                "".red(), 
                result.version, 
                result.error.as_ref().unwrap_or(&"Unknown error".to_string())
            );
        }
        anyhow::bail!("Some migrations failed");
    }
    
    Ok(())
}

fn rollback_migrations(
    database_url: &str,
    config: &Config,
    target_version: i64,
    dry_run: bool,
    verbose: bool,
) -> Result<()> {
    let db_type = utils::parse_database_url(database_url)?;
    
    if verbose {
        utils::print_info(&format!("Database: {} ({})", database_url, db_type.name()));
    }
    
    utils::print_info(&format!("Rolling back to version: {}", target_version));
    
    let progress = Progress::new("Loading migrations");
    let migrations = load_migrations_from_directory(&config.migrations.directory)?;
    progress.finish_with_message(&format!("{} migrations found", migrations.len()));
    
    if dry_run {
        utils::print_info("DRY RUN - No changes will be applied");
        utils::print_warning("Note: Cannot determine which migrations would be rolled back without database connection");
        return Ok(());
    }
    
    // Run rollback based on database type
    match db_type {
        DatabaseType::PostgreSQL => {
            rollback_postgres_migrations(database_url, config, migrations, target_version)?;
        }
        DatabaseType::SQLite => {
            rollback_sqlite_migrations(database_url, config, migrations, target_version)?;
        }
    }
    
    Ok(())
}

fn show_status(
    database_url: &str,
    config: &Config,
    detailed: bool,
) -> Result<()> {
    let db_type = utils::parse_database_url(database_url)?;
    
    utils::print_info(&format!("Database: {} ({})", database_url, db_type.name()));
    
    let progress = Progress::new("Loading migrations");
    let migrations = load_migrations_from_directory(&config.migrations.directory)?;
    progress.finish_with_message(&format!("{} migrations found", migrations.len()));
    
    // Get status based on database type
    match db_type {
        DatabaseType::PostgreSQL => {
            show_postgres_status(database_url, config, migrations, detailed)?;
        }
        DatabaseType::SQLite => {
            show_sqlite_status(database_url, config, migrations, detailed)?;
        }
    }
    
    Ok(())
}

fn validate_migrations(
    directory: &str,
    check_gaps: bool,
    verify_checksums: bool,
    verbose: bool,
) -> Result<()> {
    let migrations = load_migrations_from_directory(directory)?;
    
    if migrations.is_empty() {
        utils::print_warning("No migrations found");
        return Ok(());
    }
    
    utils::print_info(&format!("Found {} migration(s)", migrations.len()));
    
    // Check for version gaps
    if check_gaps {
        let mut versions: Vec<i64> = migrations.iter().map(|m| m.version).collect();
        versions.sort();
        
        let mut has_gaps = false;
        for i in 1..versions.len() {
            if versions[i] - versions[i-1] > 1 {
                utils::print_warning(&format!(
                    "Gap detected between versions {} and {}", 
                    versions[i-1], 
                    versions[i]
                ));
                has_gaps = true;
            }
        }
        
        if !has_gaps {
            utils::print_success("No version gaps found");
        }
    }
    
    if verify_checksums {
        utils::print_info("Verifying migration checksums...");
        
        let checksum_errors = 0;
        for migration in &migrations {
            let calculated_checksum = calculate_migration_checksum(migration);
            
            // For now, just show the checksum (we'll add comparison with DB later)
            if verbose {
                println!("  {} - {}: {}", 
                    migration.version, 
                    migration.name, 
                    &calculated_checksum[..8]
                );
            }
        }
        
        if checksum_errors == 0 {
            utils::print_success("All checksums verified");
        } else {
            utils::print_error(&format!("{} checksum error(s) found", checksum_errors));
        }
    }
    
    Ok(())
}

fn list_migrations(
    directory: &str,
    pending_only: bool,
    applied_only: bool,
) -> Result<()> {
    let migrations = load_migrations_from_directory(directory)?;
    
    if migrations.is_empty() {
        utils::print_warning("No migrations found");
        return Ok(());
    }
    
    println!("{}", "Available Migrations:".bold());
    println!();
    
    let headers = vec!["Version", "Name", "Type"];
    let mut rows = Vec::new();
    
    for migration in migrations {
        rows.push(vec![
            migration.version.to_string(),
            migration.name.clone(),
            migration.migration_type.clone(),
        ]);
    }
    
    print!("{}", utils::format_table(headers, rows));
    
    if pending_only || applied_only {
        utils::print_info("Filtering by status requires database connection (not yet implemented)");
    }
    
    Ok(())
}

#[cfg(feature = "postgres")]
fn show_postgres_status(
    database_url: &str,
    config: &Config,
    migrations: Vec<FileMigration>,
    detailed: bool,
) -> Result<()> {
    use postgres::{Client, NoTls};
    use parsql_migrations::postgres_simple::PostgresMigrationConnection;
    
    let progress = Progress::new("Connecting to PostgreSQL");
    let mut client = Client::connect(database_url, NoTls)
        .context("Failed to connect to PostgreSQL")?;
    progress.finish();
    
    let mut migration_conn = PostgresMigrationConnection::new(&mut client);
    
    // Get applied migrations
    let records = migration_conn.query_migrations(&config.migrations.table_name)
        .context("Failed to fetch applied migrations")?;
    
    // Convert to map for easy lookup
    let mut applied = std::collections::HashMap::new();
    for record in records {
        applied.insert(record.version, record);
    }
    
    let total_count = migrations.len();
    let applied_count = applied.len();
    let pending_count = migrations.iter()
        .filter(|m| !applied.contains_key(&m.version))
        .count();
    
    // Print summary
    println!();
    println!("{}", "Migration Status:".bold());
    println!("  {} migrations", utils::colorize_number(total_count, "Total"));
    println!("  {} migrations", utils::colorize_number(applied_count, "Applied").green());
    println!("  {} migrations", utils::colorize_number(pending_count, "Pending").yellow());
    
    if detailed {
        println!();
        println!("{}", "Detailed Status:".bold());
        
        let headers = vec!["Version", "Name", "Status", "Applied At", "Checksum"];
        let mut rows = Vec::new();
        
        for migration in migrations {
            let status;
            let applied_at;
            
            if let Some(record) = applied.get(&migration.version) {
                status = "Applied".green().to_string();
                applied_at = record.applied_at.format("%Y-%m-%d %H:%M:%S").to_string();
            } else {
                status = "Pending".yellow().to_string();
                applied_at = "-".to_string();
            }
            
            let checksum_status = if let Some(record) = applied.get(&migration.version) {
                let calculated_checksum = calculate_migration_checksum(&migration);
                if let Some(ref stored_checksum) = record.checksum {
                    if stored_checksum == &calculated_checksum {
                        "".green().to_string()
                    } else {
                        format!("✗ Mismatch").red().to_string()
                    }
                } else {
                    "No checksum".dimmed().to_string()
                }
            } else {
                "-".to_string()
            };
            
            rows.push(vec![
                migration.version.to_string(),
                migration.name.clone(),
                status,
                applied_at,
                checksum_status,
            ]);
        }
        
        print!("{}", utils::format_table(headers, rows));
    }
    
    Ok(())
}

#[cfg(feature = "sqlite")]
fn show_sqlite_status(
    database_url: &str,
    config: &Config,
    migrations: Vec<FileMigration>,
    detailed: bool,
) -> Result<()> {
    use rusqlite::Connection;
    use parsql_migrations::sqlite_simple::SqliteMigrationConnection;
    
    let db_path = database_url.strip_prefix("sqlite:").unwrap_or(database_url);
    
    let progress = Progress::new("Opening SQLite database");
    let mut conn = Connection::open(db_path)
        .context("Failed to open SQLite database")?;
    progress.finish();
    
    let mut migration_conn = SqliteMigrationConnection::new(&mut conn);
    
    // Get applied migrations
    let records = migration_conn.query_migrations(&config.migrations.table_name)
        .context("Failed to fetch applied migrations")?;
    
    // Convert to map for easy lookup
    let mut applied = std::collections::HashMap::new();
    for record in records {
        applied.insert(record.version, record);
    }
    
    let total_count = migrations.len();
    let applied_count = applied.len();
    let pending_count = migrations.iter()
        .filter(|m| !applied.contains_key(&m.version))
        .count();
    
    // Print summary
    println!();
    println!("{}", "Migration Status:".bold());
    println!("  {} migrations", utils::colorize_number(total_count, "Total"));
    println!("  {} migrations", utils::colorize_number(applied_count, "Applied").green());
    println!("  {} migrations", utils::colorize_number(pending_count, "Pending").yellow());
    
    if detailed {
        println!();
        println!("{}", "Detailed Status:".bold());
        
        let headers = vec!["Version", "Name", "Status", "Applied At", "Checksum"];
        let mut rows = Vec::new();
        
        for migration in migrations {
            let status;
            let applied_at;
            
            if let Some(record) = applied.get(&migration.version) {
                status = "Applied".green().to_string();
                applied_at = record.applied_at.format("%Y-%m-%d %H:%M:%S").to_string();
            } else {
                status = "Pending".yellow().to_string();
                applied_at = "-".to_string();
            }
            
            let checksum_status = if let Some(record) = applied.get(&migration.version) {
                let calculated_checksum = calculate_migration_checksum(&migration);
                if let Some(ref stored_checksum) = record.checksum {
                    if stored_checksum == &calculated_checksum {
                        "".green().to_string()
                    } else {
                        format!("✗ Mismatch").red().to_string()
                    }
                } else {
                    "No checksum".dimmed().to_string()
                }
            } else {
                "-".to_string()
            };
            
            rows.push(vec![
                migration.version.to_string(),
                migration.name.clone(),
                status,
                applied_at,
                checksum_status,
            ]);
        }
        
        print!("{}", utils::format_table(headers, rows));
    }
    
    Ok(())
}

// Helper structures and functions

fn calculate_migration_checksum(migration: &FileMigration) -> String {
    let mut hasher = Sha256::new();
    hasher.update(migration.version.to_string());
    hasher.update(&migration.name);
    
    if let Some(ref up_sql) = migration.up_sql {
        hasher.update(up_sql);
    }
    if let Some(ref down_sql) = migration.down_sql {
        hasher.update(down_sql);
    }
    
    format!("{:x}", hasher.finalize())
}

struct FileMigration {
    version: i64,
    name: String,
    migration_type: String,
    up_sql: Option<String>,
    down_sql: Option<String>,
}

impl Migration for FileMigration {
    fn version(&self) -> i64 {
        self.version
    }
    
    fn name(&self) -> &str {
        &self.name
    }
    
    fn up(&self, conn: &mut dyn MigrationConnection) -> Result<(), MigrationError> {
        if let Some(ref sql) = self.up_sql {
            conn.execute(sql)?;
        }
        Ok(())
    }
    
    fn down(&self, conn: &mut dyn MigrationConnection) -> Result<(), MigrationError> {
        if let Some(ref sql) = self.down_sql {
            conn.execute(sql)?;
        }
        Ok(())
    }
    
    fn checksum(&self) -> String {
        calculate_migration_checksum(self)
    }
}

fn load_migrations_from_directory(directory: &str) -> Result<Vec<FileMigration>> {
    let dir_path = Path::new(directory);
    
    if !dir_path.exists() {
        return Ok(Vec::new());
    }
    
    let mut migrations = Vec::new();
    
    for entry in fs::read_dir(dir_path)? {
        let entry = entry?;
        let path = entry.path();
        
        if let Some(file_name) = path.file_name() {
            let file_name_str = file_name.to_string_lossy();
            
            // Parse SQL migrations
            if file_name_str.ends_with(".up.sql") {
                let base_name = file_name_str.trim_end_matches(".up.sql");
                let parts: Vec<&str> = base_name.splitn(3, '_').collect();
                
                if parts.len() >= 3 {
                    let version = parts[0].parse::<i64>()
                        .context("Failed to parse migration version")?;
                    let name = parts[2].to_string();
                    
                    let up_sql = fs::read_to_string(&path)
                        .context("Failed to read up migration file")?;
                    
                    let down_path = path.with_file_name(format!("{}.down.sql", base_name));
                    let down_sql = if down_path.exists() {
                        Some(fs::read_to_string(&down_path)
                            .context("Failed to read down migration file")?)
                    } else {
                        None
                    };
                    
                    migrations.push(FileMigration {
                        version,
                        name,
                        migration_type: "SQL".to_string(),
                        up_sql: Some(up_sql),
                        down_sql,
                    });
                }
            }
            
            // TODO: Parse Rust migrations
        }
    }
    
    migrations.sort_by_key(|m| m.version);
    Ok(migrations)
}

#[cfg(feature = "postgres")]
fn rollback_postgres_migrations(
    database_url: &str,
    config: &Config,
    migrations: Vec<FileMigration>,
    target_version: i64,
) -> Result<()> {
    use postgres::{Client, NoTls};
    use parsql_migrations::postgres_simple::PostgresMigrationConnection;
    
    let progress = Progress::new("Connecting to PostgreSQL");
    let mut client = Client::connect(database_url, NoTls)
        .context("Failed to connect to PostgreSQL")?;
    progress.finish();
    
    let mut migration_conn = PostgresMigrationConnection::new(&mut client);
    let migration_config = config.to_parsql_migration_config();
    let mut runner = MigrationRunner::with_config(migration_config);
    
    // Add all migrations
    for migration in migrations {
        runner.add_migration(Box::new(migration));
    }
    
    // Perform rollback
    let progress = Progress::new("Rolling back migrations");
    let report = runner.rollback(&mut migration_conn, target_version)
        .context("Failed to rollback migrations")?;
    progress.finish();
    
    // Print report
    if report.successful_count() > 0 {
        utils::print_success(&format!("Rolled back {} migration(s)", report.successful_count()));
        for result in &report.successful {
            println!("  {} Version {} - {}", "".cyan(), result.version, result.name);
        }
    } else {
        utils::print_info("No migrations to roll back");
    }
    
    if report.failed_count() > 0 {
        utils::print_error(&format!("Failed to rollback {} migration(s)", report.failed_count()));
        for result in &report.failed {
            println!("  {} Version {}: {}", 
                "".red(), 
                result.version, 
                result.error.as_ref().unwrap_or(&"Unknown error".to_string())
            );
        }
        anyhow::bail!("Some rollbacks failed");
    }
    
    Ok(())
}

#[cfg(feature = "sqlite")]
fn rollback_sqlite_migrations(
    database_url: &str,
    config: &Config,
    migrations: Vec<FileMigration>,
    target_version: i64,
) -> Result<()> {
    use rusqlite::Connection;
    use parsql_migrations::sqlite_simple::SqliteMigrationConnection;
    
    let db_path = database_url.strip_prefix("sqlite:").unwrap_or(database_url);
    
    let progress = Progress::new("Opening SQLite database");
    let mut conn = Connection::open(db_path)
        .context("Failed to open SQLite database")?;
    progress.finish();
    
    let mut migration_conn = SqliteMigrationConnection::new(&mut conn);
    let migration_config = config.to_parsql_migration_config();
    let mut runner = MigrationRunner::with_config(migration_config);
    
    // Add all migrations
    for migration in migrations {
        runner.add_migration(Box::new(migration));
    }
    
    // Perform rollback
    let progress = Progress::new("Rolling back migrations");
    let report = runner.rollback(&mut migration_conn, target_version)
        .context("Failed to rollback migrations")?;
    progress.finish();
    
    // Print report
    if report.successful_count() > 0 {
        utils::print_success(&format!("Rolled back {} migration(s)", report.successful_count()));
        for result in &report.successful {
            println!("  {} Version {} - {}", "".cyan(), result.version, result.name);
        }
    } else {
        utils::print_info("No migrations to roll back");
    }
    
    if report.failed_count() > 0 {
        utils::print_error(&format!("Failed to rollback {} migration(s)", report.failed_count()));
        for result in &report.failed {
            println!("  {} Version {}: {}", 
                "".red(), 
                result.version, 
                result.error.as_ref().unwrap_or(&"Unknown error".to_string())
            );
        }
        anyhow::bail!("Some rollbacks failed");
    }
    
    Ok(())
}