Fix the gift→guest rename migration for legacy databases
Two defects found by booting the renamed app against a fixture of the
pre-rename database (the live portal's exact state):
1. members.gift_expires_at: the RENAME above uses the stale
snapshot, so the plain
below fired right after a successful rename and crashed boot with
'duplicate column name' — the portal would have failed its deploy
health check on the live database. Make the two branches exclusive.
2. The ledger's own expiry column: ALTER TABLE guests RENAME moved the
table but kept its legacy gift_expires_at column, which the provision
INSERT (writing guest_expires_at) would fail on at first use. Rename
the column with the table.
Also moves the credit-ledger dedupe references ('gift:<email>' /
'gift-return:<email>') to their guest prefixes at boot — returns first,
since 'gift-return:…' also matches 'gift:%' — so a re-grant after the
rename cannot double-credit.
Verified: migration harness 10/10 on a legacy-state fixture (renames,
value conversion, ref migration, idempotent second boot, readback
helpers); full portal suite 9/9 files green.
This commit is contained in:
1 parent
27f9cc06b2
commit
93184b6011
1 file changed
+19
-1
+19
-1
@@ -939,13 +939,31 @@ def get_db() -> sqlite3.Connection:
|
||||
conn.execute("ALTER TABLE gifts RENAME TO guests")
|
||||
if "gift_lapse_notices" in _tables and "guest_lapse_notices" not in _tables:
|
||||
conn.execute("ALTER TABLE gift_lapse_notices RENAME TO guest_lapse_notices")
|
||||
# The ledger's own expiry column moves with its table — the provision
|
||||
# INSERT writes guest_expires_at, so a legacy table keeping the old
|
||||
# column name would fail on the first provision after the rename.
|
||||
_gcols = [r[1] for r in conn.execute("PRAGMA table_info(guests)").fetchall()]
|
||||
if "gift_expires_at" in _gcols and "guest_expires_at" not in _gcols:
|
||||
conn.execute("ALTER TABLE guests RENAME COLUMN gift_expires_at TO guest_expires_at")
|
||||
if "gift_expires_at" in cols and "guest_expires_at" not in cols:
|
||||
conn.execute("ALTER TABLE members RENAME COLUMN gift_expires_at TO guest_expires_at")
|
||||
conn.execute("UPDATE members SET account_type = 'guest' WHERE account_type = 'gift'")
|
||||
# Credit-ledger dedupe references move with the rename (a re-grant that
|
||||
# misses the old prefix could double-credit). Returns first: a
|
||||
# 'gift-return:…' value also matches 'gift:%'. Only when the table
|
||||
# already exists — a first-boot database creates it below, empty.
|
||||
if "credit_ledger" in _tables:
|
||||
conn.execute("UPDATE credit_ledger SET reference = 'guest-return:' || substr(reference, 13) "
|
||||
"WHERE reference LIKE 'gift-return:%'")
|
||||
conn.execute("UPDATE credit_ledger SET reference = 'guest:' || substr(reference, 6) "
|
||||
"WHERE reference LIKE 'gift:%'")
|
||||
# Migration: guest_expires_at — when a guest account's GUEST_DAYS time box
|
||||
# elapses (set at guest provisioning). The sweep deactivates the account
|
||||
# after this instant even if credits remain.
|
||||
if "guest_expires_at" not in cols:
|
||||
# NOTE: this must be an EXCLUSIVE branch against the RENAME above — `cols`
|
||||
# is a snapshot taken before the rename, so a plain `if not in cols` here
|
||||
# would add a duplicate column on any database the rename just migrated.
|
||||
if "gift_expires_at" not in cols and "guest_expires_at" not in cols:
|
||||
conn.execute("ALTER TABLE members ADD COLUMN guest_expires_at TEXT")
|
||||
# Guests ledger — the abuse-protection audit trail: WHO invited WHOM, when,
|
||||
# and what became of the guest. One row per guest ever sent.
|
||||
|
||||
Reference in new issue
Block a user