Java.DBMigrationTools.What does validateOnMigrate do in Flyway?

validateOnMigrate tells Flyway to validate applied migrations against the local migration files before running migrate.
If validation fails → migration stops.

📌 One-liner:

“It ensures the database history matches the migration files before applying new migrations.”

What “validation” actually checks

When validateOnMigrate=true, Flyway compares:

1️⃣ Applied migrations vs local files

  • Same versions
  • Same descriptions
  • Same checksums

2️⃣ Detects dangerous situations

Flyway will fail startup if it finds:

  • ❌ Modified versioned migrations
  • ❌ Missing migration files
  • ❌ Out-of-order versions (unless explicitly allowed)
  • ❌ Failed migrations in history
  • ❌ Checksum mismatches

This happens before any SQL is executed.

What happens during migrate

Order of operations:

  1. Validate (if validateOnMigrate=true)
  2. Abort if validation fails
  3. Apply pending migrations

So:

validate → migrate

Not:

migrate → hope for the best

Why this setting exists (real reason)

Without validation:

  • A developer can modify an old migration
  • CI might still pass
  • Prod may silently diverge
  • You lose schema reproducibility

With validation:

  • Drift is detected early
  • Startup fails loudly
  • Teams are forced to fix history properly

📌 Interview phrase:

“It enforces immutability of applied migrations.”

Default value (important!)

  • Flyway default: true
  • You must explicitly disable it
flyway.validateOnMigrate=false

⚠️ Disabling it is usually a red flag

When would you ever disable it?

⚠️ Rare, controlled cases only

  • Legacy databases with known drift
  • Temporary migration cleanup phase
  • One-time bootstrap of existing schema

Even then:

  • Disable temporarily
  • Re-enable ASAP

Good interview line:

“Disabling validation should be a short-lived exception, not a standard configuration.”

Relation to flyway repair

Very important distinction:

| Feature             | Purpose         |
| ------------------- | --------------- |
| `validateOnMigrate` | Detect problems |
| `flyway.repair()`   | Fix metadata    |

Workflow:

validate fails
→ investigate
→ fix scripts OR run repair
→ migrate

Never:

validate fails
→ disable validation
→ migrate anyway ❌

Typical failure example

If someone edits an old migration:

Validate failed: Migration checksum mismatch for version 3

Correct response:

  • Restore original migration
  • Or introduce a new migration
  • Or (rarely) run repair after safe formatting changes

Interview-ready answer (perfect)

Short answer (2–3 sentences):

validateOnMigrate makes Flyway validate the applied migrations in flyway_schema_history against the local migration files before running migrate. If it detects checksum mismatches, missing migrations, or failed entries, migration is aborted. It’s a safety mechanism that enforces immutability and prevents schema drift.”

That’s a strong senior answer.

This entry was posted in Без рубрики. Bookmark the permalink.