Repository navigation
fix(migrations): make per-database upgrades safe to run concurrently - #532
Conversation
Deployment databases on one server share only cluster-global roles. - The shared rememberstack_view_owner role is now created tolerating a concurrent creator (duplicate_object/unique_violation swallowed), so first migrations of several databases at once no longer fail. - Migrations refuse a database name longer than 43 bytes: the per-database roles rememberstack_query_/graph_<database> would be truncated at 63 bytes, and two databases could share a role and its grants. - The upgrades page documents how an operator loops setup over databases, and the three shared things (name limit, shared role, pg_partman_bgw.dbname); packaging design §6 records the rule. - Tests: a concurrent shared-role creator, two databases migrated by two processes at once, and a too-long name refused. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01F9cMsQBqiW2qU2ow4zeNCm
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01F9cMsQBqiW2qU2ow4zeNCm
Codex round 1: role names are unquoted, so PostgreSQL lowercases them; two databases differing only in case shared one query role. Database names must be 1-43 characters of [a-z0-9_], not starting with a digit. The operator loop now reports a failed run, and the docs say first-time shared-role creation can make one upgrade briefly wait for another. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01F9cMsQBqiW2qU2ow4zeNCm
Codex review round 1 (gpt-6.1-sol, max effort)VERDICT: REQUEST_CHANGES
Otherwise, the nested exception handler is correct, the rollback before Alembic configuration correctly restores transaction ownership, and 43 = 63 − 20 fits both role prefixes. I found no additional shared-state races in the migrations or setup path; graph memberships, password provisioning, and existing bucket guards are adequate with distinct role names. The image entrypoint accepts The three new tests and 36 related tests passed. Additional checks confirmed a 43-byte multibyte name reaches head and a 44-byte name fails before schema changes. The deterministic race test is useful; the missing case-pair test matters. The isolated PostgreSQL container was removed, and the worktree remains unchanged. Disposition (Claude): all three accepted
|
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01F9cMsQBqiW2qU2ow4zeNCm
Codex review round 2 (gpt-6.1-sol, max effort)VERDICT: APPROVE
No blocker or major findings. All three round-1 findings are fixed; none were declined. I found no additional unsafe shared state or races across databases in the migration chain or The nested exception handler preserves the surrounding transaction. The preflight rollback correctly restores Alembic’s transaction ownership. With names restricted to ASCII, 43 = 63 − 20 fits both role prefixes exactly. Password provisioning targets distinct roles, graph memberships remain separate, and bucket creation already handles concurrent creators. The Docker command matches the image entrypoint. The loop checks every PID and reports failures; the shared-role wait description is accurate. The comma-separated The new tests meaningfully cover lock contention, concurrent upgrades, and rejection before schema changes. 56 tests passed, plus manual boundary, transaction, and shell-loop checks. Lint, formatting, types, and documentation checks passed. The isolated PG 19 Beta 3 container was removed. No files were modified. Disposition (Claude)The minor finding is fixed in the latest commit, a docs-only wording change ("its own roles"). |
UMC relaunch plan phase 2 item (h), "Upgrades per database, in parallel, no machinery." Migrations must be safe to run against each Project database independently and concurrently. An operator loops over databases, and no rollout runner is built here (that is a deferred ticket).
Audit: what a migration or
setupshares across databasesrememberstack_query_<db>(p9_02_0023) andrememberstack_graph_<db>(p9_17_0038) are per database, so nothing races. They are, however, subject to 63-byte truncation (see fix 2).rememberstack_view_owner(p9_02_0023) is shared. ItsIF NOT EXISTS … CREATE ROLEraced: two first migrations at once both see it missing, and the second fails withunique_violationonpg_authid. I reproduced this in the new test.pg_advisory_xact_lock) are database-scoped, so they need no change.pg_partmanare per database. Thepg_partman_bgw.dbnameserver setting must list every database, so that is documented.setup:ALTER ROLE … PASSWORDtargets the per-database query role.ensure_bucketalready toleratesBucketAlreadyOwnedByYou.Fixes
create_role_if_absent_sqlwrapsCREATE ROLEinEXCEPTION WHEN duplicate_object OR unique_violation THEN NULL. Editing the old migration body is safe: databases that already ran it are unaffected, and new ones get the safe form. Alembic runs a whole upgrade in one transaction, so a concurrent first migration waits for the first one's commit and then proceeds.migrations/env.pyrefuses a database name longer than 43 bytes (63 −len("rememberstack_query_")). PostgreSQL truncates role names at 63 bytes, so two long database names with the same prefix would share one query role. That role would then getCONNECTand view grants on both databases, which breaks isolation.Docs
docker run --env-file … setup &per deployment, thenwait) and the three shared things: the name limit, the shared role, andpg_partman_bgw.dbname.llms-full.txtis regenerated.Tests (
src/tests/spine/test_per_database_migrations_pg.py, PG 19 Beta 3)UniqueViolation).alembic upgrade headprocesses at once, both reach head and get distinct roles.For the UMC runtime: Project database names must be at most 43 bytes.
Checks
🤖 Generated with Claude Code
https://claude.ai/code/session_01F9cMsQBqiW2qU2ow4zeNCm