# Schema migration: what does ROLLBACK restore?

After an error, read the schema, version and records. The message alone does not establish what was undone.

L00P.AI · 1.0 · 2026-11-19

https://www.l00p.ai/en/resources/series/migracja-schematu-co-przywraca-rollback/

## 01 / Check the state after the error

The team changes a database structure. Several statements succeed; the last one reports an error. Can the old application still read the data? A message saying “migration failed” does not answer that question. You need to read the schema, version and records after error handling has finished.

The [batch import trial](/en/resources/series/import-partii-konflikt-nie-cofa-wczesniejszych-zapisow/) examined record commit boundaries. Here the change also includes the table structure and an application version number. This original local trial used fictional data on 19 November 2026, with Python 3.11.11 and SQLite 3.47.1. No production database was changed.

## 02 / Define the contract first

Version 1 has a docs table with id and title columns. Record A contains Alpha; record B contains Beta. The version 2 contract, saved before execution, requires caption instead of title, a new status column, and preservation of both identifiers and texts. A receives approved; B retains the default draft. These are original control values, not archive documents.

The guarded variant starts BEGIN IMMEDIATE. Separate execute calls then rename the column, add status with NOT NULL and default draft, update A and set PRAGMA user_version=2. Success leads to COMMIT. In the failure variant, only after all these changes do we attempt to add another record with the existing ID A. The expected outcome is a PRIMARY KEY error followed by explicit ROLLBACK.

According to the [user_version documentation](https://www.sqlite.org/pragma.html#pragma_user_version), this integer is available for application use; SQLite itself does not use it to assess the structure. A value of 2 therefore does not prove migration completion. A separate control changed only this number while leaving the old schema and records. The full contract check rejected it.

## 03 / Three paths, observed outcomes

![Success: committed version 2. Late error and explicit ROLLBACK: v1 schema, version and records restored. Without a shared transaction v2 remains; a version label alone is insufficient.](https://www.l00p.ai/wydawnictwo/migracja-schematu-co-przywraca-rollback/migracja-en-v1.svg)

```text
v1: id, title / A=Alpha / B=Beta / user_version=1
  BEGIN -> schema and data changes -> user_version=2
    +-> COMMIT: caption, status / approved, draft / v2
    +-> PRIMARY KEY error -> ROLLBACK: exactly v1
  no shared transaction -> same error: v2 remains
```

After successful commit, closing the connection and reopening read-only returned the expected version 2. After the late error in the guarded variant, the changes were still visible within the active transaction. Explicit rollback then restored version 1. After reopening, we compared the complete sqlite_schema entries, column information, version number and ordered records with the saved initial state. All of these matched.

The negative variant without a shared BEGIN used isolation_level=None. Earlier statements persisted their changes before the same duplicate error. No transaction remained active to roll back; version 2 remained after reopening. The exception alone did not reverse previous statements.

## 04 / What reversible means here

There were 34 passing checks in one trial covering four databases: success, rollback, no shared transaction and version-number-only change. The integrity check returned ok for all four. The two deliberately unsuitable variants show why file integrity alone cannot establish acceptance of a migration contract.

ROLLBACK here reverses an open transaction. We did not prepare a down migration after COMMIT or a backup restoration procedure. There was no power cut, process termination, WAL test, concurrent writing, performance measurement, foreign-key migration, view or trigger trial. Acceptance of this simple example does not establish reversibility of arbitrary schema changes or effects in external services.

## 05 / Migration acceptance card

```text
Before: schema, columns, records, user_version
After success: expected v2 contract after reopening
Control error: after schema, data and version changes
Handling: explicit ROLLBACK of the active transaction
After error: compare every element with the v1 contract
Separately: backup, down migration, crash, external effects
```

Before deployment, record the state you accept after success and after failure. Introduce a late error in an isolated demonstration copy and retain the reread result. Calling an operation “rollback” does not establish what was actually reversed. Can your team distinguish undoing uncommitted statements from reversing a completed release?

## 06 / Sources, authorship and correction

The result rests on an original pre-execution protocol, script, four databases and a report of 34 checks. Current official documentation for [SQLite transactions](https://www.sqlite.org/lang_transaction.html), [ALTER TABLE](https://www.sqlite.org/lang_altertable.html) and [Python 3.11 transaction control](https://docs.python.org/3.11/library/sqlite3.html#transaction-control) explains the mechanisms. Current documentation may describe capabilities newer than the installed library; the result applies to the runtime version stated above. We did not use executescript; Python documentation warns that it can commit a pending transaction first.

Codex created the fictional data, code, PL/EN text and original static SVG and ASCII diagram, performing the agent roles of editorial, management and control. No independent second-model review was received. No third-party records or media are reproduced. A new schema, different library or transaction handling, or a mismatch after reopening triggers renewed acceptance. Lech retains the option to withdraw the publication.
