Skip to content

feat(storage): add versioned schema migrations - #2416

Merged
mbrandenburger merged 4 commits into
hyperledger-labs:mainfrom
mbrandenburger:feat/2411-schema-migrations
Nov 1, 2026
Merged

mbrandenburger merged 4 commits into
hyperledger-labs:mainfrom
mbrandenburger:feat/2411-schema-migrations

Conversation

@mbrandenburger

@mbrandenburger mbrandenburger commented Oct 30, 2026 •

Copy link
Copy Markdown
Member

Fixes #2411.

Problem

FSC stores record no schema version. A database written by a newer release opens without notice, schema changes are probes inside CreateSchema, and skipCreateTable: true skips those probes silently. A SQLite test on main confirmed all three: no version table exists after open, a table with an extra NOT NULL column opens fine, and a (pkey, ns) KVS key stays unconverted under skipCreateTable.

Changes

migrate package (platform/view/services/storage/driver/sql/common/migrate)

  • Step, Versioned (StoreName, Steps), Migrate, WithCheckOnly, ErrSchemaTooNew, ErrPending.
  • One <prefix>_schema_versions (store, version, readable_by) table per prefix; a mixed-case prefix is lowercased.
  • Migrate reads the row without a lock and returns when nothing is pending. readable_by above the binary's latest step returns ErrSchemaTooNew; a corrupt row returns an error.
  • Postgres: a dedicated connection holds a session advisory lock keyed by the versions table. The waiter logs every 10s and has no timeout.
  • SQLite: each step runs in BEGIN IMMEDIATE on the write connection.
  • Each step is one transaction that re-reads the row, skips a step another migrator applied, and records the new version. A failed step rolls back and names the store and the step.
  • A different Go type claiming the same store row in the same database is rejected.
  • Step.Go func(ctx, migrate.Execer) error runs after the step's SQL in the same transaction, for changes SQL cannot express (a backfill computed in Go, a SQLite table rebuild). An error rolls the whole step back. Execer is the ExecContext/QueryContext/QueryRowContext subset shared by *sql.Tx, *sql.Conn and *sql.DB. It is not a *sql.Tx because SQLite steps run in BEGIN IMMEDIATE on a raw *sql.Conn, and modernc sets the begin mode only per pool (_txlock).

Constructors

  • All six constructors (sqlite, postgres, memory for view and fabric) call migrate.CreateSchema: Migrate for a Versioned store, CreateSchema() otherwise.
  • migrate.CreateSchema takes opts ...Option. With WithCheckOnly() nothing is created or migrated: a Versioned store returns ErrPending when steps are pending, and a plain store's CreateSchema() is not called.
  • Under skipCreateTable: true the four SQL constructors pass WithCheckOnly() instead of skipping the call, so a Versioned store with pending steps fails to open; the memory drivers have no skipCreateTable. Applications with their own constructors do the same.

Docs

  • db-driver.md: how constructors choose, locking, adopting Versioned in an application store, Go steps, WithCheckOnly for skipCreateTable, and tables shared by several stores (one owning store lists the steps).
  • database-layout.md: the versions table. docs/agents/conventions.md: "Schema migrations", including that a released step's Go function never changes.

Deviations from the issue

  • InProgress() moves to Version every FSC store and add the migrate command #2412, next to its only caller (/readyz).
  • Postgres polls pg_try_advisory_lock instead of blocking on pg_advisory_lock. The server never waits, so there is no lock_timeout to reset.
  • Each step has its own transaction on SQLite too, not one BEGIN IMMEDIATE for all steps. Steps applied before a failure stay applied, like on Postgres.
  • The duplicate check is keyed by database, versions table and store name, and only rejects a different type: FSC reopens the same store type on one table in-process, and a sqlite and a postgres vault with the same table name are different databases.
  • The hook is in the memory drivers as well, since Version every FSC store and add the migrate command #2412 versions memory stores too.
  • Across processes on one SQLite file the wait is bounded by busy_timeout; the store then fails to open.

Compatibility

Not breaking. DBObject and InitSchema are unchanged. Panurus builds its stores with its own constructors that call CreateSchema() directly and does not use FSC's NewPersistenceWithOpts, so it is unaffected; a test checks that a constructor generic over DBObject still compiles and runs.

Tests

  • TestMigrateSQLite and TestPostgresMigrate run one suite on both backends: order and once, failed step leaves the row and the pool usable, four concurrent migrators apply each step once (including creating the versions table), ErrSchemaTooNew, WithCheckOnly, duplicates, mixed-case prefix, step numbering.
  • TestPostgresMigrateWaiter: a waiting migrator logs and proceeds after the lock is released; nothing pending opens while another session holds the lock.
  • GoStep: a failing Go step rolls back the step's SQL too and keeps the version; a working one backfills and commits. CreateSchemaCheckOnly: ErrPending for a pending Versioned store, no CreateSchema() call for a plain store. Both run on SQLite and Postgres.
  • TestNewPersistenceWithOptsSchema: plain store gets CreateSchema, Versioned store is migrated, skipCreateTable skips CreateSchema and refuses a Versioned store with pending steps (ErrPending, nothing created), then opens it once migrated.
  • Mutations (no lock, no re-read, no too-new check, no duplicate check, plain BEGIN on SQLite, Go step not called or its error ignored, check-only ignored for a plain store, constructor not passing WithCheckOnly under skipCreateTable) each fail a test.
  • make checks, make unit-tests-root and the storage Postgres tests pass.

@github-actions

github-actions Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

Hey @mbrandenburger 👋 thanks for the PR!
I'm your friendly PR Helper Bot 🤖 and I'll be riding shotgun on this one, keeping track of your PR's status to help you get it approved and merged.

This comment updates automatically as you push changes -- think of it as your PR's live scoreboard!
Here's the latest:


PR Checks

✅ DCO Sign-off -- All commits have valid sign-offs. Nice work!


❌ GPG Signature -- Heads up! The following commits don't have a verified GPG signature:

  • 4c612f8 feat(storage): add versioned schema migrations
  • 1426c85 fixup! feat(storage): add versioned schema migrations
  • 55ca090 fixup! feat(storage): add versioned schema migrations
  • 0cfd149 fixup! feat(storage): add versioned schema migrations

You'll need to sign your commits with GPG (e.g. git commit -S). See the Signing Guide for a step-by-step walkthrough.


✅ Merge Conflicts -- No merge conflicts detected. Smooth sailing!


✅ Issue Link -- Linked to #2411 (assigned to you).


⏳ All checks must pass before this PR can be reviewed. You've got this!

@github-actions github-actions Bot added the status: needs revision The pull request requires changes from the author before it can be reviewed or merged label Oct 30, 2026
@mbrandenburger mbrandenburger added this to the 26Q4 milestone Oct 30, 2026
@mergify

mergify Bot commented Nov 1, 2026 •

Copy link
Copy Markdown

This pull request does not currently match the merge queue conditions, so it cannot be queued from here. The box comes back if it matches again.

@mbrandenburger
mbrandenburger enabled auto-merge (squash) November 1, 2026 09:21
The new package platform/view/services/storage/driver/sql/common/migrate records a schema version per SQL store and runs ordered SQL steps. A store opts in by implementing migrate.Versioned (StoreName and Steps). Each table prefix gets one <prefix>_schema_versions table with a row per store holding version and readable_by. No FSC store implements Versioned yet, so nothing changes at runtime.

Migrate reads the store's row without a lock and returns if nothing is pending, so an up-to-date store opens without waiting. A readable_by above the binary's latest step returns ErrSchemaTooNew, and WithCheckOnly returns ErrPending instead of running steps. When steps are pending:
- Postgres migrators of one prefix exclude each other with a session advisory lock on a dedicated connection. The waiter polls pg_try_advisory_lock, logs every 10s and has no timeout, so lock_timeout does not apply.
- SQLite runs each step in BEGIN IMMEDIATE on the write connection; across processes the wait is bounded by busy_timeout.
Each step runs in its own transaction, re-reads the row, skips a step another migrator applied meanwhile, and records the new version. A failed step rolls back and names the store and the step. A different Go type claiming the same store row in the same database is rejected.

The six driver constructors (sqlite, postgres and memory, for view and fabric stores) call migrate.CreateSchema, which runs Migrate for a Versioned store and CreateSchema otherwise. skipCreateTable still skips both. DBObject and InitSchema are unchanged, so applications such as Panurus that build stores with their own constructors keep compiling and behave as before.

docs/platform/view/services/db-driver.md describes how constructors choose, the locking and how an application store adopts Versioned; database-layout.md documents the versions table; docs/agents/conventions.md adds the rules for schema migrations.

Fixes hyperledger-labs#2411

Signed-off-by: Marcus Brandenburger <bur@zurich.ibm.com>
Signed-off-by: Marcus Brandenburger <bur@zurich.ibm.com>
Signed-off-by: Marcus Brandenburger <bur@zurich.ibm.com>
Signed-off-by: Marcus Brandenburger <bur@zurich.ibm.com>
@mbrandenburger
mbrandenburger force-pushed the feat/2411-schema-migrations branch from cf933fc to 0cfd149 Compare November 1, 2026 09:21
@mbrandenburger
mbrandenburger merged commit 8d2662c into hyperledger-labs:main Nov 1, 2026
@mbrandenburger
mbrandenburger deleted the feat/2411-schema-migrations branch November 1, 2026 09:21

@SaidAltury-ibm SaidAltury-ibm left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thank you @mbrandenburger, i have left a comment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature status: needs revision The pull request requires changes from the author before it can be reviewed or merged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add versioned schema migrations

2 participants