"""ยง22.13 step 1 โ€” the bootstrap-id re-stamp: 'default' โ†’ the configured DEFAULT_PROJECT_ID across every project-scoped table, with the composite FKs kept intact and the stale 'default' projects row dropped. Idempotent.""" from __future__ import annotations import tempfile from pathlib import Path import app.db as db from app import projects class _Cfg: def __init__(self, path, default_id): self.database_path = path self.default_project_id = default_id def _setup(monkeypatch, default_id="ohm"): path = str(Path(tempfile.mkdtemp()) / "t.db") cfg = _Cfg(path, default_id) db.run_migrations(cfg) monkeypatch.setattr(db, "_CONN", db.connect(path)) conn = db.conn() # M1 bootstrap row + a registry-mirrored 'ohm' row coexist pre-restamp. conn.execute("INSERT OR IGNORE INTO projects (id,name,type,content_repo,visibility,initial_state) " "VALUES ('default','Bootstrap','document','ohm-content','public','super-draft')") conn.execute("INSERT OR IGNORE INTO projects (id,name,type,content_repo,visibility,initial_state) " "VALUES ('ohm','Open Human Model','document','ohm-content','public','super-draft')") conn.execute("INSERT INTO users (id,gitea_login,display_name,role) VALUES (1,'a','A','contributor')") # default-stamped data with a composite-FK child conn.execute("INSERT INTO cached_rfcs (slug,title,state,project_id) VALUES ('human','Human','active','default')") conn.execute("INSERT INTO rfc_collaborators (rfc_slug,user_id,role_in_rfc,project_id) " "VALUES ('human',1,'contributor','default')") conn.execute("INSERT INTO stars (user_id,rfc_slug,project_id) VALUES (1,'human','default')") return cfg, conn def test_restamp_moves_data_and_drops_bootstrap_row(monkeypatch): cfg, conn = _setup(monkeypatch, default_id="ohm") projects.restamp_default_project(cfg) assert conn.execute("SELECT COUNT(*) c FROM cached_rfcs WHERE project_id='default'").fetchone()["c"] == 0 assert conn.execute("SELECT project_id FROM cached_rfcs WHERE slug='human'").fetchone()["project_id"] == "ohm" assert conn.execute("SELECT project_id FROM rfc_collaborators WHERE rfc_slug='human'").fetchone()["project_id"] == "ohm" assert conn.execute("SELECT project_id FROM stars WHERE rfc_slug='human'").fetchone()["project_id"] == "ohm" # stale bootstrap projects row removed; 'ohm' remains assert conn.execute("SELECT 1 FROM projects WHERE id='default'").fetchone() is None assert conn.execute("SELECT 1 FROM projects WHERE id='ohm'").fetchone() is not None # FK integrity intact after the rename assert conn.execute("PRAGMA foreign_key_check").fetchall() == [] def test_restamp_is_idempotent(monkeypatch): cfg, conn = _setup(monkeypatch, default_id="ohm") projects.restamp_default_project(cfg) projects.restamp_default_project(cfg) # second call: no rows left โ†’ no-op assert conn.execute("SELECT COUNT(*) c FROM cached_rfcs WHERE project_id='ohm'").fetchone()["c"] == 1 def test_restamp_noop_when_default_id_unchanged(monkeypatch): cfg, conn = _setup(monkeypatch, default_id="") # resolves to 'default' projects.restamp_default_project(cfg) # nothing renamed; bootstrap data + row still present assert conn.execute("SELECT project_id FROM cached_rfcs WHERE slug='human'").fetchone()["project_id"] == "default"