diff --git a/tools/check_databases.py b/tools/check_databases.py index f805f00..03350c2 100644 --- a/tools/check_databases.py +++ b/tools/check_databases.py @@ -2,15 +2,11 @@ from __future__ import annotations +import shutil import subprocess import sys from pathlib import Path -try: - import sqlite3 -except ModuleNotFoundError: - import pysqlite3 as sqlite3 - DATABASE_EXTENSIONS = { ".db", @@ -34,25 +30,97 @@ def tracked_databases() -> list[Path]: ] -def check_pragma( - cursor: sqlite3.Cursor, +def run_sqlite(path: Path, sql: str) -> subprocess.CompletedProcess[str]: + return subprocess.run( + [ + "sqlite3", + "-readonly", + str(path), + sql, + ], + capture_output=True, + text=True, + ) + + +def check_integrity( + path: Path, pragma: str, ) -> bool: - print(f" {pragma} ... ", end="", flush=True) + print( + f" {pragma} ... ", + end="", + flush=True, + ) - rows = cursor.execute( - f"PRAGMA {pragma}" - ).fetchall() + result = run_sqlite( + path, + f"PRAGMA {pragma};", + ) - if rows == [("ok",)]: + if result.returncode != 0: + print("FAILED") + + if result.stderr: + print( + result.stderr.strip(), + file=sys.stderr, + ) + + return False + + output = result.stdout.strip() + + if output == "ok": print("OK") return True print("FAILED") - for row in rows: + if output: + for line in output.splitlines(): + print( + f" {line}", + file=sys.stderr, + ) + + return False + + +def check_foreign_keys(path: Path) -> bool: + print( + " foreign_key_check ... ", + end="", + flush=True, + ) + + result = run_sqlite( + path, + "PRAGMA foreign_key_check;", + ) + + if result.returncode != 0: + print("FAILED") + + if result.stderr: + print( + result.stderr.strip(), + file=sys.stderr, + ) + + return False + + output = result.stdout.strip() + + if not output: + print("OK") + return True + + print("FAILED") + + for line in output.splitlines(): print( - f" {row}", + f" {line}", file=sys.stderr, ) @@ -64,75 +132,29 @@ def check_database(path: Path) -> bool: print(f"Checking {path}") print("=" * 72) - try: - uri = f"{path.resolve().as_uri()}?mode=ro" - - connection = sqlite3.connect( - uri, - uri=True, - ) - - except sqlite3.Error as error: + if not path.is_file(): print( - f"✗ Cannot open database: {error}", + f"✗ Database does not exist: {path}", file=sys.stderr, ) return False success = True - try: - cursor = connection.cursor() - - if not check_pragma( - cursor, - "quick_check", - ): - success = False - - if not check_pragma( - cursor, - "integrity_check", - ): - success = False - - print( - " foreign_key_check ... ", - end="", - flush=True, - ) - - foreign_key_errors = cursor.execute( - "PRAGMA foreign_key_check" - ).fetchall() - - if foreign_key_errors: - print("FAILED") - success = False - - for row in foreign_key_errors: - table, rowid, parent, fk_id = row - - print( - " " - f"table={table}, " - f"rowid={rowid}, " - f"parent={parent}, " - f"foreign_key={fk_id}", - file=sys.stderr, - ) - else: - print("OK") - - except sqlite3.Error as error: - print( - f"✗ SQLite error: {error}", - file=sys.stderr, - ) + if not check_integrity( + path, + "quick_check", + ): success = False - finally: - connection.close() + if not check_integrity( + path, + "integrity_check", + ): + success = False + + if not check_foreign_keys(path): + success = False if success: print(f"✓ {path} is healthy") @@ -148,6 +170,23 @@ def check_database(path: Path) -> bool: def main() -> int: + if shutil.which("sqlite3") is None: + print( + "ERROR: sqlite3 executable not found.", + file=sys.stderr, + ) + return 2 + + version = subprocess.run( + ["sqlite3", "--version"], + check=True, + capture_output=True, + text=True, + ).stdout.strip() + + print(f"SQLite: {version}") + print() + databases = tracked_databases() if not databases: @@ -157,10 +196,6 @@ def main() -> int: ) return 1 - print("SQLite implementation:") - print(f" {sqlite3.sqlite_version}") - print() - print("Databases:") for database in databases: print(f" - {database}")