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
//! Testing utilities for PostgreSQL migration development.
//!
//! This module provides a test harness for PostgreSQL migration testing: [PostgresTestHarness]

use crate::{postgres::PostgresMigrator, Error};
use postgres::types::FromSql;
use postgres::Client;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// A test harness for PostgreSQL migration testing that provides state control and assertion helpers.
///
/// # Example
///
/// ```ignore
/// use migratio::testing::postgres::PostgresTestHarness;
/// use migratio::{Migration, Error};
/// use migratio::postgres::PostgresMigrator;
///
/// 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, name TEXT)", &[])?;
///         Ok(())
///     }
///     fn postgres_down(&self, tx: &mut postgres::Transaction) -> Result<(), Error> {
///         tx.execute("DROP TABLE users", &[])?;
///         Ok(())
///     }
/// }
///
/// #[tokio::test]
/// async fn test() -> Result<(), Error> {
///     let client = get_test_client(); // however you want to connect to a postgres database in your tests
///     let mut harness = PostgresTestHarness::new(client, PostgresMigrator::new(vec![Box::new(Migration1)]));
///
///     // Migrate to version 1
///     harness.migrate_to(1)?;
///
///     // Insert test data
///     harness.execute("INSERT INTO users (name) VALUES ('alice')")?;
///
///     // Assert table exists
///     harness.assert_table_exists("users")?;
///
///     // Query data
///     let name: String = harness.query_one("SELECT name FROM users WHERE id = 1")?;
///     assert_eq!(name, "alice");
///     Ok(())
/// }
/// ```
pub struct PostgresTestHarness {
    client: Client,
    migrator: PostgresMigrator,
}

/// Represents a captured database schema for comparison and snapshotting.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct SchemaSnapshot {
    /// Map of table name to table definitions
    pub tables: HashMap<String, TableSchema>,
}

/// Represents a table's schema.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct TableSchema {
    /// List of columns
    pub columns: Vec<ColumnInfo>,
    /// List of indexes
    pub indexes: Vec<IndexInfo>,
}

/// Information about a column.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ColumnInfo {
    pub name: String,
    pub type_name: String,
    pub not_null: bool,
    pub default_value: Option<String>,
    pub primary_key: bool,
}

/// Information about an index.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct IndexInfo {
    pub name: String,
    pub unique: bool,
    pub columns: Vec<String>,
}

impl PostgresTestHarness {
    /// Create a new test harness with the given PostgreSQL client and migrator.
    ///
    /// Uses the provided PostgreSQL client, which can be obtained any way you like.
    pub fn new(client: Client, migrator: PostgresMigrator) -> Self {
        Self { client, migrator }
    }

    /// Migrate to a specific version.
    ///
    /// Returns an error if the target version does not exist in the migration list,
    /// or if any migration fails during execution.
    pub fn migrate_to(&mut self, target_version: u32) -> Result<(), Error> {
        // Validate target version exists (version 0 is always valid for empty state)
        if target_version > 0 {
            let version_exists = self
                .migrator
                .migrations()
                .iter()
                .any(|m| m.version() == target_version);
            if !version_exists {
                return Err(Error::Generic(format!(
                    "Migration version {} does not exist. Available versions: {}",
                    target_version,
                    self.migrator
                        .migrations()
                        .iter()
                        .map(|m| m.version().to_string())
                        .collect::<Vec<_>>()
                        .join(", ")
                )));
            }
        }

        let current = self.current_version()?;

        if target_version > current {
            // Migrate up to target version
            let report = self.migrator.upgrade_to(&mut self.client, target_version)?;
            if let Some(failure) = report.failing_migration {
                return Err(failure.error);
            }
        } else if target_version < current {
            // Migrate down
            let report = self.migrator.downgrade(&mut self.client, target_version)?;
            if let Some(failure) = report.failing_migration {
                return Err(failure.error);
            }
        }

        Ok(())
    }

    /// Migrate up by exactly one migration.
    ///
    /// If you have multiple pending migrations and only want to run one, use `migrate_to()` instead.
    pub fn migrate_up_one(&mut self) -> Result<(), Error> {
        let current = self.current_version()?;

        // Find the next migration version after current, since versions may not be contiguous
        let next_version = self
            .migrator
            .migrations()
            .iter()
            .map(|m| m.version())
            .filter(|&v| v > current)
            .min();

        match next_version {
            Some(target) => self.migrate_to(target),
            None => Err(Error::Generic("No more migrations to apply".to_string())),
        }
    }

    /// Migrate down by exactly one migration.
    ///
    /// Returns an error if already at version 0, or if the migration fails.
    pub fn migrate_down_one(&mut self) -> Result<(), Error> {
        let current = self.current_version()?;
        if current == 0 {
            return Err(Error::Generic(
                "Already at version 0, cannot migrate down".to_string(),
            ));
        }

        let report = self.migrator.downgrade(&mut self.client, current - 1)?;
        if let Some(failure) = report.failing_migration {
            return Err(failure.error);
        }
        Ok(())
    }

    /// Get the current migration version.
    pub fn current_version(&mut self) -> Result<u32, Error> {
        self.migrator.get_current_version(&mut self.client)
    }

    /// Execute a SQL statement (for setting up test data).
    pub fn execute(&mut self, sql: &str) -> Result<(), Error> {
        self.client.execute(sql, &[])?;
        Ok(())
    }

    /// Query a single value from the database.
    ///
    /// Note: The type `T` must be an owned type (e.g., `String` not `&str`).
    pub fn query_one<T>(&mut self, sql: &str) -> Result<T, Error>
    where
        T: for<'a> FromSql<'a>,
    {
        let row = self.client.query_one(sql, &[])?;
        Ok(row.get(0))
    }

    /// Query all values from a single-column result.
    ///
    /// Note: The type `T` must be an owned type (e.g., `String` not `&str`).
    pub fn query_all<T>(&mut self, sql: &str) -> Result<Vec<T>, Error>
    where
        T: for<'a> FromSql<'a>,
    {
        let rows = self.client.query(sql, &[])?;
        Ok(rows.iter().map(|row| row.get(0)).collect())
    }

    /// Query with a custom row mapper.
    pub fn query_map<T, F>(&mut self, sql: &str, mut f: F) -> Result<Vec<T>, Error>
    where
        F: FnMut(postgres::Row) -> Result<T, Error>,
    {
        let rows = self.client.query(sql, &[])?;
        rows.into_iter().map(|row| f(row)).collect()
    }

    /// Assert that a table exists in the database.
    pub fn assert_table_exists(&mut self, table_name: &str) -> Result<(), Error> {
        let exists: bool = self
            .client
            .query_one(
                "SELECT EXISTS (SELECT FROM information_schema.tables WHERE table_schema = 'public' AND table_name = $1)",
                &[&table_name],
            )?
            .get(0);

        if !exists {
            return Err(Error::Generic(format!(
                "Table '{}' does not exist",
                table_name
            )));
        }

        Ok(())
    }

    /// Assert that a table does not exist in the database.
    pub fn assert_table_not_exists(&mut self, table_name: &str) -> Result<(), Error> {
        let exists: bool = self
            .client
            .query_one(
                "SELECT EXISTS (SELECT FROM information_schema.tables WHERE table_schema = 'public' AND table_name = $1)",
                &[&table_name],
            )?
            .get(0);

        if exists {
            return Err(Error::Generic(format!(
                "Table '{}' exists but should not",
                table_name
            )));
        }

        Ok(())
    }

    /// Assert that a column exists in a table.
    pub fn assert_column_exists(
        &mut self,
        table_name: &str,
        column_name: &str,
    ) -> Result<(), Error> {
        let columns = self.get_columns(table_name)?;

        if !columns.iter().any(|c| c.name == column_name) {
            return Err(Error::Generic(format!(
                "Column '{}' does not exist in table '{}'",
                column_name, table_name
            )));
        }

        Ok(())
    }

    /// Assert that an index exists.
    pub fn assert_index_exists(&mut self, index_name: &str) -> Result<(), Error> {
        let exists: bool = self
            .client
            .query_one(
                "SELECT EXISTS (SELECT FROM pg_indexes WHERE schemaname = 'public' AND indexname = $1)",
                &[&index_name],
            )?
            .get(0);

        if !exists {
            return Err(Error::Generic(format!(
                "Index '{}' does not exist",
                index_name
            )));
        }

        Ok(())
    }

    /// Capture the current PostgreSQL database schema as a snapshot.
    pub fn capture_schema(&mut self) -> Result<SchemaSnapshot, Error> {
        let mut tables = HashMap::new();

        // Get all user tables (exclude system tables and migration table)
        let table_rows = self.client.query(
            "SELECT table_name FROM information_schema.tables
             WHERE table_schema = 'public'
             AND table_name != '_migratio_version_'
             ORDER BY table_name",
            &[],
        )?;

        for row in table_rows {
            let table_name: String = row.get(0);
            let columns = self.get_columns(&table_name)?;
            let indexes = self.get_indexes(&table_name)?;

            tables.insert(table_name, TableSchema { columns, indexes });
        }

        Ok(SchemaSnapshot { tables })
    }

    /// Assert that the current schema matches a previously captured snapshot.
    pub fn assert_schema_matches(&mut self, expected: &SchemaSnapshot) -> Result<(), Error> {
        let actual = self.capture_schema()?;

        if actual != *expected {
            let mut differences = Vec::new();

            // Sort table names for deterministic ordering
            let mut expected_table_names: Vec<_> = expected.tables.keys().collect();
            expected_table_names.sort();
            let mut actual_table_names: Vec<_> = actual.tables.keys().collect();
            actual_table_names.sort();

            // Check for tables in expected but not in actual
            for table_name in &expected_table_names {
                if !actual.tables.contains_key(*table_name) {
                    differences.push(format!("  - Table '{}' is missing", table_name));
                }
            }

            // Check for tables in actual but not in expected
            for table_name in &actual_table_names {
                if !expected.tables.contains_key(*table_name) {
                    differences.push(format!("  - Unexpected table '{}' found", table_name));
                }
            }

            // Check for differences in common tables (sorted order)
            for table_name in &expected_table_names {
                let expected_table = &expected.tables[*table_name];
                if let Some(actual_table) = actual.tables.get(*table_name) {
                    // Check column differences
                    if expected_table.columns != actual_table.columns {
                        let expected_cols: Vec<_> =
                            expected_table.columns.iter().map(|c| &c.name).collect();
                        let actual_cols: Vec<_> =
                            actual_table.columns.iter().map(|c| &c.name).collect();

                        if expected_cols != actual_cols {
                            differences.push(format!(
                                "  - Table '{}' column mismatch:\n    Expected columns: {:?}\n    Actual columns:   {:?}",
                                table_name, expected_cols, actual_cols
                            ));
                        } else {
                            // Same column names but different properties
                            for (expected_col, actual_col) in
                                expected_table.columns.iter().zip(&actual_table.columns)
                            {
                                if expected_col != actual_col {
                                    differences.push(format!(
                                        "  - Table '{}' column '{}' properties differ:\n    Expected: {:?}\n    Actual:   {:?}",
                                        table_name, expected_col.name, expected_col, actual_col
                                    ));
                                }
                            }
                        }
                    }

                    // Check index differences
                    if expected_table.indexes != actual_table.indexes {
                        let expected_idxs: Vec<_> =
                            expected_table.indexes.iter().map(|i| &i.name).collect();
                        let actual_idxs: Vec<_> =
                            actual_table.indexes.iter().map(|i| &i.name).collect();
                        differences.push(format!(
                            "  - Table '{}' index mismatch:\n    Expected indexes: {:?}\n    Actual indexes:   {:?}",
                            table_name, expected_idxs, actual_idxs
                        ));
                    }
                }
            }

            return Err(Error::Generic(format!(
                "Schema mismatch detected:\n{}",
                differences.join("\n")
            )));
        }

        Ok(())
    }

    /// Get column information for a table.
    fn get_columns(&mut self, table_name: &str) -> Result<Vec<ColumnInfo>, Error> {
        // Get primary key columns using a subquery to convert table name to oid
        let pk_rows = self.client.query(
            "SELECT a.attname
             FROM pg_index i
             JOIN pg_attribute a ON a.attrelid = i.indrelid AND a.attnum = ANY(i.indkey)
             JOIN pg_class c ON c.oid = i.indrelid
             WHERE c.relname = $1 AND c.relnamespace = 'public'::regnamespace AND i.indisprimary",
            &[&table_name],
        )?;
        let pk_columns: Vec<String> = pk_rows.iter().map(|row| row.get(0)).collect();

        let rows = self.client.query(
            "SELECT
                column_name,
                data_type,
                is_nullable,
                column_default
             FROM information_schema.columns
             WHERE table_schema = 'public' AND table_name = $1
             ORDER BY ordinal_position",
            &[&table_name],
        )?;

        let columns = rows
            .into_iter()
            .map(|row| {
                let name: String = row.get(0);
                let type_name: String = row.get(1);
                let is_nullable: String = row.get(2);
                let default_value: Option<String> = row.get(3);
                let primary_key = pk_columns.contains(&name);

                ColumnInfo {
                    name,
                    type_name,
                    not_null: is_nullable == "NO",
                    default_value,
                    primary_key,
                }
            })
            .collect();

        Ok(columns)
    }

    /// Get index information for a table.
    fn get_indexes(&mut self, table_name: &str) -> Result<Vec<IndexInfo>, Error> {
        // Get all indexes for this table, excluding primary key indexes
        let rows = self.client.query(
            "SELECT
                i.relname AS index_name,
                ix.indisunique AS is_unique,
                array_agg(a.attname ORDER BY array_position(ix.indkey, a.attnum)) AS columns
             FROM pg_class t
             JOIN pg_index ix ON t.oid = ix.indrelid
             JOIN pg_class i ON i.oid = ix.indexrelid
             JOIN pg_attribute a ON a.attrelid = t.oid AND a.attnum = ANY(ix.indkey)
             WHERE t.relkind = 'r'
               AND t.relname = $1
               AND NOT ix.indisprimary
             GROUP BY i.relname, ix.indisunique
             ORDER BY i.relname",
            &[&table_name],
        )?;

        let indexes = rows
            .into_iter()
            .map(|row| {
                let name: String = row.get(0);
                let unique: bool = row.get(1);
                let columns: Vec<String> = row.get(2);

                IndexInfo {
                    name,
                    unique,
                    columns,
                }
            })
            .collect();

        Ok(indexes)
    }

    /// Get a mutable reference to the underlying client for advanced usage.
    pub fn client(&mut self) -> &mut Client {
        &mut self.client
    }
}

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

    use super::*;

    struct TestMigration1;
    impl Migration for TestMigration1 {
        fn version(&self) -> u32 {
            1
        }
        fn postgres_up(&self, tx: &mut postgres::Transaction) -> Result<(), Error> {
            tx.execute(
                "CREATE TABLE users (id SERIAL PRIMARY KEY, name VARCHAR(255))",
                &[],
            )?;
            Ok(())
        }
        fn postgres_down(&self, tx: &mut postgres::Transaction) -> Result<(), Error> {
            tx.execute("DROP TABLE users", &[])?;
            Ok(())
        }
        fn name(&self) -> String {
            "create_users_table".to_string()
        }
        #[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 TestMigration2;
    impl Migration for TestMigration2 {
        fn version(&self) -> u32 {
            2
        }
        fn postgres_up(&self, tx: &mut postgres::Transaction) -> Result<(), Error> {
            tx.execute("ALTER TABLE users ADD COLUMN email VARCHAR(255)", &[])?;
            Ok(())
        }
        fn postgres_down(&self, tx: &mut postgres::Transaction) -> Result<(), Error> {
            tx.execute("ALTER TABLE users DROP COLUMN email", &[])?;
            Ok(())
        }
        fn name(&self) -> String {
            "add_email_column".to_string()
        }
        #[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 TestMigration3;
    impl Migration for TestMigration3 {
        fn version(&self) -> u32 {
            3
        }
        fn postgres_up(&self, tx: &mut postgres::Transaction) -> Result<(), Error> {
            tx.execute("CREATE INDEX idx_users_email ON users(email)", &[])?;
            Ok(())
        }
        fn postgres_down(&self, tx: &mut postgres::Transaction) -> Result<(), Error> {
            tx.execute("DROP INDEX idx_users_email", &[])?;
            Ok(())
        }
        fn name(&self) -> String {
            "add_email_index".to_string()
        }
        #[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(())
        }
    }

    #[test]
    fn test_migrate_to() {
        let client = get_test_client();
        let mut harness = PostgresTestHarness::new(
            client,
            PostgresMigrator::new(vec![
                Box::new(TestMigration1),
                Box::new(TestMigration2),
                Box::new(TestMigration3),
            ]),
        );

        assert_eq!(harness.current_version().unwrap(), 0);

        harness.migrate_to(2).unwrap();
        assert_eq!(harness.current_version().unwrap(), 2);

        harness.migrate_to(1).unwrap();
        assert_eq!(harness.current_version().unwrap(), 1);

        harness.migrate_to(3).unwrap();
        assert_eq!(harness.current_version().unwrap(), 3);
    }

    #[test]
    fn test_migrate_to_nonexistent_version() {
        let client = get_test_client();
        let mut harness = PostgresTestHarness::new(
            client,
            PostgresMigrator::new(vec![Box::new(TestMigration1), Box::new(TestMigration2)]),
        );

        let result = harness.migrate_to(5);
        assert!(result.is_err());

        let err_msg = format!("{:?}", result.unwrap_err());
        assert!(err_msg.contains("Migration version 5 does not exist"));
        assert!(err_msg.contains("Available versions: 1, 2"));
    }

    #[test]
    fn test_migrate_up_one() {
        let client = get_test_client();
        let mut harness = PostgresTestHarness::new(
            client,
            PostgresMigrator::new(vec![Box::new(TestMigration1), Box::new(TestMigration2)]),
        );

        assert_eq!(harness.current_version().unwrap(), 0);

        harness.migrate_up_one().unwrap();
        assert_eq!(harness.current_version().unwrap(), 1);

        harness.migrate_up_one().unwrap();
        assert_eq!(harness.current_version().unwrap(), 2);
    }

    #[test]
    fn test_migrate_down_one() {
        let client = get_test_client();
        let mut harness = PostgresTestHarness::new(
            client,
            PostgresMigrator::new(vec![Box::new(TestMigration1), Box::new(TestMigration2)]),
        );

        harness.migrate_to(2).unwrap();
        assert_eq!(harness.current_version().unwrap(), 2);

        harness.migrate_down_one().unwrap();
        assert_eq!(harness.current_version().unwrap(), 1);

        harness.migrate_down_one().unwrap();
        assert_eq!(harness.current_version().unwrap(), 0);
    }

    #[test]
    fn test_execute_and_query() {
        let client = get_test_client();
        let mut harness = PostgresTestHarness::new(
            client,
            PostgresMigrator::new(vec![Box::new(TestMigration1)]),
        );

        harness.migrate_to(1).unwrap();
        harness
            .execute("INSERT INTO users (name) VALUES ('alice')")
            .unwrap();

        let name: String = harness
            .query_one("SELECT name FROM users WHERE id = 1")
            .unwrap();
        assert_eq!(name, "alice");
    }

    #[test]
    fn test_query_all() {
        let client = get_test_client();
        let mut harness = PostgresTestHarness::new(
            client,
            PostgresMigrator::new(vec![Box::new(TestMigration1)]),
        );

        harness.migrate_to(1).unwrap();
        harness
            .execute("INSERT INTO users (name) VALUES ('alice')")
            .unwrap();
        harness
            .execute("INSERT INTO users (name) VALUES ('bob')")
            .unwrap();

        let names: Vec<String> = harness
            .query_all("SELECT name FROM users ORDER BY id")
            .unwrap();
        assert_eq!(names, vec!["alice", "bob"]);
    }

    #[test]
    fn test_assert_table_exists() {
        let client = get_test_client();
        let mut harness = PostgresTestHarness::new(
            client,
            PostgresMigrator::new(vec![Box::new(TestMigration1)]),
        );

        harness.migrate_to(1).unwrap();
        harness.assert_table_exists("users").unwrap();

        let result = harness.assert_table_exists("nonexistent");
        assert!(result.is_err());
    }

    #[test]
    fn test_assert_table_not_exists() {
        let client = get_test_client();
        let mut harness = PostgresTestHarness::new(
            client,
            PostgresMigrator::new(vec![Box::new(TestMigration1)]),
        );

        harness.assert_table_not_exists("users").unwrap();

        harness.migrate_to(1).unwrap();
        let result = harness.assert_table_not_exists("users");
        assert!(result.is_err());
    }

    #[test]
    fn test_assert_column_exists() {
        let client = get_test_client();
        let mut harness = PostgresTestHarness::new(
            client,
            PostgresMigrator::new(vec![Box::new(TestMigration1), Box::new(TestMigration2)]),
        );

        harness.migrate_to(1).unwrap();
        harness.assert_column_exists("users", "name").unwrap();

        let result = harness.assert_column_exists("users", "email");
        assert!(result.is_err());

        harness.migrate_to(2).unwrap();
        harness.assert_column_exists("users", "email").unwrap();
    }

    #[test]
    fn test_assert_index_exists() {
        let client = get_test_client();
        let mut harness = PostgresTestHarness::new(
            client,
            PostgresMigrator::new(vec![
                Box::new(TestMigration1),
                Box::new(TestMigration2),
                Box::new(TestMigration3),
            ]),
        );

        harness.migrate_to(2).unwrap();
        let result = harness.assert_index_exists("idx_users_email");
        assert!(result.is_err());

        harness.migrate_to(3).unwrap();
        harness.assert_index_exists("idx_users_email").unwrap();
    }

    #[test]
    fn test_capture_schema() {
        let client = get_test_client();
        let mut harness = PostgresTestHarness::new(
            client,
            PostgresMigrator::new(vec![Box::new(TestMigration1), Box::new(TestMigration2)]),
        );

        harness.migrate_to(2).unwrap();
        let snapshot = harness.capture_schema().unwrap();

        assert!(snapshot.tables.contains_key("users"));
        let users_table = &snapshot.tables["users"];
        assert_eq!(users_table.columns.len(), 3); // id, name, email
        assert!(users_table.columns.iter().any(|c| c.name == "id"));
        assert!(users_table.columns.iter().any(|c| c.name == "name"));
        assert!(users_table.columns.iter().any(|c| c.name == "email"));
    }

    #[test]
    fn test_schema_reversibility() {
        let client = get_test_client();
        let mut harness = PostgresTestHarness::new(
            client,
            PostgresMigrator::new(vec![Box::new(TestMigration1), Box::new(TestMigration2)]),
        );

        // Capture schema at version 2
        harness.migrate_to(2).unwrap();
        let schema_at_2 = harness.capture_schema().unwrap();

        // Go back to version 1
        harness.migrate_to(1).unwrap();

        // Go back up to version 2
        harness.migrate_to(2).unwrap();
        let schema_at_2_again = harness.capture_schema().unwrap();

        // Should be identical
        assert_eq!(schema_at_2, schema_at_2_again);
    }

    #[test]
    fn test_assert_schema_matches() {
        let client = get_test_client();
        let mut harness = PostgresTestHarness::new(
            client,
            PostgresMigrator::new(vec![Box::new(TestMigration1)]),
        );

        harness.migrate_to(1).unwrap();
        let snapshot = harness.capture_schema().unwrap();

        // Should match itself
        harness.assert_schema_matches(&snapshot).unwrap();

        // Add a column - should no longer match
        harness
            .execute("ALTER TABLE users ADD COLUMN age INT")
            .unwrap();
        let result = harness.assert_schema_matches(&snapshot);
        assert!(result.is_err());
    }
}