From 93184b601126e7477f114ed4eeff43b2f187d878 Mon Sep 17 00:00:00 2001 From: Inferencebot Date: Sun, 8 Nov 2026 02:42:53 +0000 Subject: [PATCH] =?UTF-8?q?Fix=20the=20gift=E2=86=92guest=20rename=20migra?= =?UTF-8?q?tion=20for=20legacy=20databases?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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:' / 'gift-return:') 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. --- app/main.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/app/main.py b/app/main.py index 2f87a91..6abbe8c 100644 --- a/app/main.py +++ b/app/main.py @@ -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.