Repository navigation
feat(storage): add versioned schema migrations - #2416
mbrandenburger merged 4 commits into
Conversation
|
Hey @mbrandenburger 👋 thanks for the PR! This comment updates automatically as you push changes -- think of it as your PR's live scoreboard! 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:
You'll need to sign your commits with GPG (e.g. ✅ 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! |
|
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. |
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>
cf933fc to
0cfd149
Compare
SaidAltury-ibm
left a comment
There was a problem hiding this comment.
Thank you @mbrandenburger, i have left a comment
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, andskipCreateTable: trueskips those probes silently. A SQLite test on main confirmed all three: no version table exists after open, a table with an extraNOT NULLcolumn opens fine, and a(pkey, ns)KVS key stays unconverted underskipCreateTable.Changes
migratepackage (platform/view/services/storage/driver/sql/common/migrate)Step,Versioned(StoreName,Steps),Migrate,WithCheckOnly,ErrSchemaTooNew,ErrPending.<prefix>_schema_versions (store, version, readable_by)table per prefix; a mixed-case prefix is lowercased.Migratereads the row without a lock and returns when nothing is pending.readable_byabove the binary's latest step returnsErrSchemaTooNew; a corrupt row returns an error.BEGIN IMMEDIATEon the write connection.Step.Go func(ctx, migrate.Execer) errorruns 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.Execeris theExecContext/QueryContext/QueryRowContextsubset shared by*sql.Tx,*sql.Connand*sql.DB. It is not a*sql.Txbecause SQLite steps run inBEGIN IMMEDIATEon a raw*sql.Conn, and modernc sets the begin mode only per pool (_txlock).Constructors
migrate.CreateSchema:Migratefor aVersionedstore,CreateSchema()otherwise.migrate.CreateSchematakesopts ...Option. WithWithCheckOnly()nothing is created or migrated: aVersionedstore returnsErrPendingwhen steps are pending, and a plain store'sCreateSchema()is not called.skipCreateTable: truethe four SQL constructors passWithCheckOnly()instead of skipping the call, so aVersionedstore with pending steps fails to open; the memory drivers have noskipCreateTable. Applications with their own constructors do the same.Docs
db-driver.md: how constructors choose, locking, adoptingVersionedin an application store, Go steps,WithCheckOnlyforskipCreateTable, 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 themigratecommand #2412, next to its only caller (/readyz).pg_try_advisory_lockinstead of blocking onpg_advisory_lock. The server never waits, so there is nolock_timeoutto reset.BEGIN IMMEDIATEfor all steps. Steps applied before a failure stay applied, like on Postgres.migratecommand #2412 versions memory stores too.busy_timeout; the store then fails to open.Compatibility
Not breaking.
DBObjectandInitSchemaare unchanged. Panurus builds its stores with its own constructors that callCreateSchema()directly and does not use FSC'sNewPersistenceWithOpts, so it is unaffected; a test checks that a constructor generic overDBObjectstill compiles and runs.Tests
TestMigrateSQLiteandTestPostgresMigraterun 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:ErrPendingfor a pendingVersionedstore, noCreateSchema()call for a plain store. Both run on SQLite and Postgres.TestNewPersistenceWithOptsSchema: plain store getsCreateSchema,Versionedstore is migrated,skipCreateTableskipsCreateSchemaand refuses aVersionedstore with pending steps (ErrPending, nothing created), then opens it once migrated.BEGINon SQLite, Go step not called or its error ignored, check-only ignored for a plain store, constructor not passingWithCheckOnlyunderskipCreateTable) each fail a test.make checks,make unit-tests-rootand the storage Postgres tests pass.