Fixing deployed changes
Rework a change that needs to do something new; repair one whose scripts were edited harmlessly.
A deployed change is immutable: if its scripts change, even by one character, its hash no longer matches the database and deploy stops. That protects every database from running something other than what the others ran. There are two ways forward, depending on what the edit is.
| The edit | Use | Runs SQL? |
|---|---|---|
| The change must do something different (a new procedure body, another column) | bsm rework: a new change that supersedes it | Yes, as a normal deploy |
A bug in revert.sql or verify.sql, a comment, formatting, a new !notx flag | bsm repair: accept the edit in the journal | No |
Rework
bsm rework get-customer # adds get-customer-v2
bsm rework get-customer --as get-customer-by-email -n "search by email"
Rework adds a change named <change>-v2 (then -v3, and so on) that requires the original:
deploy.sqlstarts as a copy of the original deploy script. Edit it.revert.sqlis the original deploy script, so reverting the rework restores the previous version.verify.sqlis copied when the original has one;!notxcarries over.
Rework fits definitions that can be run again: CREATE OR ALTER procedures, views and functions kept in changes, permissions, and idempotent data fixes. For a table change, edit both scripts: an ALTER TABLE to deploy and its opposite to revert.
-- Revert get-customer-v2: restore what get-customer deployed.
-- Copied from changes/get-customer/deploy.sql; edit it if that script cannot simply be run again.
CREATE OR ALTER PROCEDURE dbo.GetCustomer @Id int AS
SELECT Id, Name FROM dbo.Customer WHERE Id = @Id;
Already edited the original by mistake? Move your edits into the new change and restore the original (git checkout -- changes/get-customer), so it matches what was deployed.
Repair
bsm status -e test # ! create-order EDITED AFTER DEPLOY
bsm repair create-order -e test --reason "revert dropped the wrong index"
bsm repair --all --dry-run -e test # list every edited change and what differs
Repair records the working copy's scripts, hashes and flags for the named changes and recomputes the chain ids of every change deployed after them, so the database certifies against the plan again. It runs no SQL. Each repaired change gets a repair event in the log with what differed, the old hash and your reason.
- Repair is per database: run it in every environment that has the change.
- A fixed
revert.sqlis what revert will run from then on, because revert uses the scripts stored in the journal. - When
deploy.sqlitself differs, bsm warns and always asks for confirmation (type the environment name): databases that already ran the old script keep the old result. Only repair deploy edits that change nothing, and put real changes in a rework. - Repair takes the deploy lock and respects the environment ownership guard.