Read the Docs and PostgreSQL: Schema Migration Rollback Behavior
0 reputation · 27 Mar 2024, 19:28 UTC
0 reputation · 27 Mar 2024, 19:28 UTC
Read the Docs utilizes the Django migration system to manage its PostgreSQL schema. During instance upgrades, migrations are applied to the database to ensure compatibility with the new release. However, the current architecture does not provide an automated mechanism to revert these changes if a deployment fails or if a specific migration introduces instability.
When a schema change is applied, the system relies on manual intervention via management commands to return to a previous state. There is an unresolved design consideration regarding whether migration steps should be wrapped in transactions to allow for automatic rollbacks upon failure, particularly for environments integrated with automated build triggers.
Does the current stable v2.0 series support transactional migration wrappers to prevent partial schema updates? If not, what is the recommended strategy for ensuring schema consistency without relying on full database restores?
34775 reputation · 28 Mar 2024, 03:19 UTC
The Read the Docs (RTD) v2.0 series utilizes the Django migration framework. Because it targets PostgreSQL, transactional migration wrappers are supported and enabled by default. Django wraps each migration in a transaction, meaning if a migration fails midway, PostgreSQL rolls back the changes to the state prior to that specific migration's start.
Despite default transactional behavior, partial schema updates can still persist in the following scenarios:
atomic = False bypasses the transaction wrapper.CREATE INDEX CONCURRENTLY). These must be run with atomic = False, leaving the database in a partial state if they fail.To ensure consistency without relying on full database restores, implement the following workflow:
atomic = False. If a migration must be non-atomic, ensure it is idempotent so it can be safely re-run.python manage.py migrate <app_name> <migration_name>
Note: This requires the migration to have a defined reverse operation.To verify the current state of your schema after a failed upgrade, run:
python manage.py showmigrations
Check for migrations that are marked as applied ([X]) but may have failed partially due to atomic = False settings.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.