Java.DBMigrationTools.What happens when a migration fails?

Short Answer

When a migration fails:

  • The migration process stops immediately
  • The failed changeset is not marked as executed
  • Any partial changes already applied by that changeset remain in the database unless you handle rollback manually

❗ No automatic rollback is performed unless you explicitly define and run one.


🔎 Liquibase – What happens on failure?

🔹 Suppose you run:

liquibase update

🔹 Scenario:

One of your changesets (e.g. id=3) has a syntax error or constraint violation.

🔸 What happens:

  • Liquibase stops at the error.
  • That changeset is not marked as executed in DATABASECHANGELOG
  • Previous changesets remain applied
  • Partial SQL from the failed changeset may have affected the DB, depending on DB settings and the operation

✅ You can:

  • Fix the issue
  • Re-run liquibase update (it will skip changesets already marked as executed)

🔹 Example:

<changeSet id="1" author="alice">
  <createTable tableName="users">
    <column name="id" type="bigint" autoIncrement="true"/>
  </createTable>
</changeSet>

<changeSet id="2" author="alice">
  <addColumn tableName="users">
    <column name="email" type="varchar(255)"/>
  </addColumn>
</changeSet>

<changeSet id="3" author="alice">
  <!-- This fails due to bad SQL -->
  <sql>ALTER TABLE users ADD age_in_days_typo INT</sql>
</changeSet>

✅ Changeset 3 fails
✅ Changesets 1 and 2 are marked as executed
❌ 3 is not
❗ You must fix the SQL, then re-run

🔹 Rollback?

Liquibase does not rollback automatically on failure.
You can:

  • Define a <rollback> section per changeset
  • Manually run:
liquibase rollbackCount 1

But rollback must be explicitly triggered, not automatic.

🔎 Flyway – What happens on failure?

In Flyway:

  • The failed script is recorded with a failure status in flyway_schema_history
  • The entire migration run is stopped
  • You must repair or fix the problem before continuing

✅ Fix the file or write a new script
✅ Then run:

flyway repair  # marks the failed migration as pending or removes bad checksum
flyway migrate

Flyway does not support rollback, so you must clean the database (dev only) or manually fix the schema.


📌 Key Takeaways

ToolOn FailureRollback?Recovery
LiquibaseStops, logs error❌ Manual (unless scripted)Fix issue → rerun update
FlywayStops, logs failure❌ Not supported nativelyFix + repair + re-run
This entry was posted in Без рубрики. Bookmark the permalink.