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
//! Convenience macros for defining migrations.

/// Define a simple SQL-only migration.
///
/// This macro reduces boilerplate for migrations that consist of simple SQL statements
/// that work identically across SQLite, MySQL, and PostgreSQL (or with minor dialect differences).
///
/// # Basic Usage
///
/// ```
/// use migratio::sql_migration;
///
/// // Define a migration struct with SQL
/// sql_migration!(CreateUsersTable, 1, "Create users table",
///     up: "CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT NOT NULL)",
///     down: "DROP TABLE users"
/// );
/// ```
///
/// This expands to a struct `CreateUsersTable` that implements the [`Migration`](crate::Migration) trait
/// with `sqlite_up`/`sqlite_down`, `mysql_up`/`mysql_down`, and `postgres_up`/`postgres_down` methods.
///
/// # Database-Specific SQL
///
/// When databases require different SQL syntax, provide separate statements:
///
/// ```
/// use migratio::sql_migration;
///
/// sql_migration!(CreateUsersTable, 1, "Create users table",
///     sqlite_up: "CREATE TABLE users (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT)",
///     sqlite_down: "DROP TABLE users",
///     mysql_up: "CREATE TABLE users (id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(255))",
///     mysql_down: "DROP TABLE users",
///     postgres_up: "CREATE TABLE users (id SERIAL PRIMARY KEY, name TEXT)",
///     postgres_down: "DROP TABLE users"
/// );
/// ```
///
/// For backwards compatibility, you can also specify just SQLite and MySQL - PostgreSQL will
/// use the SQLite SQL since they share most syntax:
///
/// ```
/// use migratio::sql_migration;
///
/// sql_migration!(CreateUsersTable, 1, "Create users table",
///     sqlite_up: "CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)",
///     sqlite_down: "DROP TABLE users",
///     mysql_up: "CREATE TABLE users (id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(255))",
///     mysql_down: "DROP TABLE users"
/// );
/// ```
///
/// # Up-Only Migrations
///
/// If your migration doesn't need a `down` implementation (common for production systems),
/// omit the `down` clauses:
///
/// ```
/// use migratio::sql_migration;
///
/// sql_migration!(CreateUsersTable, 1, "Create users table",
///     up: "CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT NOT NULL)"
/// );
/// ```
///
/// Calling `downgrade()` on such a migration will panic with a helpful error message.
///
/// # Multiple Statements
///
/// For migrations with multiple SQL statements, use an array:
///
/// ```
/// use migratio::sql_migration;
///
/// sql_migration!(InitialSchema, 1, "Create initial schema",
///     up: [
///         "CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)",
///         "CREATE TABLE posts (id INTEGER PRIMARY KEY, user_id INTEGER, title TEXT)",
///         "CREATE INDEX idx_posts_user ON posts(user_id)"
///     ],
///     down: [
///         "DROP INDEX idx_posts_user",
///         "DROP TABLE posts",
///         "DROP TABLE users"
///     ]
/// );
/// ```
///
/// # When to Use This Macro
///
/// Use `sql_migration!` when:
/// - Your migration is pure SQL with no Rust logic
/// - The SQL works on all databases (or you provide dialect-specific versions)
/// - You don't need to query data and transform it in Rust
///
/// For complex migrations that need to query data, transform it in Rust, and write it back,
/// implement the [`Migration`](crate::Migration) trait directly instead.
#[macro_export]
macro_rules! sql_migration {
    // Pattern 1a: Shared up/down SQL (array of statements)
    ($name:ident, $version:expr, $migration_name:expr,
        up: [$($up_sql:expr),* $(,)?],
        down: [$($down_sql:expr),* $(,)?]
    ) => {
        $crate::__sql_migration_impl!($name, $version, $migration_name,
            sqlite_up: [$($up_sql),*],
            sqlite_down: [$($down_sql),*],
            mysql_up: [$($up_sql),*],
            mysql_down: [$($down_sql),*],
            postgres_up: [$($up_sql),*],
            postgres_down: [$($down_sql),*]
        );
    };

    // Pattern 1b: Shared up/down SQL (single statement)
    ($name:ident, $version:expr, $migration_name:expr,
        up: $up_sql:expr,
        down: $down_sql:expr
    ) => {
        $crate::__sql_migration_impl!($name, $version, $migration_name,
            sqlite_up: [$up_sql],
            sqlite_down: [$down_sql],
            mysql_up: [$up_sql],
            mysql_down: [$down_sql],
            postgres_up: [$up_sql],
            postgres_down: [$down_sql]
        );
    };

    // Pattern 2a: Shared up SQL only (array, no down)
    ($name:ident, $version:expr, $migration_name:expr,
        up: [$($up_sql:expr),* $(,)?]
    ) => {
        $crate::__sql_migration_impl_no_down!($name, $version, $migration_name,
            sqlite_up: [$($up_sql),*],
            mysql_up: [$($up_sql),*],
            postgres_up: [$($up_sql),*]
        );
    };

    // Pattern 2b: Shared up SQL only (single statement, no down)
    ($name:ident, $version:expr, $migration_name:expr,
        up: $up_sql:expr
    ) => {
        $crate::__sql_migration_impl_no_down!($name, $version, $migration_name,
            sqlite_up: [$up_sql],
            mysql_up: [$up_sql],
            postgres_up: [$up_sql]
        );
    };

    // Pattern 3a: Database-specific SQL with down (single statements, all three DBs)
    ($name:ident, $version:expr, $migration_name:expr,
        sqlite_up: $sqlite_up:expr,
        sqlite_down: $sqlite_down:expr,
        mysql_up: $mysql_up:expr,
        mysql_down: $mysql_down:expr,
        postgres_up: $postgres_up:expr,
        postgres_down: $postgres_down:expr
    ) => {
        $crate::__sql_migration_impl!($name, $version, $migration_name,
            sqlite_up: [$sqlite_up],
            sqlite_down: [$sqlite_down],
            mysql_up: [$mysql_up],
            mysql_down: [$mysql_down],
            postgres_up: [$postgres_up],
            postgres_down: [$postgres_down]
        );
    };

    // Pattern 3b: Database-specific SQL with down (single statements, SQLite/MySQL only - backwards compat)
    // Uses SQLite SQL for Postgres since they share most syntax
    ($name:ident, $version:expr, $migration_name:expr,
        sqlite_up: $sqlite_up:expr,
        sqlite_down: $sqlite_down:expr,
        mysql_up: $mysql_up:expr,
        mysql_down: $mysql_down:expr
    ) => {
        $crate::__sql_migration_impl!($name, $version, $migration_name,
            sqlite_up: [$sqlite_up],
            sqlite_down: [$sqlite_down],
            mysql_up: [$mysql_up],
            mysql_down: [$mysql_down],
            postgres_up: [$sqlite_up],
            postgres_down: [$sqlite_down]
        );
    };

    // Pattern 4a: Database-specific SQL without down (single statements, all three DBs)
    ($name:ident, $version:expr, $migration_name:expr,
        sqlite_up: $sqlite_up:expr,
        mysql_up: $mysql_up:expr,
        postgres_up: $postgres_up:expr
    ) => {
        $crate::__sql_migration_impl_no_down!($name, $version, $migration_name,
            sqlite_up: [$sqlite_up],
            mysql_up: [$mysql_up],
            postgres_up: [$postgres_up]
        );
    };

    // Pattern 4b: Database-specific SQL without down (single statements, SQLite/MySQL only - backwards compat)
    // Uses SQLite SQL for Postgres since they share most syntax
    ($name:ident, $version:expr, $migration_name:expr,
        sqlite_up: $sqlite_up:expr,
        mysql_up: $mysql_up:expr
    ) => {
        $crate::__sql_migration_impl_no_down!($name, $version, $migration_name,
            sqlite_up: [$sqlite_up],
            mysql_up: [$mysql_up],
            postgres_up: [$sqlite_up]
        );
    };
}

/// Internal implementation macro with down methods.
#[macro_export]
#[doc(hidden)]
macro_rules! __sql_migration_impl {
    ($name:ident, $version:expr, $migration_name:expr,
        sqlite_up: [$($sqlite_up:expr),*],
        sqlite_down: [$($sqlite_down:expr),*],
        mysql_up: [$($mysql_up:expr),*],
        mysql_down: [$($mysql_down:expr),*],
        postgres_up: [$($postgres_up:expr),*],
        postgres_down: [$($postgres_down:expr),*]
    ) => {
        pub struct $name;

        impl $crate::Migration for $name {
            fn version(&self) -> u32 {
                $version
            }

            fn name(&self) -> String {
                $migration_name.to_string()
            }

            #[cfg(feature = "sqlite")]
            fn sqlite_up(&self, tx: &::rusqlite::Transaction) -> Result<(), $crate::Error> {
                $(tx.execute($sqlite_up, [])?;)*
                Ok(())
            }

            #[cfg(feature = "sqlite")]
            fn sqlite_down(&self, tx: &::rusqlite::Transaction) -> Result<(), $crate::Error> {
                $(tx.execute($sqlite_down, [])?;)*
                Ok(())
            }

            #[cfg(feature = "mysql")]
            fn mysql_up(&self, conn: &mut ::mysql::Conn) -> Result<(), $crate::Error> {
                use ::mysql::prelude::Queryable;
                $(conn.query_drop($mysql_up)?;)*
                Ok(())
            }

            #[cfg(feature = "mysql")]
            fn mysql_down(&self, conn: &mut ::mysql::Conn) -> Result<(), $crate::Error> {
                use ::mysql::prelude::Queryable;
                $(conn.query_drop($mysql_down)?;)*
                Ok(())
            }

            #[cfg(feature = "postgres")]
            fn postgres_up(&self, tx: &mut ::postgres::Transaction) -> Result<(), $crate::Error> {
                $(tx.execute($postgres_up, &[])?;)*
                Ok(())
            }

            #[cfg(feature = "postgres")]
            fn postgres_down(&self, tx: &mut ::postgres::Transaction) -> Result<(), $crate::Error> {
                $(tx.execute($postgres_down, &[])?;)*
                Ok(())
            }
        }
    };
}

/// Internal implementation macro without down methods.
#[macro_export]
#[doc(hidden)]
macro_rules! __sql_migration_impl_no_down {
    ($name:ident, $version:expr, $migration_name:expr,
        sqlite_up: [$($sqlite_up:expr),*],
        mysql_up: [$($mysql_up:expr),*],
        postgres_up: [$($postgres_up:expr),*]
    ) => {
        pub struct $name;

        impl $crate::Migration for $name {
            fn version(&self) -> u32 {
                $version
            }

            fn name(&self) -> String {
                $migration_name.to_string()
            }

            #[cfg(feature = "sqlite")]
            fn sqlite_up(&self, tx: &::rusqlite::Transaction) -> Result<(), $crate::Error> {
                $(tx.execute($sqlite_up, [])?;)*
                Ok(())
            }

            #[cfg(feature = "mysql")]
            fn mysql_up(&self, conn: &mut ::mysql::Conn) -> Result<(), $crate::Error> {
                use ::mysql::prelude::Queryable;
                $(conn.query_drop($mysql_up)?;)*
                Ok(())
            }

            #[cfg(feature = "postgres")]
            fn postgres_up(&self, tx: &mut ::postgres::Transaction) -> Result<(), $crate::Error> {
                $(tx.execute($postgres_up, &[])?;)*
                Ok(())
            }
        }
    };
}

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

    #[test]
    fn test_macro_compiles_shared_sql() {
        sql_migration!(TestMigration1, 1, "Test migration",
            up: "CREATE TABLE test (id INTEGER PRIMARY KEY)",
            down: "DROP TABLE test"
        );

        let m = TestMigration1;
        assert_eq!(m.version(), 1);
        assert_eq!(m.name(), "Test migration");
    }

    #[test]
    fn test_macro_compiles_up_only() {
        sql_migration!(TestMigration2, 2, "Test migration 2",
            up: "CREATE TABLE test2 (id INTEGER PRIMARY KEY)"
        );

        let m = TestMigration2;
        assert_eq!(m.version(), 2);
        assert_eq!(m.name(), "Test migration 2");
    }

    #[test]
    fn test_macro_compiles_multi_statement() {
        sql_migration!(TestMigration3, 3, "Multi-statement",
            up: [
                "CREATE TABLE a (id INTEGER PRIMARY KEY)",
                "CREATE TABLE b (id INTEGER PRIMARY KEY)"
            ],
            down: [
                "DROP TABLE b",
                "DROP TABLE a"
            ]
        );

        let m = TestMigration3;
        assert_eq!(m.version(), 3);
    }

    #[cfg(all(feature = "sqlite", feature = "mysql"))]
    #[test]
    fn test_macro_compiles_db_specific() {
        sql_migration!(TestMigration4, 4, "DB-specific",
            sqlite_up: "CREATE TABLE test (id INTEGER PRIMARY KEY AUTOINCREMENT)",
            sqlite_down: "DROP TABLE test",
            mysql_up: "CREATE TABLE test (id INT PRIMARY KEY AUTO_INCREMENT)",
            mysql_down: "DROP TABLE test"
        );

        let m = TestMigration4;
        assert_eq!(m.version(), 4);
    }

    #[cfg(feature = "sqlite")]
    #[test]
    fn test_macro_sqlite_runtime() {
        use crate::sqlite::SqliteMigrator;
        use rusqlite::Connection;

        sql_migration!(CreateUsers, 1, "Create users",
            up: "CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)",
            down: "DROP TABLE users"
        );

        sql_migration!(CreatePosts, 2, "Create posts",
            up: [
                "CREATE TABLE posts (id INTEGER PRIMARY KEY, user_id INTEGER, title TEXT)",
                "CREATE INDEX idx_posts_user ON posts(user_id)"
            ],
            down: [
                "DROP INDEX idx_posts_user",
                "DROP TABLE posts"
            ]
        );

        let migrator = SqliteMigrator::new(vec![Box::new(CreateUsers), Box::new(CreatePosts)]);

        let mut conn = Connection::open_in_memory().unwrap();

        // Run migrations
        let report = migrator.upgrade(&mut conn).unwrap();
        assert_eq!(report.migrations_run, vec![1, 2]);

        // Verify tables exist
        let tables: Vec<String> = conn
            .prepare("SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%' AND name != '_migratio_version_' ORDER BY name")
            .unwrap()
            .query_map([], |row| row.get(0))
            .unwrap()
            .collect::<Result<Vec<_>, _>>()
            .unwrap();
        assert_eq!(tables, vec!["posts", "users"]);

        // Verify index exists
        let index_count: i32 = conn
            .query_row(
                "SELECT COUNT(*) FROM sqlite_master WHERE type='index' AND name='idx_posts_user'",
                [],
                |row| row.get(0),
            )
            .unwrap();
        assert_eq!(index_count, 1);

        // Test downgrade
        let report = migrator.downgrade(&mut conn, 0).unwrap();
        assert_eq!(report.migrations_run, vec![2, 1]);

        // Verify tables are gone
        let table_count: i32 = conn
            .query_row(
                "SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name IN ('users', 'posts')",
                [],
                |row| row.get(0),
            )
            .unwrap();
        assert_eq!(table_count, 0);
    }
}