Drupal Update API + Database Abstraction Layer: Is Automatic Schema Rollback Feasible?
0 reputation · 30 Apr 2021, 13:47 UTC
0 reputation · 30 Apr 2021, 13:47 UTC
Investigate whether Drupal can automatically revert database schema changes applied by hook_update_N when a subsequent update fails, and identify the current state of this feature.
Drupal’s Update API applies schema changes via hook_update_N implementations, but the Database Abstraction Layer executes raw DDL without maintaining a version history. Consequently, a failed update leaves the database altered, and the only available rollback path is a manual database restore or a custom script.
While the Config API supports import/export rollbacks, it does not affect the underlying schema. Community discussions have highlighted the need for a versioned migration system, yet no consensus has been reached on integrating such a feature into core.
Is there a viable path to make schema changes rollback‑safe without manual intervention, and what would be required to add such capability to Drupal core?
hook_update_N if a subsequent update fails?34775 reputation · 30 Apr 2021, 18:22 UTC
No. Drupal core (including Drupal 10/11) provides no mechanism to automatically revert schema changes applied by hook_update_N when a later update fails. The Update API records that an update ran; it does not record how to undo it. Rollback safety today comes from external backups taken before running updates, not from Drupal itself.
The blocker is not just missing bookkeeping — it is how the underlying databases behave.
ALTER TABLE, CREATE TABLE, and DROP COLUMN commit the current transaction implicitly. Wrapping a hook_update_N in a transaction does not protect you: once the DDL executes, it is durable. PostgreSQL supports transactional DDL, but Drupal must behave consistently across drivers, so core cannot rely on that.DROP COLUMN cannot restore the data that was in the column. Some operations are inherently one-way, which is why a generic auto-rollback would give false confidence even if it existed.The update runner executes pending updates sequentially. If update N+3 throws, updates N through N+2 remain applied — both their schema changes and their "completed" markers. Re-running update.php or drush updatedb resumes from the failed update; it does not undo anything. You can verify this in a development environment by writing a module with two updates where the second throws deliberately: the first update's schema change persists.
drush sql:dump --result-file=pre-update.sql, then drush updatedb. On failure, restore the dump and the previous codebase together — schema and code must match.$schema->tableExists(), fieldExists()) so a partially-applied update can be safely re-run. Make data migrations resumable via the $sandbox batch mechanism rather than monolithic loops.The need for a versioned, reversible migration system has been raised repeatedly in the Drupal issue queues and community discussions over the years, but as of this writing there is no accepted core initiative that adds automatic schema rollback to the Update API. The implicit-commit problem on MySQL means any such feature would still require backup-based recovery for destructive changes, which weakens the case for building partial rollback into core. Treat any claim of an active, landed proposal as needing verification against the current drupal.org issue queue before relying on it.
Automatic schema rollback is not feasible as a general core feature, primarily because DDL cannot be transactionally reversed on Drupal's most common database engines and because destructive changes are not invertible at all. The supported, reliable strategy remains: backup before updates, test on a production copy, write idempotent updates, and restore code-plus-database together when something fails.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.