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.