switchy_schema_test_utils 0.3.0

Switchy Schema Test Utils package
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
#![cfg_attr(feature = "fail-on-warnings", deny(warnings))]
#![warn(clippy::all, clippy::pedantic, clippy::nursery, clippy::cargo)]
#![allow(clippy::multiple_crate_versions)]

//! Migration test builder for complex migration testing scenarios
//!
//! This module provides the `MigrationTestBuilder` for testing migrations with
//! data insertion at specific points in the migration sequence. This is particularly
//! useful for testing data migration scenarios where existing data needs to be
//! transformed by subsequent migrations.
//!
//! ## Default Behavior
//!
//! Migrations persist after execution (no rollback) to allow tests to work with
//! the migrated schema. Use `.with_rollback()` to explicitly enable rollback
//! behavior for tests that need to verify migration reversibility.
//!
//! ## Common Usage Patterns
//!
//! ### Integration Testing (Default)
//! ```rust,no_run
//! use switchy_schema_test_utils::MigrationTestBuilder;
//! use std::sync::Arc;
//!
//! # async fn example(migrations: Vec<Arc<dyn switchy_schema::migration::Migration<'static> + 'static>>, db: &dyn switchy_database::Database) -> Result<(), Box<dyn std::error::Error>> {
//! MigrationTestBuilder::new(migrations)
//!     .with_table_name("__test_migrations")
//!     .run(db)
//!     .await?;
//! // Schema persists for testing
//! # Ok(())
//! # }
//! ```
//!
//! ### Migration Reversibility Testing
//! ```rust,no_run
//! use switchy_schema_test_utils::MigrationTestBuilder;
//! use std::sync::Arc;
//!
//! # async fn example(migrations: Vec<Arc<dyn switchy_schema::migration::Migration<'static> + 'static>>, db: &dyn switchy_database::Database) -> Result<(), Box<dyn std::error::Error>> {
//! MigrationTestBuilder::new(migrations)
//!     .with_rollback()  // Explicitly enable rollback
//!     .run(db)
//!     .await?;
//! // Schema is rolled back after execution
//! # Ok(())
//! # }
//! ```
//!
//! ### Data Migration Testing
//! ```rust,no_run
//! use switchy_schema_test_utils::MigrationTestBuilder;
//! use std::sync::Arc;
//!
//! # async fn example(migrations: Vec<Arc<dyn switchy_schema::migration::Migration<'static> + 'static>>, db: &dyn switchy_database::Database) -> Result<(), Box<dyn std::error::Error>> {
//! MigrationTestBuilder::new(migrations)
//!     .with_data_before("migration_id", |db| {
//!         Box::pin(async move {
//!             // Insert test data before migration
//!             Ok(())
//!         })
//!     })
//!     .with_data_after("migration_id", |db| {
//!         Box::pin(async move {
//!             // Verify data after migration
//!             Ok(())
//!         })
//!     })
//!     .run(db)
//!     .await?;
//! # Ok(())
//! # }
//! ```

use std::{future::Future, pin::Pin, sync::Arc};

use async_trait::async_trait;
use switchy_database::{Database, DatabaseError};
use switchy_schema::{
    migration::{Migration, MigrationSource},
    runner::MigrationRunner,
    version::{DEFAULT_MIGRATIONS_TABLE, VersionTracker},
};

use crate::TestError;

/// Builder for complex migration test scenarios with breakpoints
///
/// Allows running migrations with data insertion at specific points,
/// useful for testing data transformations during migrations.
///
/// By default, migrations persist after execution. Use `.with_rollback()`
/// to enable cleanup after test completion.
pub struct MigrationTestBuilder<'a> {
    migrations: Vec<Arc<dyn Migration<'a> + 'a>>,
    breakpoints: Vec<Breakpoint<'a>>,
    initial_setup: Option<SetupFn<'a>>,
    with_rollback: bool,
    table_name: Option<String>,
}

/// A breakpoint in the migration sequence where custom actions can be performed
struct Breakpoint<'a> {
    /// The migration ID to target
    migration_id: String,
    /// When to execute relative to the migration
    timing: BreakpointTiming,
    /// The action to perform at this breakpoint
    action: SetupFn<'a>,
}

/// When to execute a breakpoint action relative to a migration
#[derive(Debug, Clone, PartialEq, Eq)]
enum BreakpointTiming {
    /// Execute before the specified migration runs
    Before,
    /// Execute after the specified migration runs
    After,
}

type SetupFn<'a> = Box<
    dyn for<'db> FnOnce(
            &'db dyn Database,
        )
            -> Pin<Box<dyn Future<Output = Result<(), DatabaseError>> + Send + 'db>>
        + Send
        + 'a,
>;

/// Internal helper struct that wraps a Vec of migrations into a `MigrationSource`
struct VecMigrationSource<'a> {
    migrations: Vec<Arc<dyn Migration<'a> + 'a>>,
}

impl<'a> VecMigrationSource<'a> {
    #[must_use]
    fn new(migrations: Vec<Arc<dyn Migration<'a> + 'a>>) -> Self {
        Self { migrations }
    }
}

#[async_trait]
impl<'a> MigrationSource<'a> for VecMigrationSource<'a> {
    async fn migrations(&self) -> switchy_schema::Result<Vec<Arc<dyn Migration<'a> + 'a>>> {
        Ok(self.migrations.clone()) // Cheap Arc cloning!
    }
}

impl<'a> MigrationTestBuilder<'a> {
    /// Create a new test builder with the given migrations
    ///
    /// Migrations will persist by default (no rollback).
    #[must_use]
    pub fn new(migrations: Vec<Arc<dyn Migration<'a> + 'a>>) -> Self {
        Self {
            migrations,
            breakpoints: Vec::new(),
            initial_setup: None,
            with_rollback: false,
            table_name: None,
        }
    }

    /// Set up initial database state before any migrations run
    #[must_use]
    pub fn with_initial_setup<F>(mut self, setup: F) -> Self
    where
        F: for<'db> FnOnce(
                &'db dyn Database,
            )
                -> Pin<Box<dyn Future<Output = Result<(), DatabaseError>> + Send + 'db>>
            + Send
            + 'a,
    {
        self.initial_setup = Some(Box::new(setup));
        self
    }

    /// Insert data BEFORE the specified migration runs
    #[must_use]
    pub fn with_data_before<F>(mut self, migration_id: &str, setup: F) -> Self
    where
        F: for<'db> FnOnce(
                &'db dyn Database,
            )
                -> Pin<Box<dyn Future<Output = Result<(), DatabaseError>> + Send + 'db>>
            + Send
            + 'a,
    {
        self.breakpoints.push(Breakpoint {
            migration_id: migration_id.to_string(),
            timing: BreakpointTiming::Before,
            action: Box::new(setup),
        });
        self
    }

    /// Insert data AFTER the specified migration runs
    #[must_use]
    pub fn with_data_after<F>(mut self, migration_id: &str, setup: F) -> Self
    where
        F: for<'db> FnOnce(
                &'db dyn Database,
            )
                -> Pin<Box<dyn Future<Output = Result<(), DatabaseError>> + Send + 'db>>
            + Send
            + 'a,
    {
        self.breakpoints.push(Breakpoint {
            migration_id: migration_id.to_string(),
            timing: BreakpointTiming::After,
            action: Box::new(setup),
        });
        self
    }

    /// Enable rollback after migrations complete
    ///
    /// By default, migrations persist to allow testing with the migrated schema.
    /// Use this method to enable rollback for testing migration reversibility.
    #[must_use]
    pub const fn with_rollback(mut self) -> Self {
        self.with_rollback = true;
        self
    }

    /// Use a custom migration table name
    #[must_use]
    pub fn with_table_name(mut self, table_name: impl Into<String>) -> Self {
        self.table_name = Some(table_name.into());
        self
    }

    /// Execute the test scenario
    ///
    /// Runs migrations with any configured breakpoints and data insertions.
    /// By default, the migrated schema persists. Use `.with_rollback()` to
    /// enable cleanup after execution.
    ///
    /// # Errors
    ///
    /// * If initial setup fails
    /// * If a migration ID in breakpoints is not found in the migration list
    /// * If any migration execution fails
    /// * If any breakpoint action fails
    /// * If rollback fails (when explicitly enabled)
    pub async fn run(self, db: &dyn Database) -> Result<(), TestError> {
        use std::collections::BTreeMap;

        // Extract fields to avoid borrow checker issues
        let migrations = self.migrations;
        let breakpoints = self.breakpoints;
        let initial_setup = self.initial_setup;
        let with_rollback = self.with_rollback;
        let table_name = self.table_name;

        // Step 1: Run initial setup if provided
        if let Some(setup) = initial_setup {
            setup(db).await?;
        }

        // Step 2: Group breakpoints by migration and sort by migration order
        let mut breakpoints_by_migration: BTreeMap<
            usize,
            (Vec<Breakpoint<'_>>, Vec<Breakpoint<'_>>),
        > = BTreeMap::new();

        for breakpoint in breakpoints {
            // Find the index of this migration in our migration list
            let migration_index = migrations
                .iter()
                .position(|m| m.id() == breakpoint.migration_id)
                .ok_or_else(|| {
                    TestError::Migration(switchy_schema::MigrationError::Validation(format!(
                        "Migration '{}' not found in migration list",
                        breakpoint.migration_id
                    )))
                })?;

            let entry = breakpoints_by_migration
                .entry(migration_index)
                .or_insert((Vec::new(), Vec::new()));
            match breakpoint.timing {
                BreakpointTiming::Before => entry.0.push(breakpoint),
                BreakpointTiming::After => entry.1.push(breakpoint),
            }
        }

        // Step 3: Execute migrations with breakpoints
        let mut current_migration_index = 0;

        for (breakpoint_migration_index, (before_breakpoints, after_breakpoints)) in
            breakpoints_by_migration
        {
            // Run migrations up to (but not including) the breakpoint migration
            if current_migration_index < breakpoint_migration_index {
                let migrations_to_run =
                    migrations[current_migration_index..breakpoint_migration_index].to_vec();
                if !migrations_to_run.is_empty() {
                    let source = VecMigrationSource::new(migrations_to_run);
                    let mut runner = MigrationRunner::new(Box::new(source));

                    if let Some(ref table_name) = table_name {
                        runner = runner.with_table_name(table_name.clone());
                    }

                    runner.run(db).await?;
                }
                current_migration_index = breakpoint_migration_index;
            }

            // Handle the breakpoint migration with before/after actions
            let target_migration = &migrations[breakpoint_migration_index];

            // Execute all "before" actions
            for breakpoint in before_breakpoints {
                (breakpoint.action)(db).await?;
            }

            // Run the migration
            target_migration
                .up(db)
                .await
                .map_err(TestError::Migration)?;

            // Execute all "after" actions
            for breakpoint in after_breakpoints {
                (breakpoint.action)(db).await?;
            }

            // Update migration tracking table manually since we ran the migration directly
            if let Some(ref table_name) = table_name {
                Self::record_migration(db, table_name, target_migration.id()).await?;
            } else {
                Self::record_migration(db, DEFAULT_MIGRATIONS_TABLE, target_migration.id()).await?;
            }

            current_migration_index += 1;
        }

        // Step 4: Run any remaining migrations after the last breakpoint
        if current_migration_index < migrations.len() {
            let remaining_migrations = migrations[current_migration_index..].to_vec();
            let source = VecMigrationSource::new(remaining_migrations);
            let mut runner = MigrationRunner::new(Box::new(source));

            if let Some(ref table_name) = table_name {
                runner = runner.with_table_name(table_name.clone());
            }

            runner.run(db).await?;
        }

        // Step 5: Rollback all migrations unless skipped
        if with_rollback {
            let source = VecMigrationSource::new(migrations);
            let mut runner = MigrationRunner::new(Box::new(source));

            if let Some(ref table_name) = table_name {
                runner = runner.with_table_name(table_name.clone());
            }

            runner
                .rollback(db, switchy_schema::runner::RollbackStrategy::All)
                .await?;
        }

        Ok(())
    }

    /// Record a migration as completed in the migration tracking table
    async fn record_migration(
        db: &dyn Database,
        table_name: &str,
        migration_id: &str,
    ) -> Result<(), TestError> {
        // Create the migration table if it doesn't exist
        let version_tracker = VersionTracker::with_table_name(table_name);
        version_tracker.ensure_table_exists(db).await?;
        version_tracker.record_migration(db, migration_id).await?;

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::Arc;
    use switchy_database::{query, query::FilterableQuery};
    use switchy_schema::migration::Migration;

    // Mock migration for testing
    struct TestMigration {
        id: String,
        up_sql: String,
        down_sql: Option<String>,
    }

    impl TestMigration {
        fn new(id: &str, up_sql: &str, down_sql: Option<&str>) -> Self {
            Self {
                id: id.to_string(),
                up_sql: up_sql.to_string(),
                down_sql: down_sql.map(String::from),
            }
        }
    }

    #[async_trait]
    impl Migration<'static> for TestMigration {
        fn id(&self) -> &str {
            &self.id
        }

        async fn up(&self, db: &dyn Database) -> switchy_schema::Result<()> {
            db.exec_raw(&self.up_sql).await?;
            Ok(())
        }

        async fn down(&self, db: &dyn Database) -> switchy_schema::Result<()> {
            if let Some(ref down_sql) = self.down_sql {
                db.exec_raw(down_sql).await?;
            }
            Ok(())
        }

        fn description(&self) -> Option<&str> {
            None
        }
    }

    #[cfg(feature = "sqlite")]
    #[test_log::test(switchy_async::test)]
    async fn test_migration_test_builder_basic() {
        let db = crate::create_empty_in_memory().await.unwrap();

        let migrations = vec![Arc::new(TestMigration::new(
            "001_create_users",
            "CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)",
            Some("DROP TABLE users"),
        )) as Arc<dyn Migration<'static> + 'static>];

        MigrationTestBuilder::new(migrations)
            .run(&*db)
            .await
            .unwrap();

        // With default behavior, tables should persist
        let result = query::select("sqlite_master")
            .columns(&["name"])
            .where_eq("type", "table")
            .where_eq("name", "users")
            .execute(db.as_ref())
            .await
            .unwrap();
        assert_eq!(result.len(), 1);
    }

    #[cfg(feature = "sqlite")]
    #[test_log::test(switchy_async::test)]
    async fn test_migration_test_builder_default_persistence() {
        let db = crate::create_empty_in_memory().await.unwrap();

        let migrations = vec![Arc::new(TestMigration::new(
            "001_create_test",
            "CREATE TABLE test_table (id INTEGER)",
            Some("DROP TABLE test_table"),
        )) as Arc<dyn Migration<'static> + 'static>];

        MigrationTestBuilder::new(migrations)
            .run(&*db)
            .await
            .unwrap();

        // Table should still exist since migrations persist by default
        let result = query::select("sqlite_master")
            .columns(&["name"])
            .where_eq("type", "table")
            .where_eq("name", "test_table")
            .execute(db.as_ref())
            .await
            .unwrap();
        assert_eq!(result.len(), 1);
    }

    #[cfg(feature = "sqlite")]
    #[test_log::test(switchy_async::test)]
    async fn test_migration_test_builder_custom_table_name() {
        let db = crate::create_empty_in_memory().await.unwrap();

        let migrations = vec![Arc::new(TestMigration::new(
            "001_create_test",
            "CREATE TABLE test_table (id INTEGER)",
            Some("DROP TABLE test_table"),
        )) as Arc<dyn Migration<'static> + 'static>];

        MigrationTestBuilder::new(migrations)
            .with_table_name("__custom_migrations")
            .run(&*db)
            .await
            .unwrap();

        // Verify custom migration table was created
        let result = query::select("sqlite_master")
            .columns(&["name"])
            .where_eq("type", "table")
            .where_eq("name", "__custom_migrations")
            .execute(db.as_ref())
            .await
            .unwrap();
        assert_eq!(result.len(), 1);
    }

    #[cfg(feature = "sqlite")]
    #[test_log::test(switchy_async::test)]
    async fn test_with_data_before_breakpoint() {
        let db = crate::create_empty_in_memory().await.unwrap();

        let migrations = vec![
            Arc::new(TestMigration::new(
                "001_create_users",
                "CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)",
                Some("DROP TABLE users"),
            )) as Arc<dyn Migration<'static> + 'static>,
            Arc::new(TestMigration::new(
                "002_add_email_column",
                "ALTER TABLE users ADD COLUMN email TEXT",
                Some("ALTER TABLE users DROP COLUMN email"),
            )) as Arc<dyn Migration<'static> + 'static>,
        ];

        MigrationTestBuilder::new(migrations)
            .with_data_before("002_add_email_column", |db| {
                Box::pin(async move {
                    // Insert data before the email column is added
                    db.exec_raw("INSERT INTO users (name) VALUES ('Alice')")
                        .await?;
                    Ok(())
                })
            })
            .run(&*db)
            .await
            .unwrap();

        // Verify the user was inserted and the email column was added
        let result = query::select("users")
            .columns(&["name", "email"])
            .where_eq("name", "Alice")
            .execute(db.as_ref())
            .await
            .unwrap();

        assert_eq!(result.len(), 1);
        let row = &result[0];
        assert_eq!(row.get("name").unwrap().as_str().unwrap(), "Alice");
        // email column should exist but be NULL since it was added after the row was inserted
        assert_eq!(
            row.get("email").unwrap(),
            switchy_database::DatabaseValue::Null
        );
    }

    #[cfg(feature = "sqlite")]
    #[test_log::test(switchy_async::test)]
    async fn test_with_data_after_breakpoint() {
        let db = crate::create_empty_in_memory().await.unwrap();

        let migrations = vec![
            Arc::new(TestMigration::new(
                "001_create_users",
                "CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)",
                Some("DROP TABLE users"),
            )) as Arc<dyn Migration<'static> + 'static>,
            Arc::new(TestMigration::new(
                "002_add_email_column",
                "ALTER TABLE users ADD COLUMN email TEXT",
                Some("ALTER TABLE users DROP COLUMN email"),
            )) as Arc<dyn Migration<'static> + 'static>,
        ];

        MigrationTestBuilder::new(migrations)
            .with_data_after("002_add_email_column", |db| {
                Box::pin(async move {
                    // Insert data after the email column is added
                    db.exec_raw(
                        "INSERT INTO users (name, email) VALUES ('Bob', 'bob@example.com')",
                    )
                    .await?;
                    Ok(())
                })
            })
            .run(&*db)
            .await
            .unwrap();

        // Verify the user was inserted with email data
        let result = query::select("users")
            .columns(&["name", "email"])
            .where_eq("name", "Bob")
            .execute(db.as_ref())
            .await
            .unwrap();

        assert_eq!(result.len(), 1);
        let row = &result[0];
        assert_eq!(row.get("name").unwrap().as_str().unwrap(), "Bob");
        assert_eq!(
            row.get("email").unwrap().as_str().unwrap(),
            "bob@example.com"
        );
    }

    #[cfg(feature = "sqlite")]
    #[test_log::test(switchy_async::test)]
    async fn test_multiple_breakpoints_in_sequence() {
        let db = crate::create_empty_in_memory().await.unwrap();

        let migrations = vec![
            Arc::new(TestMigration::new(
                "001_create_users",
                "CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)",
                Some("DROP TABLE users"),
            )) as Arc<dyn Migration<'static> + 'static>,
            Arc::new(TestMigration::new(
                "002_add_email_column",
                "ALTER TABLE users ADD COLUMN email TEXT",
                Some("ALTER TABLE users DROP COLUMN email"),
            )) as Arc<dyn Migration<'static> + 'static>,
            Arc::new(TestMigration::new(
                "003_add_age_column",
                "ALTER TABLE users ADD COLUMN age INTEGER",
                Some("ALTER TABLE users DROP COLUMN age"),
            )) as Arc<dyn Migration<'static> + 'static>,
        ];

        MigrationTestBuilder::new(migrations)
            .with_data_before("002_add_email_column", |db| {
                Box::pin(async move {
                    db.exec_raw("INSERT INTO users (name) VALUES ('Alice')")
                        .await?;
                    Ok(())
                })
            })
            .with_data_after("002_add_email_column", |db| {
                Box::pin(async move {
                    db.exec_raw(
                        "UPDATE users SET email = 'alice@example.com' WHERE name = 'Alice'",
                    )
                    .await?;
                    Ok(())
                })
            })
            .with_data_after("003_add_age_column", |db| {
                Box::pin(async move {
                    db.exec_raw("UPDATE users SET age = 30 WHERE name = 'Alice'")
                        .await?;
                    Ok(())
                })
            })
            .run(&*db)
            .await
            .unwrap();

        // Verify all data was inserted and updated correctly
        let result = query::select("users")
            .columns(&["name", "email", "age"])
            .where_eq("name", "Alice")
            .execute(db.as_ref())
            .await
            .unwrap();

        assert_eq!(result.len(), 1);
        let row = &result[0];
        assert_eq!(row.get("name").unwrap().as_str().unwrap(), "Alice");
        assert_eq!(
            row.get("email").unwrap().as_str().unwrap(),
            "alice@example.com"
        );
        assert_eq!(row.get("age").unwrap().as_i64().unwrap(), 30);
    }

    #[cfg(feature = "sqlite")]
    #[test_log::test(switchy_async::test)]
    async fn test_initial_setup_functionality() {
        let db = crate::create_empty_in_memory().await.unwrap();

        let migrations = vec![Arc::new(TestMigration::new(
            "001_create_users",
            "CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)",
            Some("DROP TABLE users"),
        )) as Arc<dyn Migration<'static> + 'static>];

        MigrationTestBuilder::new(migrations)
            .with_initial_setup(|db| {
                Box::pin(async move {
                    // Create a temporary table for setup
                    db.exec_raw("CREATE TABLE temp_setup (value TEXT)").await?;
                    db.exec_raw("INSERT INTO temp_setup VALUES ('setup_complete')")
                        .await?;
                    Ok(())
                })
            })
            .run(&*db)
            .await
            .unwrap();

        // Verify initial setup ran
        let result = query::select("temp_setup")
            .columns(&["value"])
            .execute(db.as_ref())
            .await
            .unwrap();

        assert_eq!(result.len(), 1);
        let row = &result[0];
        assert_eq!(
            row.get("value").unwrap().as_str().unwrap(),
            "setup_complete"
        );

        // Verify migration also ran
        let result = query::select("sqlite_master")
            .columns(&["name"])
            .where_eq("type", "table")
            .where_eq("name", "users")
            .execute(db.as_ref())
            .await
            .unwrap();
        assert_eq!(result.len(), 1);
    }

    #[cfg(feature = "sqlite")]
    #[test_log::test(switchy_async::test)]
    async fn test_breakpoint_with_nonexistent_migration_id() {
        let db = crate::create_empty_in_memory().await.unwrap();

        let migrations = vec![Arc::new(TestMigration::new(
            "001_create_users",
            "CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)",
            Some("DROP TABLE users"),
        )) as Arc<dyn Migration<'static> + 'static>];

        let result = MigrationTestBuilder::new(migrations)
            .with_data_before("999_nonexistent", |_db| Box::pin(async move { Ok(()) }))
            .run(&*db)
            .await;

        // Should return an error for non-existent migration
        assert!(result.is_err());
        let error_msg = format!("{:?}", result.unwrap_err());
        assert!(error_msg.contains("Migration '999_nonexistent' not found"));
    }

    #[cfg(feature = "sqlite")]
    #[test_log::test(switchy_async::test)]
    async fn test_rollback_works_with_breakpoints() {
        let db = crate::create_empty_in_memory().await.unwrap();

        let migrations = vec![
            Arc::new(TestMigration::new(
                "001_create_users",
                "CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)",
                Some("DROP TABLE users"),
            )) as Arc<dyn Migration<'static> + 'static>,
            Arc::new(TestMigration::new(
                "002_add_email_column",
                "ALTER TABLE users ADD COLUMN email TEXT",
                Some("ALTER TABLE users DROP COLUMN email"),
            )) as Arc<dyn Migration<'static> + 'static>,
        ];

        MigrationTestBuilder::new(migrations)
            .with_data_before("002_add_email_column", |db| {
                Box::pin(async move {
                    db.exec_raw("INSERT INTO users (name) VALUES ('Alice')")
                        .await?;
                    Ok(())
                })
            })
            .with_rollback() // Explicitly enable rollback
            .run(&*db)
            .await
            .unwrap();

        // After rollback, tables should not exist
        let result = query::select("sqlite_master")
            .columns(&["name"])
            .where_eq("type", "table")
            .where_eq("name", "users")
            .execute(db.as_ref())
            .await;
        assert!(result.is_err() || result.unwrap().is_empty());
    }
}