Repository navigation
Expand file tree
/
Copy pathmigration.rs
More file actions
499 lines (453 loc) · 18 KB
/
Copy pathmigration.rs
File metadata and controls
499 lines (453 loc) · 18 KB
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
//! Migration contract, runner, seeder, and factory traits.
//!
//! [`Migrator`] executes registered [`Migration`] bodies against a live
//! [`DbPool`], recording each in a tracking table (default `migrations`) so
//! re-runs are idempotent and the last batch can be rolled back in reverse. The
//! tracking-table name is configurable via [`Migrator::with_migrations_table`]
//! to honour `[database.migrations] table`; the default keeps existing callers
//! unchanged. The sync `up_sql`/`down_sql` emit API is retained for backward
//! compatibility. Only the `sqlx` runtime API is used — never the compile-time
//! `query!` macros.
use crate::db::DbPool;
use crate::error::{OrmError, Result};
use crate::types::Value;
use chrono::Utc;
use std::collections::HashSet;
use std::sync::Arc;
mod fresh;
mod traits;
pub use traits::{run_migrations, Factory, MigrationRecord, Seeder};
/// Name of the migration tracking table.
///
/// Used when no `[database.migrations] table` override is supplied; see
/// [`Migrator::with_migrations_table`] and [`Migrator::migrations_table`].
const MIGRATIONS_TABLE: &str = "migrations";
/// Process-wide migration registry populated by an application at startup.
///
/// `cargo artisan migrate` reads this registry so it executes the app's
/// migrations without the CLI depending on the application crate. Register
/// migrations with [`register_migration`] (order = execution order) and seeders
/// with [`register_seeder`]; [`registered_migrator`] returns a clone for the
/// runner.
static MIGRATIONS: std::sync::OnceLock<std::sync::Mutex<Migrator>> = std::sync::OnceLock::new();
/// Register a migration into the process-wide registry.
///
/// Call once per migration from application boot code (`main`/provider) in the
/// intended execution order; the CLI's `migrate` command then runs them.
/// Aborts (panics) if the registry mutex was poisoned mid-registration.
pub fn register_migration<M: Migration + 'static>(migration: M) {
let registry = MIGRATIONS.get_or_init(|| std::sync::Mutex::new(Migrator::new()));
lock_registry_for_write(registry, "register_migration").add(migration);
}
/// Register a seeder into the process-wide registry.
///
/// Seeders run (in registration order) when `migrate --seed` or `migrate:fresh
/// --seed` executes. Aborts (panics) if the registry mutex was poisoned.
pub fn register_seeder<S: Seeder + 'static>(seeder: S) {
let registry = MIGRATIONS.get_or_init(|| std::sync::Mutex::new(Migrator::new()));
lock_registry_for_write(registry, "register_seeder").add_seeder(seeder);
}
/// Clone the process-wide registry for execution.
///
/// Returns an empty [`Migrator`] when nothing has been registered. Recovers a
/// poisoned lock via [`std::sync::PoisonError::into_inner`] — see the helper.
pub fn registered_migrator() -> Migrator {
MIGRATIONS
.get()
.map(|registry| lock_registry_for_read(registry).clone())
.unwrap_or_default()
}
/// Lock the registry for a mutating registration, aborting on poison.
///
/// A poisoned lock may be mid-mutation, so registration panics rather than
/// silently dropping the entry; `operation` names the failing call site.
fn lock_registry_for_write<'a>(
registry: &'a std::sync::Mutex<Migrator>,
operation: &str,
) -> std::sync::MutexGuard<'a, Migrator> {
registry.lock().unwrap_or_else(|_poison| {
panic!(
"migration registry poisoned during {operation}: a prior holder \
panicked mid-registration; aborting to avoid silent data loss"
)
})
}
/// Lock the registry for a read-only clone, recovering a poisoned lock.
///
/// Recovering via [`std::sync::PoisonError::into_inner`] is safe here: the worst
/// case is a partially-registered snapshot, never a dropped registration.
fn lock_registry_for_read(
registry: &std::sync::Mutex<Migrator>,
) -> std::sync::MutexGuard<'_, Migrator> {
registry
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
}
/// Errors surfaced by migrations.
#[derive(Debug, thiserror::Error)]
pub enum MigrationError {
/// `up` was declared irreversible (no matching `down`).
#[error("migration {name} is irreversible")]
Irreversible { name: String },
/// A required Postgres extension (e.g. `vector`) is missing.
#[error("extension {extension} missing: {hint}")]
ExtensionMissing { extension: String, hint: String },
/// The migration body failed.
#[error("migration {name} failed: {detail}")]
Failed {
/// Migration that failed.
name: String,
/// Failure detail.
detail: String,
},
}
/// A versioned, reversible migration with `up`/`down`.
pub trait Migration: Send + Sync {
/// Unique migration name (`2027_01_01_000001_create_users_table`).
fn name(&self) -> &str;
/// Compile the forward SQL body.
fn up(&self) -> Result<String>;
/// Compile the reverse SQL body; `Err(Irreversible)` when not revertable.
fn down(&self) -> Result<String> {
Err(MigrationError::Irreversible {
name: self.name().to_string(),
}
.into())
}
}
/// Runner for an ordered migration set, tracking the `migrations` table.
///
/// The tracking table defaults to `migrations`; a `[database.migrations] table`
/// override is threaded through [`Migrator::with_migrations_table`] without
/// changing existing call sites.
#[derive(Default, Clone)]
pub struct Migrator {
migrations: Vec<Arc<dyn Migration>>,
seeders: Vec<Arc<dyn Seeder>>,
/// Optional tracking-table override; `None` uses [`MIGRATIONS_TABLE`].
table: Option<String>,
}
impl Migrator {
/// Create an empty migrator.
pub fn new() -> Self {
Self::default()
}
/// Override the migration tracking-table name.
///
/// Supply `[database.migrations] table` (see
/// [`crate::connections::DatabaseConfig::migrations_table`]) so the runner
/// records into the configured table. A blank name is ignored. Returns
/// `&mut Self` for builder chaining.
pub fn with_migrations_table(&mut self, table: impl Into<String>) -> &mut Self {
let table = table.into();
if !table.trim().is_empty() {
self.table = Some(table);
}
self
}
/// The effective tracking-table name (`migrations` unless overridden).
pub fn migrations_table(&self) -> &str {
self.table.as_deref().unwrap_or(MIGRATIONS_TABLE)
}
/// Register a migration (order of registration = order of execution).
pub fn add<M: Migration + 'static>(&mut self, migration: M) -> &mut Self {
self.migrations.push(Arc::new(migration));
self
}
/// Register a seeder run by [`Migrator::seed`].
pub fn add_seeder<S: Seeder + 'static>(&mut self, seeder: S) -> &mut Self {
self.seeders.push(Arc::new(seeder));
self
}
/// Whether any migration has been registered.
pub fn is_empty(&self) -> bool {
self.migrations.is_empty()
}
/// List registered migration names in execution order.
pub fn names(&self) -> Vec<String> {
self.migrations
.iter()
.map(|m| m.name().to_string())
.collect()
}
/// Compile the pending `up` SQL statements (emit-only, no round-trip).
pub fn up_sql(&self) -> Result<Vec<String>> {
self.migrations.iter().map(|m| m.up()).collect()
}
/// Compile `down` SQL in reverse order (emit-only, no round-trip).
pub fn down_sql(&self) -> Result<Vec<String>> {
let mut out = Vec::with_capacity(self.migrations.len());
for m in self.migrations.iter().rev() {
out.push(m.down()?);
}
Ok(out)
}
/// Apply every not-yet-applied migration in registration order.
///
/// Each migration's schema body and its tracking row commit atomically in a
/// single transaction (one transaction per migration). SQLite/Postgres DDL is
/// transactional, so a record failure rolls the schema back too; MySQL DDL is
/// non-transactional, so its schema change is best-effort and may persist even
/// when the tracking row rolls back. Creates the tracking table first, and
/// a second call is a no-op. Returns the names applied by this call.
pub async fn run(&self, pool: &DbPool) -> Result<Vec<String>> {
self.ensure_table(pool).await?;
let applied = self.applied_names(pool).await?;
let pending: Vec<&Arc<dyn Migration>> = self
.migrations
.iter()
.filter(|m| !applied.contains(m.name()))
.collect();
if pending.is_empty() {
return Ok(Vec::new());
}
let batch = self.max_batch(pool).await? + 1;
let mut applied_now = Vec::with_capacity(pending.len());
for migration in pending {
let name = migration.name().to_string();
let sql = migration.up()?;
let applied = name.clone();
let table = self.migrations_table().to_string();
crate::execution::transaction(pool, |tx| {
Box::pin(async move {
tx.execute_script(&sql)
.await
.map_err(|error| failed(&name, error))?;
tx.execute_bind(
&format!(
"INSERT INTO {table} (name, batch, executed_at) \
VALUES ($1, $2, $3)"
),
&[
Value::Text(name.clone()),
Value::Int(batch),
Value::Text(Utc::now().to_rfc3339()),
],
)
.await?;
Ok(())
})
})
.await?;
applied_now.push(applied);
}
Ok(applied_now)
}
/// Revert the most recent batch in reverse registration order.
///
/// Each migration's reverse schema body and its tracking-row deletion commit
/// atomically in a single transaction (one transaction per migration); the
/// record is deleted only after a successful revert, so a failed `down` leaves
/// the row intact for a retry. A migration whose `down` is the default surfaces
/// [`MigrationError::Irreversible`]. An empty tracking table is a no-op.
pub async fn rollback(&self, pool: &DbPool) -> Result<Vec<String>> {
self.ensure_table(pool).await?;
let batch = self.max_batch(pool).await?;
if batch == 0 {
return Ok(Vec::new());
}
let batch_names = self.batch_names(pool, batch).await?;
let mut rolled_back = Vec::new();
for migration in self.migrations.iter().rev() {
let name = migration.name().to_string();
if !batch_names.contains(&name) {
continue;
}
let sql = migration.down()?;
let rolled = name.clone();
let table = self.migrations_table().to_string();
crate::execution::transaction(pool, |tx| {
Box::pin(async move {
tx.execute_script(&sql)
.await
.map_err(|error| failed(&name, error))?;
tx.execute_bind(
&format!("DELETE FROM {table} WHERE name = $1"),
&[Value::Text(name.clone())],
)
.await?;
Ok(())
})
})
.await?;
rolled_back.push(rolled);
}
Ok(rolled_back)
}
/// Drop every table, then re-run all migrations from scratch.
///
/// Returns the names applied. The tracking table is dropped with the rest,
/// so the following [`Migrator::run`] treats every migration as pending.
pub async fn fresh(&self, pool: &DbPool) -> Result<Vec<String>> {
fresh::drop_all_tables(pool).await?;
self.run(pool).await
}
/// Run every registered seeder against `pool`.
///
/// Returns the names of the seeders that ran; seeders are expected to be
/// idempotent so repeated calls do not duplicate rows.
pub async fn seed(&self, pool: &DbPool) -> Result<Vec<String>> {
let mut ran = Vec::with_capacity(self.seeders.len());
for seeder in &self.seeders {
seeder.run(pool).await?;
ran.push(seeder.name().to_string());
}
Ok(ran)
}
/// Create the tracking table (configured name) when absent.
async fn ensure_table(&self, pool: &DbPool) -> Result<()> {
let table = self.migrations_table();
pool.execute_script(&format!(
"CREATE TABLE IF NOT EXISTS {table} (
name TEXT PRIMARY KEY,
batch INTEGER NOT NULL,
executed_at TEXT NOT NULL
);"
))
.await
}
/// Names already recorded in the tracking table.
async fn applied_names(&self, pool: &DbPool) -> Result<HashSet<String>> {
let table = self.migrations_table();
let rows = pool
.fetch_json(
&format!("SELECT name FROM {table} ORDER BY batch, name"),
&[],
)
.await?;
Ok(rows
.iter()
.filter_map(|row| row.get("name").and_then(|n| n.as_str()).map(str::to_string))
.collect())
}
/// Highest recorded batch number, or `0` when none exist.
async fn max_batch(&self, pool: &DbPool) -> Result<i64> {
let table = self.migrations_table();
let rows = pool
.fetch_json(
&format!("SELECT COALESCE(MAX(batch), 0) AS batch FROM {table}"),
&[],
)
.await?;
Ok(rows
.first()
.and_then(|row| row.get("batch"))
.and_then(|b| b.as_i64())
.unwrap_or(0))
}
/// Names recorded for a specific batch.
async fn batch_names(&self, pool: &DbPool, batch: i64) -> Result<HashSet<String>> {
let table = self.migrations_table();
let rows = pool
.fetch_json(
&format!("SELECT name FROM {table} WHERE batch = $1"),
&[Value::Int(batch)],
)
.await?;
Ok(rows
.iter()
.filter_map(|row| row.get("name").and_then(|n| n.as_str()).map(str::to_string))
.collect())
}
}
/// Map a driver failure onto a `Failed` migration error, preserving typed errors.
fn failed(name: &str, error: OrmError) -> OrmError {
match error {
OrmError::Migration(inner) => OrmError::Migration(inner),
other => OrmError::Migration(MigrationError::Failed {
name: name.to_string(),
detail: other.to_string(),
}),
}
}
#[cfg(test)]
mod tests {
use super::*;
struct CreateUsers;
impl Migration for CreateUsers {
fn name(&self) -> &str {
"0001_create_users_table"
}
fn up(&self) -> Result<String> {
Ok("CREATE TABLE users (id UUID PRIMARY KEY);".into())
}
fn down(&self) -> Result<String> {
Ok("DROP TABLE users;".into())
}
}
/// Verifies up/down ordering and reverse execution.
#[test]
fn migrator_reverses_in_order() {
let mut m = Migrator::new();
m.add(CreateUsers);
assert_eq!(m.names(), vec!["0001_create_users_table"]);
assert!(m.up_sql().unwrap()[0].starts_with("CREATE"));
assert!(m.down_sql().unwrap()[0].starts_with("DROP"));
}
/// Poison a fresh registry mutex by panicking while its lock is held.
fn poisoned_registry() -> std::sync::Arc<std::sync::Mutex<Migrator>> {
let registry = std::sync::Arc::new(std::sync::Mutex::new(Migrator::new()));
let handle = {
let registry = std::sync::Arc::clone(®istry);
std::thread::spawn(move || {
let _guard = registry.lock().expect("fresh lock is unpoisoned");
panic!("intentional poison");
})
};
assert!(handle.join().is_err());
registry
}
/// Registration must abort loudly when the registry lock is poisoned.
#[test]
fn registration_aborts_on_poisoned_lock() {
let registry = poisoned_registry();
let outcome = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
drop(lock_registry_for_write(®istry, "register_migration"));
}));
let payload = outcome.expect_err("poisoned write lock must panic");
let message = payload
.downcast_ref::<String>()
.map(String::as_str)
.or_else(|| payload.downcast_ref::<&str>().copied())
.unwrap_or("");
assert!(message.contains("poisoned"), "unexpected panic: {message}");
assert!(message.contains("register_migration"));
}
/// Read-only clone must recover a poisoned lock instead of swallowing it.
#[test]
fn read_recovers_poisoned_lock() {
let registry = poisoned_registry();
let guard = lock_registry_for_read(®istry);
assert!(guard.is_empty());
}
/// Verifies default down is irreversible.
#[test]
fn irreversible_by_default() {
struct NoDown;
impl Migration for NoDown {
fn name(&self) -> &str {
"0002_irreversible"
}
fn up(&self) -> Result<String> {
Ok("SELECT 1;".into())
}
}
let mut m = Migrator::new();
m.add(NoDown);
assert!(matches!(
m.down_sql().unwrap_err(),
crate::error::OrmError::Migration(MigrationError::Irreversible { .. })
));
}
/// The tracking table defaults to `migrations` and honours an override.
#[test]
fn migrations_table_defaults_and_overrides() {
let mut m = Migrator::new();
assert_eq!(m.migrations_table(), "migrations");
m.with_migrations_table("app_migrations");
assert_eq!(m.migrations_table(), "app_migrations");
// A blank override is ignored so the default is never lost.
m.with_migrations_table(" ");
assert_eq!(m.migrations_table(), "app_migrations");
}
}