"""Constructed SQLite partial-index controls; no timing or external database input.""" import argparse from datetime import datetime, timezone import hashlib import json from pathlib import Path import platform import sqlite3 import tempfile SCHEMA = [ "CREATE TABLE items(id INTEGER PRIMARY KEY, label TEXT NOT NULL, active INTEGER)", "CREATE UNIQUE INDEX one_active ON items(label) WHERE active = 1", ] STEPS = [ ("included", "INSERT INTO items VALUES(1, 'group-a', 1)"), ("excluded_false", "INSERT INTO items VALUES(2, 'group-a', 0)"), ("excluded_null", "INSERT INTO items VALUES(3, 'group-a', NULL)"), ("duplicate_included", "INSERT INTO items VALUES(4, 'group-a', 1)"), ("promote_conflict", "UPDATE items SET active=1 WHERE id=2"), ("leave_subset", "UPDATE items SET active=0 WHERE id=1"), ("promote_after_release", "UPDATE items SET active=1 WHERE id=2"), ] ROWS = "SELECT id, label, active FROM items ORDER BY id" MEMBERS = "SELECT id FROM items WHERE active=1 ORDER BY id" def snapshot(db): return { "rows": [list(row) for row in db.execute(ROWS)], "selected_ids": [row[0] for row in db.execute(MEMBERS)], "predicate_truth": [list(row) for row in db.execute( "SELECT id, active=1 FROM items ORDER BY id")], } def observe(): with tempfile.TemporaryDirectory(prefix="hesela-partial-index-") as tmp: path = Path(tmp) / "fixture.sqlite" db = sqlite3.connect(path, isolation_level=None) try: source_id = db.execute("SELECT sqlite_source_id()").fetchone()[0] compile_options = [row[0] for row in db.execute("PRAGMA compile_options")] for sql in SCHEMA: db.execute(sql) cases = [] for name, sql in STEPS: error = None try: db.execute(sql) except sqlite3.IntegrityError as exc: error = {"code": exc.sqlite_errorcode, "name": exc.sqlite_errorname, "message": str(exc)} cases.append({"case": name, "sql": sql, "error": error, **snapshot(db)}) indexes = [list(row) for row in db.execute("PRAGMA index_list(items)")] integrity = [list(row) for row in db.execute("PRAGMA integrity_check")] finally: db.close() reopened_db = sqlite3.connect(path.as_uri() + "?mode=ro", uri=True) try: reopened = snapshot(reopened_db) finally: reopened_db.close() return { "schema_version": 1, "generated_at": datetime.now(timezone.utc).isoformat(), "classification": "constructed SQLite semantics demonstration", "environment": {"python": platform.python_version(), "sqlite": sqlite3.sqlite_version, "sqlite_source_id": source_id, "compile_options": compile_options}, "code_sha256": hashlib.sha256(Path(__file__).read_bytes()).hexdigest(), "schema": SCHEMA, "state_query": ROWS, "membership_query": MEMBERS, "cases": cases, "indexes": indexes, "integrity_check": integrity, "reopened": reopened, "limits": "Seven dependent steps on one fresh database, single connection and autocommit statements. NOT NULL key isolates predicate NULL from key NULL. No planner-choice, concurrency, crash, storage-size, speed or PostgreSQL measurement.", } def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--output", type=Path, required=True) args = parser.parse_args() result = observe() with args.output.open("x", encoding="utf-8") as target: json.dump(result, target, indent=2) target.write("\n") if __name__ == "__main__": main()