migratio 0.4.2

Write expressive database migrations in Rust.
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
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
//!
//! # PostgreSQL migration support
//!
//! This module provides PostgreSQL migration support using the [`postgres`](https://crates.io/crates/postgres) crate.
//!
//! ## Transaction Safety
//!
//! PostgreSQL fully supports transactional DDL (unlike MySQL). Each migration runs within
//! its own transaction. If a migration fails (returns an error or panics), the transaction
//! is automatically rolled back, leaving the database in the state it was in after the
//! last successful migration.
//!
//! ## Comparison with SQLite and MySQL
//!
//! | Behavior | SQLite | MySQL | PostgreSQL |
//! |----------|--------|-------|------------|
//! | DDL in transactions | Fully supported | Causes implicit commit | Fully supported |
//! | Migration failure | Complete rollback | Partial DDL may persist | Complete rollback |
//! | Method parameter | `&Transaction` | `&mut Conn` | `&mut Transaction` |
//! | Automatic cleanup | Yes | Manual intervention may be needed | Yes |
//!
//! ## Exceptions
//!
//! The following operations cannot be rolled back even in PostgreSQL:
//! - `CREATE DATABASE` / `DROP DATABASE`
//! - `CREATE TABLESPACE` / `DROP TABLESPACE`
//!
//! Avoid these operations in migrations if possible.
//!
//! ## Example
//!
//! ```ignore
//! use migratio::{Migration, MigrationReport, Error};
//! use migratio::postgres::PostgresMigrator;
//! use postgres::{Client, NoTls, Transaction};
//!
//! // Define your migrations as structs that implement the Migration trait
//! struct Migration1;
//! impl Migration for Migration1 {
//!     fn version(&self) -> u32 {
//!         1
//!     }
//!     fn postgres_up(&self, tx: &mut Transaction) -> Result<(), Error> {
//!         tx.execute("CREATE TABLE users (id SERIAL PRIMARY KEY, name TEXT)", &[])?;
//!         Ok(())
//!     }
//! }
//!
//! struct Migration2;
//! impl Migration for Migration2 {
//!     fn version(&self) -> u32 {
//!         2
//!     }
//!     fn postgres_up(&self, tx: &mut Transaction) -> Result<(), Error> {
//!         tx.execute("ALTER TABLE users ADD COLUMN email TEXT", &[])?;
//!         Ok(())
//!     }
//! }
//!
//! // Construct a migrator with migrations
//! let migrator = PostgresMigrator::new(vec![Box::new(Migration1), Box::new(Migration2)]);
//!
//! // Connect to your database and run the migrations
//! let mut client = Client::connect("postgres://user:password@localhost/mydb", NoTls).unwrap();
//! let report = migrator.upgrade(&mut client).unwrap();
//! ```

use crate::core::{AppliedMigrationRow, GenericMigrator, MigrationBackend, MigrationType};
use crate::error::Error;
use crate::AppliedMigration;
use crate::Migration;
use crate::MigrationReport;
use crate::Precondition;
use chrono::Utc;
use postgres::Client;

// Re-export postgres types for use in migrations
pub use postgres::Client as PostgresClient;
pub use postgres::Transaction as PostgresTransaction;

/// PostgreSQL-specific backend implementing the MigrationBackend trait.
pub(crate) struct PostgresBackend;

impl MigrationBackend for PostgresBackend {
    type Conn = Client;

    fn version_table_exists(conn: &mut Client, table_name: &str) -> Result<bool, Error> {
        let exists: bool = conn
            .query_one(
                "SELECT EXISTS (SELECT FROM information_schema.tables WHERE table_schema = 'public' AND table_name = $1)",
                &[&table_name],
            )?
            .get(0);
        Ok(exists)
    }

    fn create_version_table(conn: &mut Client, table_name: &str) -> Result<(), Error> {
        conn.execute(
            &format!(
                "CREATE TABLE IF NOT EXISTS {} (
                    version INTEGER PRIMARY KEY NOT NULL,
                    name TEXT NOT NULL,
                    applied_at TEXT NOT NULL,
                    checksum TEXT NOT NULL,
                    migration_type TEXT NOT NULL DEFAULT 'migration'
                )",
                table_name
            ),
            &[],
        )?;
        Ok(())
    }

    fn column_exists(
        conn: &mut Client,
        table_name: &str,
        column_name: &str,
    ) -> Result<bool, Error> {
        let exists: bool = conn
            .query_one(
                "SELECT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema = 'public' AND table_name = $1 AND column_name = $2)",
                &[&table_name, &column_name],
            )?
            .get(0);
        Ok(exists)
    }

    fn add_column(
        conn: &mut Client,
        table_name: &str,
        column_name: &str,
        column_def: &str,
    ) -> Result<(), Error> {
        conn.execute(
            &format!(
                "ALTER TABLE {} ADD COLUMN {} {}",
                table_name, column_name, column_def
            ),
            &[],
        )?;
        Ok(())
    }

    fn get_applied_migration_rows(
        conn: &mut Client,
        table_name: &str,
    ) -> Result<Vec<AppliedMigrationRow>, Error> {
        let rows = conn.query(
            &format!("SELECT version, name, checksum FROM {}", table_name),
            &[],
        )?;
        let result = rows
            .into_iter()
            .map(|row| {
                let version: i32 = row.get(0);
                Ok(AppliedMigrationRow {
                    version: version as u32,
                    name: row.get(1),
                    checksum: row.get(2),
                })
            })
            .collect::<Result<Vec<_>, Error>>()?;
        Ok(result)
    }

    fn get_max_version(conn: &mut Client, table_name: &str) -> Result<u32, Error> {
        let row = conn.query_one(
            &format!("SELECT COALESCE(MAX(version), 0) FROM {}", table_name),
            &[],
        )?;
        let version: i32 = row.get(0);
        Ok(version as u32)
    }

    fn get_migration_history_rows(
        conn: &mut Client,
        table_name: &str,
    ) -> Result<Vec<AppliedMigration>, Error> {
        // Check whether the migration_type column exists for backwards compatibility
        let has_migration_type: bool = conn
            .query_one(
                "SELECT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_schema = 'public' AND table_name = $1 AND column_name = 'migration_type')",
                &[&table_name],
            )?
            .get(0);

        let rows = if has_migration_type {
            conn.query(
                &format!(
                    "SELECT version, name, applied_at, checksum, migration_type FROM {} ORDER BY version",
                    table_name
                ),
                &[],
            )?
        } else {
            conn.query(
                &format!(
                    "SELECT version, name, applied_at, checksum FROM {} ORDER BY version",
                    table_name
                ),
                &[],
            )?
        };

        let migrations = rows
            .into_iter()
            .map(|row| {
                let version: i32 = row.get(0);
                let name: String = row.get(1);
                let applied_at_str: String = row.get(2);
                let checksum: String = row.get(3);

                let applied_at = chrono::DateTime::parse_from_rfc3339(&applied_at_str)
                    .map_err(|e| Error::Generic(format!("Failed to parse datetime: {}", e)))?
                    .with_timezone(&Utc);

                let migration_type = if has_migration_type {
                    let migration_type_str: String = row.get(4);
                    if migration_type_str == "baseline" {
                        MigrationType::Baseline
                    } else {
                        MigrationType::Migration
                    }
                } else {
                    MigrationType::Migration
                };

                Ok(AppliedMigration {
                    version: version as u32,
                    name,
                    applied_at,
                    checksum,
                    migration_type,
                })
            })
            .collect::<Result<Vec<_>, Error>>()?;

        Ok(migrations)
    }

    fn execute_migration_up(
        conn: &mut Client,
        migration: &Box<dyn Migration>,
        table_name: &str,
        applied_at: &str,
        checksum: &str,
        migration_type: MigrationType,
    ) -> Result<bool, Error> {
        // Start a transaction for this migration
        let mut tx = conn.transaction()?;

        // Check precondition
        let precondition = migration.postgres_precondition(&mut tx)?;

        match precondition {
            Precondition::AlreadySatisfied => {
                // Stamp the migration without running up(), then commit
                tx.execute(
                    &format!(
                        "INSERT INTO {} (version, name, applied_at, checksum, migration_type) VALUES($1, $2, $3, $4, $5)",
                        table_name
                    ),
                    &[
                        &(migration.version() as i32),
                        &migration.name(),
                        &applied_at,
                        &checksum,
                        &migration_type.to_string(),
                    ],
                )?;
                tx.commit()?;
                Ok(false)
            }
            Precondition::NeedsApply => {
                migration.postgres_up(&mut tx)?;
                // Insert version row inside the same transaction (atomic with migration)
                tx.execute(
                    &format!(
                        "INSERT INTO {} (version, name, applied_at, checksum, migration_type) VALUES($1, $2, $3, $4, $5)",
                        table_name
                    ),
                    &[
                        &(migration.version() as i32),
                        &migration.name(),
                        &applied_at,
                        &checksum,
                        &migration_type.to_string(),
                    ],
                )?;
                tx.commit()?;
                Ok(true)
            }
        }
    }

    fn execute_migration_down(
        conn: &mut Client,
        migration: &Box<dyn Migration>,
        table_name: &str,
    ) -> Result<(), Error> {
        // Start a transaction for this migration rollback
        let mut tx = conn.transaction()?;
        migration.postgres_down(&mut tx)?;
        // Delete version row inside the same transaction (atomic with rollback)
        tx.execute(
            &format!("DELETE FROM {} WHERE version = $1", table_name),
            &[&(migration.version() as i32)],
        )?;
        tx.commit()?;
        Ok(())
    }
}

/// The entrypoint for running a sequence of [Migration]s on a PostgreSQL database.
/// Construct this struct with the list of all [Migration]s to be applied.
/// [Migration::version]s must be contiguous, greater than zero, and unique.
///
/// ## Transaction Safety
///
/// Each migration runs within its own PostgreSQL transaction. If a migration fails,
/// all changes from that migration are automatically rolled back, leaving the database
/// in a consistent state.
#[derive(Debug)]
pub struct PostgresMigrator {
    migrator: GenericMigrator,
}

impl PostgresMigrator {
    /// Create a new PostgresMigrator, validating migration invariants.
    /// Returns an error if migrations are invalid.
    pub fn try_new(migrations: Vec<Box<dyn Migration>>) -> Result<Self, String> {
        Ok(Self {
            migrator: GenericMigrator::try_new(migrations)?,
        })
    }

    /// Create a new PostgresMigrator, panicking if migration metadata is invalid.
    /// For a non-panicking version, use `try_new`.
    pub fn new(migrations: Vec<Box<dyn Migration>>) -> Self {
        match Self::try_new(migrations) {
            Ok(migrator) => migrator,
            Err(err) => panic!("{}", err),
        }
    }

    /// Set a custom name for the schema version tracking table.
    /// Defaults to "_migratio_version_".
    pub fn with_schema_version_table_name(mut self, name: impl Into<String>) -> Self {
        self.migrator.set_schema_version_table_name(name);
        self
    }

    /// Set a callback to be invoked when a migration starts.
    /// The callback receives the migration version and name.
    pub fn on_migration_start<F>(mut self, callback: F) -> Self
    where
        F: Fn(u32, &str) + Send + Sync + 'static,
    {
        self.migrator.set_on_migration_start(callback);
        self
    }

    /// Set a callback to be invoked when a migration completes successfully.
    /// The callback receives the migration version, name, and duration.
    pub fn on_migration_complete<F>(mut self, callback: F) -> Self
    where
        F: Fn(u32, &str, std::time::Duration) + Send + Sync + 'static,
    {
        self.migrator.set_on_migration_complete(callback);
        self
    }

    /// Set a callback to be invoked when a migration is skipped because its precondition
    /// returned [`Precondition::AlreadySatisfied`].
    /// The callback receives the migration version and name.
    pub fn on_migration_skipped<F>(mut self, callback: F) -> Self
    where
        F: Fn(u32, &str) + Send + Sync + 'static,
    {
        self.migrator.set_on_migration_skipped(callback);
        self
    }

    /// Set a callback to be invoked when a migration fails.
    /// The callback receives the migration version, name, and error.
    pub fn on_migration_error<F>(mut self, callback: F) -> Self
    where
        F: Fn(u32, &str, &Error) + Send + Sync + 'static,
    {
        self.migrator.set_on_migration_error(callback);
        self
    }

    /// Get a reference to all migrations in this migrator.
    pub fn migrations(&self) -> &[Box<dyn Migration>] {
        &self.migrator.migrations
    }

    pub fn schema_version_table_name(&self) -> &str {
        &self.migrator.schema_version_table_name
    }

    /// Get the current migration version from the database.
    /// Returns 0 if no migrations have been applied.
    pub fn get_current_version(&self, client: &mut Client) -> Result<u32, Error> {
        self.migrator.generic_get_current_version::<PostgresBackend>(client)
    }

    /// Get the history of all migrations that have been applied to the database.
    /// Returns migrations in the order they were applied (by version number).
    /// Returns an empty vector if no migrations have been applied.
    pub fn get_migration_history(
        &self,
        client: &mut Client,
    ) -> Result<Vec<AppliedMigration>, Error> {
        self.migrator.generic_get_migration_history::<PostgresBackend>(client)
    }

    /// Preview which migrations would be applied by `upgrade()` without actually running them.
    /// Returns a list of migrations that would be executed, in the order they would run.
    pub fn preview_upgrade(&self, client: &mut Client) -> Result<Vec<&Box<dyn Migration>>, Error> {
        self.migrator.generic_preview_upgrade::<PostgresBackend>(client)
    }

    /// Preview which migrations would be rolled back by `downgrade(target_version)` without actually running them.
    /// Returns a list of migrations that would be executed, in the order they would run (reverse order).
    pub fn preview_downgrade(
        &self,
        client: &mut Client,
        target_version: u32,
    ) -> Result<Vec<&Box<dyn Migration>>, Error> {
        self.migrator
            .generic_preview_downgrade::<PostgresBackend>(client, target_version)
    }

    /// Upgrade the database to a specific target version.
    ///
    /// This runs all pending migrations up to and including the target version.
    /// If the database is already at or beyond the target version, no migrations are run.
    pub fn upgrade_to(
        &self,
        client: &mut Client,
        target_version: u32,
    ) -> Result<MigrationReport<'_>, Error> {
        // Validate target version exists
        if target_version > 0
            && !self
                .migrations()
                .iter()
                .any(|m| m.version() == target_version)
        {
            return Err(Error::Generic(format!(
                "Target version {} does not exist in migration list",
                target_version
            )));
        }

        self.migrator
            .generic_upgrade::<PostgresBackend>(client, Some(target_version))
    }

    /// Upgrade the database by running all pending migrations.
    pub fn upgrade(&self, client: &mut Client) -> Result<MigrationReport<'_>, Error> {
        self.migrator.generic_upgrade::<PostgresBackend>(client, None)
    }

    /// Rollback migrations down to the specified target version.
    /// Pass `target_version = 0` to rollback all migrations.
    /// Each migration's `down()` method runs within its own transaction, which is automatically rolled back if it fails.
    /// Returns a [MigrationReport] describing which migrations were rolled back.
    pub fn downgrade(
        &self,
        client: &mut Client,
        target_version: u32,
    ) -> Result<MigrationReport<'_>, Error> {
        self.migrator
            .generic_downgrade::<PostgresBackend>(client, target_version)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::test_postgres::get_test_client;

    #[test]
    fn single_successful_from_clean() {
        use chrono::{DateTime, FixedOffset};

        let mut client = get_test_client();

        struct Migration1;
        impl Migration for Migration1 {
            fn version(&self) -> u32 {
                1
            }
            fn postgres_up(&self, tx: &mut postgres::Transaction) -> Result<(), Error> {
                tx.execute("CREATE TABLE test (id SERIAL PRIMARY KEY)", &[])?;
                Ok(())
            }
            #[cfg(feature = "sqlite")]
            fn sqlite_up(&self, _tx: &rusqlite::Transaction) -> Result<(), Error> {
                Ok(())
            }
            #[cfg(feature = "mysql")]
            fn mysql_up(&self, _conn: &mut mysql::Conn) -> Result<(), Error> {
                Ok(())
            }
        }

        let migrator = PostgresMigrator::new(vec![Box::new(Migration1)]);
        let report = migrator.upgrade(&mut client).unwrap();

        assert_eq!(
            report,
            MigrationReport {
                schema_version_table_existed: false,
                schema_version_table_created: true,
                migrations_run: vec![1],
                failing_migration: None,
            }
        );

        // Verify schema version table exists and has recorded version 1
        let rows = client
            .query(
                "SELECT version, name, applied_at FROM _migratio_version_",
                &[],
            )
            .unwrap();

        assert_eq!(rows.len(), 1);
        let version: i32 = rows[0].get(0);
        let name: String = rows[0].get(1);
        let applied_at_str: String = rows[0].get(2);

        assert_eq!(version, 1);
        assert_eq!(name, "Migration 1"); // default name

        let date = DateTime::parse_from_rfc3339(&applied_at_str).unwrap();
        assert_eq!(date.timezone(), FixedOffset::east_opt(0).unwrap());

        // Ensure that the date is within 5 seconds of now
        let now = Utc::now();
        let diff = now.timestamp() - date.timestamp();
        assert!(diff < 5);
    }

    #[test]
    fn single_unsuccessful_from_clean_with_rollback() {
        // This test verifies PostgreSQL's transactional DDL - the table should NOT exist after failure
        let mut client = get_test_client();

        struct Migration1;
        impl Migration for Migration1 {
            fn version(&self) -> u32 {
                1
            }
            fn postgres_up(&self, tx: &mut postgres::Transaction) -> Result<(), Error> {
                // Create table first
                tx.execute("CREATE TABLE test (id SERIAL PRIMARY KEY, value INT)", &[])?;
                // Then do something that fails
                tx.execute("THIS IS NOT VALID SQL", &[])?;
                Ok(())
            }
            #[cfg(feature = "sqlite")]
            fn sqlite_up(&self, _tx: &rusqlite::Transaction) -> Result<(), Error> {
                Ok(())
            }
            #[cfg(feature = "mysql")]
            fn mysql_up(&self, _conn: &mut mysql::Conn) -> Result<(), Error> {
                Ok(())
            }
        }

        let migrator = PostgresMigrator::new(vec![Box::new(Migration1)]);
        let report = migrator.upgrade(&mut client).unwrap();

        // Verify migration failed
        assert_eq!(report.migrations_run, Vec::<u32>::new());
        assert!(report.failing_migration.is_some());

        // CRITICAL: Verify table was rolled back (unlike MySQL where it would persist)
        let table_exists: bool = client
            .query_one(
                "SELECT EXISTS (SELECT FROM information_schema.tables WHERE table_schema = 'public' AND table_name = 'test')",
                &[],
            )
            .unwrap()
            .get(0);
        assert!(
            !table_exists,
            "Table should NOT exist due to transaction rollback"
        );
    }

    #[test]
    fn upgrade_to_specific_version() {
        let mut client = get_test_client();

        struct Migration1;
        impl Migration for Migration1 {
            fn version(&self) -> u32 {
                1
            }
            fn postgres_up(&self, tx: &mut postgres::Transaction) -> Result<(), Error> {
                tx.execute("CREATE TABLE users (id SERIAL PRIMARY KEY)", &[])?;
                Ok(())
            }
            #[cfg(feature = "sqlite")]
            fn sqlite_up(&self, _tx: &rusqlite::Transaction) -> Result<(), Error> {
                Ok(())
            }
            #[cfg(feature = "mysql")]
            fn mysql_up(&self, _conn: &mut mysql::Conn) -> Result<(), Error> {
                Ok(())
            }
        }

        struct Migration2;
        impl Migration for Migration2 {
            fn version(&self) -> u32 {
                2
            }
            fn postgres_up(&self, tx: &mut postgres::Transaction) -> Result<(), Error> {
                tx.execute("CREATE TABLE posts (id SERIAL PRIMARY KEY)", &[])?;
                Ok(())
            }
            #[cfg(feature = "sqlite")]
            fn sqlite_up(&self, _tx: &rusqlite::Transaction) -> Result<(), Error> {
                Ok(())
            }
            #[cfg(feature = "mysql")]
            fn mysql_up(&self, _conn: &mut mysql::Conn) -> Result<(), Error> {
                Ok(())
            }
        }

        struct Migration3;
        impl Migration for Migration3 {
            fn version(&self) -> u32 {
                3
            }
            fn postgres_up(&self, tx: &mut postgres::Transaction) -> Result<(), Error> {
                tx.execute("CREATE TABLE comments (id SERIAL PRIMARY KEY)", &[])?;
                Ok(())
            }
            #[cfg(feature = "sqlite")]
            fn sqlite_up(&self, _tx: &rusqlite::Transaction) -> Result<(), Error> {
                Ok(())
            }
            #[cfg(feature = "mysql")]
            fn mysql_up(&self, _conn: &mut mysql::Conn) -> Result<(), Error> {
                Ok(())
            }
        }

        let migrator = PostgresMigrator::new(vec![
            Box::new(Migration1),
            Box::new(Migration2),
            Box::new(Migration3),
        ]);

        // Upgrade to version 2
        let report = migrator.upgrade_to(&mut client, 2).unwrap();
        assert_eq!(report.migrations_run, vec![1, 2]);

        // Verify only migrations 1 and 2 ran
        let version = migrator.get_current_version(&mut client).unwrap();
        assert_eq!(version, 2);

        // Verify users and posts tables exist
        let users_exists: bool = client
            .query_one(
                "SELECT EXISTS (SELECT FROM information_schema.tables WHERE table_schema = 'public' AND table_name = 'users')",
                &[],
            )
            .unwrap()
            .get(0);
        assert!(users_exists);

        let posts_exists: bool = client
            .query_one(
                "SELECT EXISTS (SELECT FROM information_schema.tables WHERE table_schema = 'public' AND table_name = 'posts')",
                &[],
            )
            .unwrap()
            .get(0);
        assert!(posts_exists);

        // Verify comments table does not exist
        let comments_exists: bool = client
            .query_one(
                "SELECT EXISTS (SELECT FROM information_schema.tables WHERE table_schema = 'public' AND table_name = 'comments')",
                &[],
            )
            .unwrap()
            .get(0);
        assert!(!comments_exists);
    }

    #[test]
    fn success_then_failure_from_clean() {
        let mut client = get_test_client();

        struct Migration1;
        impl Migration for Migration1 {
            fn version(&self) -> u32 {
                1
            }
            fn postgres_up(&self, tx: &mut postgres::Transaction) -> Result<(), Error> {
                tx.execute("CREATE TABLE users (id SERIAL PRIMARY KEY)", &[])?;
                tx.execute("INSERT INTO users DEFAULT VALUES", &[])?;
                tx.execute("INSERT INTO users DEFAULT VALUES", &[])?;
                Ok(())
            }
            #[cfg(feature = "sqlite")]
            fn sqlite_up(&self, _tx: &rusqlite::Transaction) -> Result<(), Error> {
                Ok(())
            }
            #[cfg(feature = "mysql")]
            fn mysql_up(&self, _conn: &mut mysql::Conn) -> Result<(), Error> {
                Ok(())
            }
        }

        struct Migration2;
        impl Migration for Migration2 {
            fn version(&self) -> u32 {
                2
            }
            fn postgres_up(&self, tx: &mut postgres::Transaction) -> Result<(), Error> {
                tx.execute("INVALID SQL", &[])?;
                Ok(())
            }
            #[cfg(feature = "sqlite")]
            fn sqlite_up(&self, _tx: &rusqlite::Transaction) -> Result<(), Error> {
                Ok(())
            }
            #[cfg(feature = "mysql")]
            fn mysql_up(&self, _conn: &mut mysql::Conn) -> Result<(), Error> {
                Ok(())
            }
        }

        let migrator = PostgresMigrator::new(vec![Box::new(Migration1), Box::new(Migration2)]);
        let report = migrator.upgrade(&mut client).unwrap();

        assert_eq!(report.migrations_run, vec![1]);
        assert!(report.failing_migration.is_some());

        // Verify users table exists with data (from successful migration 1)
        let count: i64 = client
            .query_one("SELECT COUNT(*) FROM users", &[])
            .unwrap()
            .get(0);
        assert_eq!(count, 2);
    }

    #[test]
    #[should_panic(expected = "Migration version must be greater than 0")]
    fn new_rejects_zero_version() {
        struct Migration0;
        impl Migration for Migration0 {
            fn version(&self) -> u32 {
                0
            }
            fn postgres_up(&self, _tx: &mut postgres::Transaction) -> Result<(), Error> {
                Ok(())
            }
            #[cfg(feature = "sqlite")]
            fn sqlite_up(&self, _tx: &rusqlite::Transaction) -> Result<(), Error> {
                Ok(())
            }
            #[cfg(feature = "mysql")]
            fn mysql_up(&self, _conn: &mut mysql::Conn) -> Result<(), Error> {
                Ok(())
            }
        }
        PostgresMigrator::new(vec![Box::new(Migration0)]);
    }

    #[test]
    #[should_panic(expected = "Duplicate migration version found: 2")]
    fn new_rejects_duplicate_versions() {
        struct Migration1;
        impl Migration for Migration1 {
            fn version(&self) -> u32 {
                1
            }
            fn postgres_up(&self, _tx: &mut postgres::Transaction) -> Result<(), Error> {
                Ok(())
            }
            #[cfg(feature = "sqlite")]
            fn sqlite_up(&self, _tx: &rusqlite::Transaction) -> Result<(), Error> {
                Ok(())
            }
            #[cfg(feature = "mysql")]
            fn mysql_up(&self, _conn: &mut mysql::Conn) -> Result<(), Error> {
                Ok(())
            }
        }

        struct Migration2a;
        impl Migration for Migration2a {
            fn version(&self) -> u32 {
                2
            }
            fn postgres_up(&self, _tx: &mut postgres::Transaction) -> Result<(), Error> {
                Ok(())
            }
            #[cfg(feature = "sqlite")]
            fn sqlite_up(&self, _tx: &rusqlite::Transaction) -> Result<(), Error> {
                Ok(())
            }
            #[cfg(feature = "mysql")]
            fn mysql_up(&self, _conn: &mut mysql::Conn) -> Result<(), Error> {
                Ok(())
            }
        }

        struct Migration2b;
        impl Migration for Migration2b {
            fn version(&self) -> u32 {
                2
            }
            fn postgres_up(&self, _tx: &mut postgres::Transaction) -> Result<(), Error> {
                Ok(())
            }
            #[cfg(feature = "sqlite")]
            fn sqlite_up(&self, _tx: &rusqlite::Transaction) -> Result<(), Error> {
                Ok(())
            }
            #[cfg(feature = "mysql")]
            fn mysql_up(&self, _conn: &mut mysql::Conn) -> Result<(), Error> {
                Ok(())
            }
        }

        PostgresMigrator::new(vec![
            Box::new(Migration1),
            Box::new(Migration2a),
            Box::new(Migration2b),
        ]);
    }

    #[test]
    fn downgrade_works() {
        let mut client = get_test_client();

        struct Migration1;
        impl Migration for Migration1 {
            fn version(&self) -> u32 {
                1
            }
            fn postgres_up(&self, tx: &mut postgres::Transaction) -> Result<(), Error> {
                tx.execute("CREATE TABLE users (id SERIAL PRIMARY KEY)", &[])?;
                Ok(())
            }
            fn postgres_down(&self, tx: &mut postgres::Transaction) -> Result<(), Error> {
                tx.execute("DROP TABLE users", &[])?;
                Ok(())
            }
            #[cfg(feature = "sqlite")]
            fn sqlite_up(&self, _tx: &rusqlite::Transaction) -> Result<(), Error> {
                Ok(())
            }
            #[cfg(feature = "mysql")]
            fn mysql_up(&self, _conn: &mut mysql::Conn) -> Result<(), Error> {
                Ok(())
            }
        }

        struct Migration2;
        impl Migration for Migration2 {
            fn version(&self) -> u32 {
                2
            }
            fn postgres_up(&self, tx: &mut postgres::Transaction) -> Result<(), Error> {
                tx.execute("CREATE TABLE posts (id SERIAL PRIMARY KEY)", &[])?;
                Ok(())
            }
            fn postgres_down(&self, tx: &mut postgres::Transaction) -> Result<(), Error> {
                tx.execute("DROP TABLE posts", &[])?;
                Ok(())
            }
            #[cfg(feature = "sqlite")]
            fn sqlite_up(&self, _tx: &rusqlite::Transaction) -> Result<(), Error> {
                Ok(())
            }
            #[cfg(feature = "mysql")]
            fn mysql_up(&self, _conn: &mut mysql::Conn) -> Result<(), Error> {
                Ok(())
            }
        }

        let migrator = PostgresMigrator::new(vec![Box::new(Migration1), Box::new(Migration2)]);

        // Upgrade to version 2
        migrator.upgrade(&mut client).unwrap();
        assert_eq!(migrator.get_current_version(&mut client).unwrap(), 2);

        // Downgrade to version 1
        let report = migrator.downgrade(&mut client, 1).unwrap();
        assert_eq!(report.migrations_run, vec![2]);
        assert_eq!(migrator.get_current_version(&mut client).unwrap(), 1);

        // Verify posts table no longer exists
        let posts_exists: bool = client
            .query_one(
                "SELECT EXISTS (SELECT FROM information_schema.tables WHERE table_schema = 'public' AND table_name = 'posts')",
                &[],
            )
            .unwrap()
            .get(0);
        assert!(!posts_exists);

        // Verify users table still exists
        let users_exists: bool = client
            .query_one(
                "SELECT EXISTS (SELECT FROM information_schema.tables WHERE table_schema = 'public' AND table_name = 'users')",
                &[],
            )
            .unwrap()
            .get(0);
        assert!(users_exists);

        // Downgrade to version 0
        let report = migrator.downgrade(&mut client, 0).unwrap();
        assert_eq!(report.migrations_run, vec![1]);
        assert_eq!(migrator.get_current_version(&mut client).unwrap(), 0);

        // Verify users table no longer exists
        let users_exists: bool = client
            .query_one(
                "SELECT EXISTS (SELECT FROM information_schema.tables WHERE table_schema = 'public' AND table_name = 'users')",
                &[],
            )
            .unwrap()
            .get(0);
        assert!(!users_exists);
    }
}