Liquibase ↔ Database Restore: Confirming Schema Sync After Backup Recovery
0 reputation · 01 Nov 2024, 15:06 UTC
0 reputation · 01 Nov 2024, 15:06 UTC
Determine whether a restored database backup can be considered in sync with the Liquibase changelog without generating false pending changesets due to backup‑time context or label settings.
Constraints include the possibility that the backup was taken while a specific context or label was active, which may cause Liquibase’s validation and drift‑detection mechanisms to report discrepancies that are purely contextual. Additionally, Liquibase tracks only changeset text via checksums, so any data loss or corruption in the restored tables would not be flagged by its built‑in checks.
A restored backup is in sync with the Liquibase changelog only when the DATABASECHANGELOG table in the restored database matches the current changelog file and the active context/label filters are identical to those used when the backup was taken. Liquibase itself cannot distinguish “real schema drift” from “context‑only differences” because it validates solely against the DATABASECHANGELOG records and checksums; it does not diff the live schema unless you run liquibase diff.
liquibase.contexts=prod (or a label) was active, the corresponding changesets are recorded in DATABASECHANGELOG. Restoring that backup under a different context (e.g., test) makes Liquibase report those changesets as “already executed”, not pending. The opposite—running with a context that excludes changesets that were applied at backup time—will show them as pending even though the schema objects exist.liquibase validate will flag a mismatch even if the physical schema is identical.liquibase status --changelog-file=db/changelog.xmlThis reads the restored DATABASECHANGELOG and lists changesets not present there. If the list is empty, Liquibase believes the schema matches the changelog under the current context/label settings.liquibase validate --changelog-file=db/changelog.xmlAny checksum mismatch indicates the changelog file has diverged from the backup‑time version.liquibase diff --reference-url=jdbc:postgresql://ref-host/refdb \\\n --url=jdbc:postgresql://restored-host/restoreddb \\\n --changelog-file=db/changelog.xmlThe output shows missing/extra tables, columns, indexes, etc., independent of DATABASECHANGELOG.md5sum of COPY ... TO STDOUT).If the backup was taken with a non‑default context or label, you must reproduce that exact filter when running the commands above, otherwise status and validate will evaluate a different subset of changesets.
What context/label values were active at backup time versus the current environment? If they differ, re‑run steps 1‑3 with the backup‑time context (e.g., --contexts=prod) to see the true pending set.
DATABASECHANGELOG table (i.e., a full schema+data backup, not a schema‑only dump).Use comments to ask for clarification. Post a solution as an answer.
31,275 reputation · 01 Nov 2024, 23:22 UTC
After a restore, execute liquibase status --contexts=<ctx> --labels=<lbl> where <ctx> and <lbl> match those used when the backup was taken. An empty pending list means the schema matches the changelog for that context configuration.
liquibase status --contexts=prod --labels=release1
If any pending changesets appear, run liquibase diffChangelog to see what SQL Liquibase would apply. A non‑empty script indicates genuine schema drift that would be applied if the context were active.
liquibase diffChangelog --contexts=prod --labels=release1
Liquibase does not verify row contents. After a restore, compare critical data against a trusted snapshot.
SELECT COUNT(*) FROM table_name;CHECKSUM TABLE table_name;SELECT pg_total_relation_size('table_name');dbdiff, Redgate SQL Compare) against a known‑good export.Only when both the status check and data‑level queries match the backup snapshot should the database be considered fully restored and synchronized.