waypoint-core 0.6.0

Lightweight, Flyway-compatible SQL migration library for PostgreSQL and MySQL
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
//! MySQL implementation of the `migrate` command.
//!
//! The dialect-aware dispatchers (and engine-agnostic public types
//! [`MigrateReport`], [`MigrateDetail`]) live in [`crate::commands::migrate`].
//! This module owns the MySQL-specific apply loop, guard-evaluator variants,
//! and hook firing.
//!
//! MySQL DDL is non-transactional, so `--transaction` batch mode is not
//! supported here. `ensure` guards become verify-after rather than
//! rollback-if-false — the documented caveat.

use std::collections::HashMap;

use crate::commands::migrate::{
    GuardAction, MigrateDetail, MigrateReport, PendingCriteria, classify_ensure, classify_require,
    guard_parse_error, select_pending,
};
use crate::config::WaypointConfig;
use crate::db::DbClient;
use crate::error::{Result, WaypointError};
use crate::history;
use crate::hooks::{self, HookType, ResolvedHook};
use crate::migration::{MigrationVersion, ResolvedMigration, scan_migrations};
use crate::placeholder::{build_placeholders, replace_placeholders};

/// Dialect-aware `require` guard evaluator.
///
/// Shares its policy handling with the PostgreSQL path via
/// [`crate::commands::migrate::classify_require`]; only `guard::evaluate_db`
/// (which dispatches the underlying SQL per engine) differs.
async fn evaluate_require_guards_db(
    client: &DbClient,
    schema: &str,
    migration: &ResolvedMigration,
    config: &WaypointConfig,
) -> Result<GuardAction> {
    if !config.guards.enabled || migration.directives.require.is_empty() {
        return Ok(GuardAction::Continue);
    }
    for expr_str in &migration.directives.require {
        let expr = match crate::guard::parse(expr_str) {
            Ok(expr) => expr,
            Err(e) => {
                return Ok(GuardAction::Error(guard_parse_error(
                    "require",
                    &migration.script,
                    expr_str,
                    &e,
                )));
            }
        };
        let outcome = crate::guard::evaluate_db(client, schema, &expr).await;
        match classify_require(
            outcome,
            expr_str,
            &migration.script,
            &config.guards.on_require_fail,
        ) {
            GuardAction::Continue => {}
            other => return Ok(other),
        }
    }
    Ok(GuardAction::Continue)
}

/// Dialect-aware `ensure` guard evaluator.
async fn evaluate_ensure_guards_db(
    client: &DbClient,
    schema: &str,
    migration: &ResolvedMigration,
    config: &WaypointConfig,
) -> Result<()> {
    if !config.guards.enabled {
        return Ok(());
    }
    for expr_str in &migration.directives.ensure {
        let expr = crate::guard::parse(expr_str)
            .map_err(|e| guard_parse_error("ensure", &migration.script, expr_str, &e))?;
        let outcome = crate::guard::evaluate_db(client, schema, &expr).await;
        classify_ensure(outcome, expr_str, &migration.script)?;
    }
    Ok(())
}

/// Execute the migrate command (MySQL).
#[deprecated(
    since = "0.6.0",
    note = "Superseded by `execute_with_options`, which additionally takes the `force` flag. Will be removed in 1.0."
)]
pub async fn execute(
    client: &DbClient,
    config: &WaypointConfig,
    target_version: Option<&str>,
) -> Result<MigrateReport> {
    execute_with_options(client, config, target_version, false).await
}

/// Execute the migrate command with options (MySQL).
pub async fn execute_with_options(
    client: &DbClient,
    config: &WaypointConfig,
    target_version: Option<&str>,
    _force: bool,
) -> Result<MigrateReport> {
    if config.migrations.batch_transaction && !client.dialect().supports_transactional_ddl() {
        return Err(WaypointError::ConfigError(format!(
            "batch_transaction is not supported on {} — DDL is not transactional on this engine. \
             Remove `batch_transaction = true` or `--transaction` to proceed.",
            client.dialect_kind().name()
        )));
    }

    let table = &config.migrations.table;

    client.acquire_lock(table).await?;

    let result = run_migrate(client, config, target_version).await;

    if let Err(e) = client.release_lock(table).await {
        log::error!("Failed to release advisory lock: {}", e);
    }

    match &result {
        Ok(report) => {
            log::info!(
                "Migrate completed (mysql); migrations_applied={}, total_time_ms={}",
                report.migrations_applied,
                report.total_time_ms
            );
        }
        Err(e) => {
            log::error!("Migrate failed (mysql): {}", e);
        }
    }

    result
}

async fn run_migrate(
    client: &DbClient,
    config: &WaypointConfig,
    target_version: Option<&str>,
) -> Result<MigrateReport> {
    let schema = client.resolve_schema(&config.migrations.schema).await?;
    let table = &config.migrations.table;

    history::create_history_table_db(client, &schema, table).await?;

    if config.migrations.validate_on_migrate
        && let Err(e) = crate::commands::validate::execute_db(client, config).await
    {
        match &e {
            WaypointError::ValidationFailed(_) => return Err(e),
            _ => log::debug!("Validation skipped: {}", e),
        }
    }

    if config.preflight.enabled {
        let report = crate::preflight::run_preflight_db(client, &config.preflight).await?;
        if !report.passed {
            let failed_checks: Vec<String> = report
                .checks
                .iter()
                .filter(|c| c.status == crate::preflight::CheckStatus::Fail)
                .map(|c| format!("{}: {}", c.name, c.detail))
                .collect();
            return Err(WaypointError::PreflightFailed {
                checks: failed_checks.join("; "),
            });
        }
    }

    let resolved = scan_migrations(&config.migrations.locations)?;
    let applied = history::get_applied_migrations_db(client, &schema, table).await?;

    let mut all_hooks: Vec<ResolvedHook> = hooks::scan_hooks(&config.migrations.locations)?;
    let config_hooks = hooks::load_config_hooks(&config.hooks)?;
    all_hooks.extend(config_hooks);

    let db_user = client
        .current_user()
        .await
        .unwrap_or_else(|_| "unknown".into());
    let db_name = client
        .current_database()
        .await
        .unwrap_or_else(|_| "unknown".into());
    let installed_by = config
        .migrations
        .installed_by
        .as_deref()
        .unwrap_or(&db_user)
        .to_string();

    let target = target_version.map(MigrationVersion::parse).transpose()?;
    let baseline_version = applied
        .iter()
        .find(|a| a.migration_type == "BASELINE")
        .and_then(|a| a.version.as_ref())
        .map(|v| MigrationVersion::parse(v))
        .transpose()?;
    let effective_versions = history::effective_applied_versions(&applied);
    let highest_applied = effective_versions
        .iter()
        .filter_map(|v| MigrationVersion::parse(v).ok())
        .max();
    let applied_scripts: HashMap<String, Option<i32>> = applied
        .iter()
        .filter(|a| a.success && a.version.is_none())
        .map(|a| (a.script.clone(), a.checksum))
        .collect();
    let current_env = config.migrations.environment.as_deref();

    let pending = select_pending(
        &resolved,
        &PendingCriteria {
            effective_versions: &effective_versions,
            baseline_version: baseline_version.as_ref(),
            target: target.as_ref(),
            highest_applied: highest_applied.as_ref(),
            applied_scripts: &applied_scripts,
            current_env,
            out_of_order: config.migrations.out_of_order,
            dependency_ordering: config.migrations.dependency_ordering,
        },
    )?;
    let pending_repeatables = pending.repeatables;

    let mut report = MigrateReport {
        migrations_applied: 0,
        total_time_ms: 0,
        details: Vec::new(),
        hooks_executed: 0,
        hooks_time_ms: 0,
    };

    // `beforeMigrate` / `afterMigrate` fire on every run, matching the
    // PostgreSQL path and Flyway, so that hooks which prepare or tear down
    // session state observe a consistent lifecycle even on a no-op run.
    let placeholders = build_placeholders(
        &config.placeholders,
        &schema,
        &db_user,
        &db_name,
        "beforeMigrate",
    );
    fire_hooks(
        client,
        &all_hooks,
        &HookType::BeforeMigrate,
        &placeholders,
        &mut report,
    )
    .await?;

    for m in pending.versioned {
        let placeholders =
            build_placeholders(&config.placeholders, &schema, &db_user, &db_name, &m.script);

        match evaluate_require_guards_db(client, &schema, m, config).await? {
            GuardAction::Continue => {}
            GuardAction::Skip => continue,
            GuardAction::Error(e) => return Err(e),
        }

        fire_hooks(
            client,
            &all_hooks,
            &HookType::BeforeEachMigrate,
            &placeholders,
            &mut report,
        )
        .await?;

        let before_snapshot = if config.reversals.enabled && m.is_versioned() {
            Some(crate::reversal::capture_before_db(client, &schema).await?)
        } else {
            None
        };

        let elapsed = apply_one(client, m, &schema, table, &installed_by, &placeholders).await?;
        report.migrations_applied += 1;
        report.total_time_ms += elapsed;
        report.details.push(MigrateDetail {
            version: m.version().map(|v| v.raw.clone()),
            description: m.description.clone(),
            script: m.script.clone(),
            execution_time_ms: elapsed,
        });

        // ensure guards run AFTER the migration. On MySQL DDL has already
        // auto-committed, so an ensure-failure does NOT roll back the
        // migration — it surfaces as a hard error and leaves the schema in
        // the post-migration state. This is the documented MySQL caveat.
        evaluate_ensure_guards_db(client, &schema, m, config).await?;

        if let (Some(before), Some(ver)) = (before_snapshot.as_ref(), m.version()) {
            match crate::reversal::generate_reversal_db(
                client,
                &schema,
                before,
                config.reversals.warn_data_loss,
            )
            .await
            {
                Ok(result) => {
                    if let Some(ref reversal_sql) = result.reversal_sql {
                        if let Err(e) = crate::reversal::store_reversal_db(
                            client,
                            &schema,
                            table,
                            &ver.raw,
                            reversal_sql,
                        )
                        .await
                        {
                            log::warn!(
                                "Failed to store reversal SQL; version={}, error={}",
                                ver.raw,
                                e
                            );
                        }
                        for w in &result.warnings {
                            log::warn!("Reversal warning for version {}: {}", ver.raw, w);
                        }
                    }
                }
                Err(e) => {
                    log::warn!("Failed to generate reversal for version {}: {}", ver.raw, e);
                }
            }
        }

        fire_hooks(
            client,
            &all_hooks,
            &HookType::AfterEachMigrate,
            &placeholders,
            &mut report,
        )
        .await?;
    }

    for m in pending_repeatables {
        let placeholders =
            build_placeholders(&config.placeholders, &schema, &db_user, &db_name, &m.script);

        match evaluate_require_guards_db(client, &schema, m, config).await? {
            GuardAction::Continue => {}
            GuardAction::Skip => continue,
            GuardAction::Error(e) => return Err(e),
        }

        fire_hooks(
            client,
            &all_hooks,
            &HookType::BeforeEachMigrate,
            &placeholders,
            &mut report,
        )
        .await?;

        let elapsed = apply_one(client, m, &schema, table, &installed_by, &placeholders).await?;
        report.migrations_applied += 1;
        report.total_time_ms += elapsed;
        report.details.push(MigrateDetail {
            version: None,
            description: m.description.clone(),
            script: m.script.clone(),
            execution_time_ms: elapsed,
        });

        evaluate_ensure_guards_db(client, &schema, m, config).await?;

        fire_hooks(
            client,
            &all_hooks,
            &HookType::AfterEachMigrate,
            &placeholders,
            &mut report,
        )
        .await?;
    }

    let placeholders = build_placeholders(
        &config.placeholders,
        &schema,
        &db_user,
        &db_name,
        "afterMigrate",
    );
    fire_hooks(
        client,
        &all_hooks,
        &HookType::AfterMigrate,
        &placeholders,
        &mut report,
    )
    .await?;

    Ok(report)
}

/// Run all hooks of `phase` and fold the result into `report`.
async fn fire_hooks(
    client: &DbClient,
    all_hooks: &[ResolvedHook],
    phase: &HookType,
    placeholders: &HashMap<String, String>,
    report: &mut MigrateReport,
) -> Result<()> {
    let (count, ms) = hooks::run_hooks_db(client, all_hooks, phase, placeholders).await?;
    report.hooks_executed += count;
    report.hooks_time_ms += ms;
    Ok(())
}

async fn apply_one(
    client: &DbClient,
    m: &ResolvedMigration,
    schema: &str,
    table: &str,
    installed_by: &str,
    placeholders: &HashMap<String, String>,
) -> Result<i32> {
    let sql = replace_placeholders(&m.sql, placeholders)?;
    log::info!("Applying migration; script={}", m.script);

    let version = m.version().map(|v| v.raw.as_str());
    let migration_type = m.migration_type().to_string();

    let outcome = client.execute_raw(&sql).await;

    // Record the outcome either way. MySQL DDL auto-commits statement by
    // statement, so a mid-file failure leaves the schema partially migrated —
    // a `success = false` row is the only trace of that, and it is what
    // `info` surfaces as FAILED and `repair` clears. Mirrors the PostgreSQL
    // apply path and the MySQL undo path.
    let elapsed = match outcome {
        Ok(elapsed) => elapsed,
        Err(e) => {
            let reason = e.to_string();
            if let Err(record_err) = history::insert_applied_migration_db(
                client,
                schema,
                table,
                version,
                &m.description,
                &migration_type,
                &m.script,
                Some(m.checksum),
                installed_by,
                0,
                false,
            )
            .await
            {
                log::warn!(
                    "Failed to record migration failure in history table; script={}, error={}",
                    m.script,
                    record_err
                );
            }
            log::error!("Migration failed; script={}, reason={}", m.script, reason);
            return Err(WaypointError::MigrationFailed {
                script: m.script.clone(),
                reason,
            });
        }
    };

    history::insert_applied_migration_db(
        client,
        schema,
        table,
        version,
        &m.description,
        &migration_type,
        &m.script,
        Some(m.checksum),
        installed_by,
        elapsed,
        true,
    )
    .await?;

    Ok(elapsed)
}