Adds systems.db regeneration discipline (#855) via a `meta` table (#856) stamped by every generator, a pre-push hook that rejects stale DBs (#857), and the top-level `make regen-db` / `make check-systems-db` targets that drive the whole pipeline. The stamp stores SHA-1 of generator source + schema, so the pre-push hook can cheaply detect "you changed a generator but forgot to regen the DB" before a binary merge conflict lands. Sprint 36 hit that class of conflict on two branches touching systems.db simultaneously — this is the systemic fix. Regenerated systems.db is stamped; `make check-systems-db` passes. Refs: #855 #856 #857 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
119 lines
3.6 KiB
Python
119 lines
3.6 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
check-systems-db-stamp — verify that server/data/systems.db is up to date.
|
|
|
|
Reads the meta table from systems.db and checks that the stored SHA-1 of each
|
|
generator's source file(s) matches the current file content on disk.
|
|
|
|
Exit codes:
|
|
0 — DB is stamped and all generator SHAs match current sources
|
|
1 — DB is stale (one or more generators have changed since last regen)
|
|
2 — DB does not have a meta table (treat as unstamped — run make regen-db)
|
|
|
|
Usage (called by .config/hooks/pre-push):
|
|
tooling/check-systems-db-stamp
|
|
|
|
Usage (interactive):
|
|
tooling/check-systems-db-stamp --verbose
|
|
|
|
Decision refs: #855 (generator versioning), #857 (pre-push hook)
|
|
"""
|
|
|
|
import hashlib
|
|
import sqlite3
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
REPO_ROOT = Path(__file__).resolve().parent.parent
|
|
|
|
DB_PATH = REPO_ROOT / "server" / "data" / "systems.db"
|
|
|
|
# Maps generator_name (as stored in meta.generator_name) to the source
|
|
# file(s) whose SHA is stamped. The SHA is computed as SHA-1 of the
|
|
# concatenated bytes of all files in sorted order.
|
|
GENERATOR_SOURCES: dict[str, list[Path]] = {
|
|
"import_economics": [
|
|
REPO_ROOT / "tooling" / "economy-db" / "import_economics.py",
|
|
],
|
|
"generate_brands": [
|
|
REPO_ROOT / "server" / "src" / "bin" / "generate_brands" / "main.rs",
|
|
],
|
|
"generate_atlas": [
|
|
REPO_ROOT / "tooling" / "planet-gen" / "generate_atlas.py",
|
|
],
|
|
}
|
|
|
|
|
|
def file_sha1(*paths: Path) -> str:
|
|
"""SHA-1 of concatenated file contents (sorted paths, missing files skipped)."""
|
|
h = hashlib.sha1()
|
|
for p in sorted(paths):
|
|
if p.exists():
|
|
h.update(p.read_bytes())
|
|
return h.hexdigest()
|
|
|
|
|
|
def check(verbose: bool = False) -> int:
|
|
"""Return exit code: 0 = fresh, 1 = stale, 2 = no meta table."""
|
|
if not DB_PATH.exists():
|
|
if verbose:
|
|
print(f"check-systems-db-stamp: {DB_PATH} not found — skipping check")
|
|
return 0
|
|
|
|
try:
|
|
conn = sqlite3.connect(str(DB_PATH))
|
|
rows = conn.execute(
|
|
"SELECT generator_name, generator_sha FROM meta"
|
|
).fetchall()
|
|
conn.close()
|
|
except sqlite3.OperationalError:
|
|
# meta table does not exist
|
|
if verbose:
|
|
print("check-systems-db-stamp: no meta table — systems.db has not been stamped")
|
|
print(" Run: make regen-db")
|
|
return 2
|
|
|
|
if not rows:
|
|
if verbose:
|
|
print("check-systems-db-stamp: meta table is empty — systems.db has not been stamped")
|
|
print(" Run: make regen-db")
|
|
return 2
|
|
|
|
stale: list[str] = []
|
|
for generator_name, stored_sha in rows:
|
|
sources = GENERATOR_SOURCES.get(generator_name)
|
|
if sources is None:
|
|
# Unknown generator — skip (forward compat)
|
|
continue
|
|
current_sha = file_sha1(*sources)
|
|
if current_sha != stored_sha:
|
|
stale.append(generator_name)
|
|
if verbose:
|
|
print(
|
|
f"check-systems-db-stamp: STALE — {generator_name}"
|
|
f"\n stored: {stored_sha}"
|
|
f"\n current: {current_sha}"
|
|
)
|
|
|
|
if stale:
|
|
if not verbose:
|
|
print(
|
|
"systems.db is stale — run `make regen-db` before pushing.",
|
|
file=sys.stderr,
|
|
)
|
|
print(f" Stale generators: {stale}", file=sys.stderr)
|
|
return 1
|
|
|
|
if verbose:
|
|
print(f"check-systems-db-stamp: OK — {len(rows)} generator(s) up to date")
|
|
return 0
|
|
|
|
|
|
def main() -> None:
|
|
verbose = "--verbose" in sys.argv or "-v" in sys.argv
|
|
sys.exit(check(verbose=verbose))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|