smartyfy.mrindigi.com

Databases & SQL Master Course — 159. Operational Change Runbooks

Lesson 159 Operational Change Runbooks

Prerequisites and objectives

Complete inventory, retention planning, and transactional schema trials. You will turn a small migration into an operational runbook with prerequisites, exact steps, verification, stop conditions, and a bounded backout plan that does not assume every committed change can be reversed without losing information.

A runbook is a decision path, not just a script

The fictional change adds a queue column with the default general and creates an index for status-and-queue lookups. The application schema marker advances from one to two, with a matching change-history entry. The runbook must explain why the change is needed, where it is authorized, and how success will be recognized.

Before execution, verify the target, engine version, schema version, object names, expected data shape, access authority, recovery artifact, and dependency readiness. Stop on a mismatch rather than adapting the script informally in the middle of maintenance. A command that is correct for the intended lab can still be harmful against the wrong database.

Identify the person or process responsible for the change and its acceptance. Record when to pause, who can decide to continue, and how to keep the service from sending incompatible traffic during the transition. A runbook that requires an unavailable decision-maker is not operationally complete.

PRAGMA user_version=1;
CREATE TABLE lab_job159(id INTEGER PRIMARY KEY,status TEXT NOT NULL,priority INTEGER NOT NULL);
INSERT INTO lab_job159 VALUES(1,'ready',1),(2,'done',1);
CREATE TABLE lab_change159(version INTEGER PRIMARY KEY,description TEXT NOT NULL);
INSERT INTO lab_change159 VALUES(1,'Initial jobs');
Runbook checkpoints
Stage Required evidence Stop condition
Preflight Version 1 and expected objects/data Wrong target, version, or object collision
Rehearsal New columns and query results; rollback verified Unexpected result or unrecoverable state
Execution Atomic migration accepted Statement or business verification failure
Acceptance Version 2, history, schema, and behavior agree Any required check fails or remains unexplained

Worked practice

Exercise 1

Run preflight checks against the disposable fixture.

Worked solution
-- Connection A
SELECT sqlite_version();
-- Connection A
PRAGMA user_version;
-- Connection A
SELECT id,status,priority FROM lab_job159 ORDER BY id;
-- Connection A
SELECT COUNT(*) FROM sqlite_schema WHERE name='lab_job159_status_queue';

The expected version marker is one, the two rows have known values, and the planned index name is unused. These checks are gates, not merely informational output. An existing index with the same name could have a different definition, so silently adding IF NOT EXISTS would hide a potentially important mismatch.

Exercise 2

Rehearse the change and its verification, then abandon the trial.

Worked solution
-- Connection A
BEGIN;
-- Connection A
ALTER TABLE lab_job159 ADD COLUMN queue TEXT NOT NULL DEFAULT 'general';
-- Connection A
CREATE INDEX lab_job159_status_queue ON lab_job159(status,queue);
-- Connection A
INSERT INTO lab_change159 VALUES(2,'Add queue and lookup index');
-- Connection A
PRAGMA user_version=2;
-- Connection A
SELECT id,queue FROM lab_job159 ORDER BY id;
-- Connection A
SELECT id FROM lab_job159 WHERE status='ready' AND queue='general' ORDER BY id;
-- Connection A
ROLLBACK;
-- Connection A
PRAGMA user_version;
-- Connection A
SELECT name FROM pragma_table_info('lab_job159') ORDER BY cid;

The new default and representative lookup behave as intended during the transaction. Rollback restores the original columns and marker. This is an executed SQLite rehearsal, not a claim that MySQL or every other engine rolls back the same DDL. Engine-specific transaction behavior belongs in the runbook prerequisites.

Exercise 3

Trigger a known invalid uniqueness assumption and apply a stop condition.

Worked solution
-- Connection A
BEGIN;
-- Connection A
CREATE UNIQUE INDEX lab_job159_bad ON lab_job159(priority);
-- Connection A
ROLLBACK;
-- Connection A
SELECT COUNT(*) FROM sqlite_schema WHERE name='lab_job159_bad';
-- Connection A
SELECT COUNT(*) FROM lab_job159;

Both jobs have priority one, so a unique index on priority fails. The correct response is to stop and review the proposed requirement, not delete a row to make the migration pass. Explicit rollback leaves no failed index and preserves both jobs. Record the structured error and the violated assumption.

Exercise 4

Accept the supported migration and demonstrate its narrowly justified backout.

Worked solution
-- Connection A
BEGIN;
-- Connection A
ALTER TABLE lab_job159 ADD COLUMN queue TEXT NOT NULL DEFAULT 'general';
-- Connection A
CREATE INDEX lab_job159_status_queue ON lab_job159(status,queue);
-- Connection A
INSERT INTO lab_change159 VALUES(2,'Add queue and lookup index');
-- Connection A
PRAGMA user_version=2;
-- Connection A
COMMIT;
-- Connection A
PRAGMA user_version;
-- Connection A
SELECT id,queue FROM lab_job159 ORDER BY id;
-- Connection A
BEGIN;
-- Connection A
DROP INDEX lab_job159_status_queue;
-- Connection A
ALTER TABLE lab_job159 DROP COLUMN queue;
-- Connection A
DELETE FROM lab_change159 WHERE version=2;
-- Connection A
PRAGMA user_version=1;
-- Connection A
COMMIT;
-- Connection A
SELECT id,status,priority FROM lab_job159 ORDER BY id;

This backout is valid only in the stated isolated fixture: no new writes or consumers used queue, every value was the known default, and the only added dependency was the index that is removed first. SQLite DROP COLUMN requires version 3.35.0 or later and has dependency restrictions. Do not generalize this trial into permission to discard meaningful new values after a live rollout.

Distinguish rollback, backout, and recovery

Rollback abandons an uncommitted transaction. Backout is a new change intended to reverse an accepted change under stated assumptions. Recovery restores from an earlier artifact and may require reconciling later accepted work. These are different operations with different data-loss and availability consequences.

A backup taken before migration is useful evidence, but restoring it after new business writes can lose those writes. A runbook must define the point after which a simple backout is no longer safe and a forward repair or reconciled recovery is required. Do not describe every migration as reversible merely because an old file exists.

Make execution observable and bounded

Record exact artifact identifiers, start and end times, target identity, actual engine version, preflight outcomes, affected objects, and verification results. Avoid collecting unnecessary row contents or credentials. A concise execution record should let another operator understand what was attempted and what remains uncertain.

Set an operational window and stop conditions for unexpected blocking, resource pressure, incompatible application behavior, or failed checks. The numerical thresholds depend on the real service and are not invented by this tiny lab. Decide how to communicate a pause before starting rather than improvising when the system is already unhealthy.

Run the same reviewed artifact in rehearsal and execution where practical, with explicit environment parameters rather than manual edits that change its meaning. If the artifact changes after rehearsal, review and rerun the relevant tests. A checksum links evidence to exact content; it does not replace human understanding of the change.

Pitfalls and summary

Do not bypass preflight mismatches with convenience clauses, repair unexpected data by deletion, or confuse uncommitted rollback with post-commit recovery. A useful runbook names its target, assumptions, exact steps, acceptance checks, stop decisions, and the conditions under which its recovery path remains safe.

Knowledge check

Question 1

Why should an unexpected existing index name stop preflight?

  1. Any index with that name must belong to the intended migration version
  2. IF NOT EXISTS proves the existing definition matches
  3. The existing object may differ from the reviewed intended definition
  4. Index names do not matter to migrations
Answer and explanation

C. A name collision can hide drift that a convenience clause would conceal.

Question 2

What should happen when the unique-priority trial fails?

  1. Delete one job automatically and rerun the migration
  2. Ignore the error and advance the schema marker
  3. Assume the failed index now enforces uniqueness
  4. Stop and review the invalid uniqueness assumption without deleting jobs
Answer and explanation

D. A failed precondition is evidence to investigate, not permission to alter business data.

Question 3

What distinguishes backout from rollback?

  1. Backout is a new change reversing an already accepted change under stated assumptions
  2. Backout can always reuse the original rollback even after commit
  3. Backout requires no data-loss analysis if the schema marker is restored
  4. They are identical regardless of commit state
Answer and explanation

A. Commit status and recovery assumptions determine the appropriate mechanism.

Question 4

Why is the demonstrated queue-column backout narrowly safe?

  1. The original default proves no application has stored a different value since rollout
  2. No new writes or consumers used it and all values remained the known default
  3. A prior backup guarantees no later work can be lost
  4. Matching the original column count proves the removed data was unnecessary
Answer and explanation

B. The fixture deliberately satisfies assumptions that may not hold after a live rollout.

Question 5

What does a migration artifact checksum establish?

  1. Which exact artifact the execution evidence refers to
  2. That the migration is correct for every target
  3. That all required approvals exist
  4. That every recovery objective has been measured
Answer and explanation

A. Artifact identity supports review and reproducibility without proving correctness by itself.

Execution scope

These scenarios were executed with SQLite 3.53.1 using separate connections to newly created disposable local database files. No existing or external database was used. Each scenario starts from a fresh fixture. Python was an internal verification harness, not a learner prerequisite. Follow the labeled connection order in SQL clients that open the same disposable file.

Primary references