Skip to content

Remember instance refs and add safe rollback choices - #530

Merged
corrin merged 3 commits into
mainfrom
agent/safe-release-rollback
Aug 8, 2026
Merged

Remember instance refs and add safe rollback choices#530
corrin merged 3 commits into
mainfrom
agent/safe-release-rollback

Conversation

@corrin

@corrin corrin commented Aug 8, 2026

Copy link
Copy Markdown
Owner

Summary

  • persist the Git ref tracked by each instance so a bare deploy resolves the correct branch
  • append successful creates, deploys, and rollbacks to an instance deployment history with the relevant SHAs
  • replace the backup-only rollback script with one command offering two explicit database choices:
    • retain the latest database and apply the required reverse migrations (default)
    • restore the pre-deploy backup paired with the selected release (--restore-backup)
  • retain complete releases for 14 days after their last activation so recent rollback targets remain available

Why

Instance branch tracking was previously only a prose convention while deploy.sh used origin/production globally. The instance state exposed only the latest current/previous pair, and rollback always restored a database snapshot. That made it too easy to deploy the wrong branch to UAT and made a Tuesday rollback unnecessarily discard data written since Monday's release.

This gives the operator the information and choices needed to make that judgment directly: see the installed SHA history, choose a target SHA, and either reverse the latest database's migrations or restore the paired snapshot.

Jira: KAN-336

Operator commands

sudo scripts/server/instance.sh history <client> <env>
sudo scripts/rollback.sh <instance> <target-8-char-sha>
sudo scripts/rollback.sh <instance> <target-8-char-sha> --restore-backup

Both rollback modes show recent deployment history, require confirmation, take a fresh safety backup, validate the target release, and leave services stopped on a failed reverse migration.

Validation

  • bash scripts/test_release_utils.sh
  • python manage.py test apps.workflow.tests.test_rollback_migrations
  • shell syntax and ShellCheck for the touched server scripts
  • strict mypy for the new management command and tests
  • all commit hooks
  • all pre-push hooks, including backend type-baseline enforcement, 431 frontend unit tests, frontend lint/type checks, typed-router checks, and production build

UAT after merge

The first deploy must explicitly bootstrap the tracked ref for the existing UAT instance:

sudo scripts/server/deploy.sh msm-uat --ref origin/main
sudo scripts/server/instance.sh history msm uat

Then test both choices against a SHA shown in the history:

  1. Run sudo scripts/rollback.sh msm-uat <previous-sha> and verify the migration plan, application health, database checks, and new history entry.
  2. Run sudo scripts/rollback.sh msm-uat <new-release-sha> --restore-backup to restore the safety snapshot paired with the new release, then verify application health and history again.
  3. Finish with sudo scripts/server/deploy.sh msm-uat and confirm the instance still reports origin/main as its tracked ref.

If the selected releases have identical migration leaves, step 1 proves the code-only latest-database path. Select a known earlier reversible migration if an actual reverse migration needs to be exercised in UAT.

Summary by CodeRabbit

  • New Features

    • Deployments now support per-instance Git refs, including mixed-branch deployments and explicit --ref selection.
    • Added deployment history and tracked-ref details to instance status and history commands.
    • Added rollback.sh for rolling back releases while retaining the latest database or restoring a paired backup.
    • Added a migration rollback command that previews or applies safe reverse migrations.
  • Documentation

    • Updated deployment, rollback, release cleanup, and migration guidance, including 14-day release retention.

@coderabbitai

coderabbitai Bot commented Aug 8, 2026

Copy link
Copy Markdown

Review Change Stack

Warning

Review limit reached

@corrin, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 31 minutes

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: ddce0708-dfaa-4ed7-8be0-c5b3efc2d1a6

📥 Commits

Reviewing files that changed from the base of the PR and between 5b41677 and e88dc42.

📒 Files selected for processing (6)
  • scripts/README.md
  • scripts/rollback.sh
  • scripts/server/README.md
  • scripts/server/instance.sh
  • scripts/server/release-utils.sh
  • scripts/test_release_utils.sh
📝 Walkthrough

Walkthrough

The PR adds a Django migration rollback command, a rollback utility with migration and backup-restore modes, per-instance Git ref tracking, deployment history, release retention, and related deployment documentation and tests.

Changes

Deployment rollback and release tracking

Layer / File(s) Summary
Migration rollback command
apps/workflow/management/commands/rollback_migrations.py, apps/workflow/tests/test_rollback_migrations.py
The command parses migration targets, plans reverse migrations, rejects forward or irreversible operations, reports no-op plans, and optionally applies reversible rollbacks.
Deployment state and history
scripts/server/release-utils.sh, scripts/server/instance.sh, scripts/test_release_utils.sh
Deployment state now stores tracked refs and history. Instance status and history commands expose this data. Release completion timestamps and cleanup rules are tested.
Per-instance deployment resolution
scripts/server/deploy.sh, docs/adr/0029-servers-run-the-production-branch.md, docs/updating.md
Deployments resolve refs and release SHAs per instance. Explicit refs and stored refs are handled separately. Deployment state and cleanup behavior are updated.
Rollback execution
scripts/rollback.sh, scripts/README.md, scripts/server/README.md, docs/updating.md
The new rollback utility supports latest-database migration rollback and paired-backup restoration, with validation, locking, service control, safety backups, database swapping, release switching, and rollback state recording.

Estimated code review effort: 5 (Critical) | ~90+ minutes

Sequence Diagram(s)

sequenceDiagram
  participant Operator
  participant rollback.sh
  participant Django
  participant Database
  participant Release
  Operator->>rollback.sh: select target release and rollback mode
  rollback.sh->>Django: plan or apply reverse migrations
  Django->>Database: inspect or update migration state
  rollback.sh->>Database: restore or swap database when requested
  rollback.sh->>Release: switch instance release
  rollback.sh-->>Operator: report rollback result
Loading

Possibly related PRs

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 5.26% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main changes: per-instance ref tracking and safer rollback options.
Description check ✅ Passed The description explains the changes, motivation, Jira work item, operator commands, validation, and UAT steps in sufficient detail.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch agent/safe-release-rollback

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@corrin
corrin marked this pull request as ready for review August 8, 2026 02:09

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 7

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@scripts/README.md`:
- Line 50: Update the preceding predeploy_backup.sh description to state that
its paired database backup supports rollback.sh --restore-backup, replacing the
outdated “switch release, psql restore” wording while leaving the rollback.sh
entry unchanged.

In `@scripts/rollback.sh`:
- Around line 109-113: Update stop_services to remove the || true failure
suppression from all three systemctl stop commands, and verify each service is
inactive before returning so rollback cannot proceed to database changes while
application processes remain active.
- Around line 101-105: Update cleanup() so a failed dropdb for RESTORE_DB
reports the database name to stderr and returns a non-zero status; remove the ||
true suppression while preserving TARGETS_FILE removal and successful cleanup
behavior.
- Around line 90-92: Update the current-SHA guard in the rollback mode handling
so it rejects matching CURRENT_SHA and TARGET_SHA only for latest-db operations,
while allowing --restore-backup to proceed and recover the current release’s
safety backup. Add a regression test covering current-release --restore-backup
success and preserve the existing rejection for latest-db.

In `@scripts/server/instance.sh`:
- Around line 621-622: Move the write_deploy_state call for the "create" action
out of the early setup path and into the final successful-create path in
instance creation. Place it after all required migrations, configuration, and
service operations complete, so deploy-state.env and deploy-history.tsv are
updated only when creation succeeds.

In `@scripts/server/README.md`:
- Around line 134-148: The Step 3 deployment sequence description must reflect
per-instance refs: update the `deploy.sh` workflow text to say it resolves each
target instance’s ref and builds or reuses one release per unique SHA, including
that a bare `--all` deployment may use multiple refs.

In `@scripts/server/release-utils.sh`:
- Around line 77-85: Update the command constructed for sudo in the release
setup flow to enable errexit, nounset, and pipefail with set -euo pipefail
before either source command. Keep the existing virtualenv activation,
environment loading, exports, directory change, and management command execution
unchanged.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 348fbd9b-f6a5-41dd-b16c-5c8f81f5fa03

📥 Commits

Reviewing files that changed from the base of the PR and between f5a89ff and 5b41677.

📒 Files selected for processing (12)
  • apps/workflow/management/commands/rollback_migrations.py
  • apps/workflow/tests/test_rollback_migrations.py
  • docs/adr/0029-servers-run-the-production-branch.md
  • docs/updating.md
  • scripts/README.md
  • scripts/predeploy_rollback.sh
  • scripts/rollback.sh
  • scripts/server/README.md
  • scripts/server/deploy.sh
  • scripts/server/instance.sh
  • scripts/server/release-utils.sh
  • scripts/test_release_utils.sh
💤 Files with no reviewable changes (1)
  • scripts/predeploy_rollback.sh

Comment thread scripts/README.md
Comment thread scripts/rollback.sh Outdated
Comment thread scripts/rollback.sh
Comment thread scripts/rollback.sh Outdated
Comment thread scripts/server/instance.sh Outdated
Comment thread scripts/server/README.md
Comment thread scripts/server/release-utils.sh Outdated
@corrin
corrin merged commit 8358ad0 into main Aug 8, 2026
22 checks passed
@corrin
corrin deleted the agent/safe-release-rollback branch August 8, 2026 02:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant