236 lines
4.1 KiB
Python
236 lines
4.1 KiB
Python
#!/usr/bin/env python3
|
|
|
|
from __future__ import annotations
|
|
|
|
import shutil
|
|
import subprocess
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
|
|
DATABASE_EXTENSIONS = {
|
|
".db",
|
|
".sqlite",
|
|
".sqlite3",
|
|
}
|
|
|
|
|
|
def tracked_databases() -> list[Path]:
|
|
result = subprocess.run(
|
|
["git", "ls-files"],
|
|
check=True,
|
|
capture_output=True,
|
|
text=True,
|
|
)
|
|
|
|
return [
|
|
Path(filename)
|
|
for filename in result.stdout.splitlines()
|
|
if Path(filename).suffix.lower() in DATABASE_EXTENSIONS
|
|
]
|
|
|
|
|
|
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,
|
|
)
|
|
|
|
result = run_sqlite(
|
|
path,
|
|
f"PRAGMA {pragma};",
|
|
)
|
|
|
|
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")
|
|
|
|
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" {line}",
|
|
file=sys.stderr,
|
|
)
|
|
|
|
return False
|
|
|
|
|
|
def check_database(path: Path) -> bool:
|
|
print("=" * 72)
|
|
print(f"Checking {path}")
|
|
print("=" * 72)
|
|
|
|
if not path.is_file():
|
|
print(
|
|
f"✗ Database does not exist: {path}",
|
|
file=sys.stderr,
|
|
)
|
|
return False
|
|
|
|
success = True
|
|
|
|
if not check_integrity(
|
|
path,
|
|
"quick_check",
|
|
):
|
|
success = False
|
|
|
|
if not check_integrity(
|
|
path,
|
|
"integrity_check",
|
|
):
|
|
success = False
|
|
|
|
if not check_foreign_keys(path):
|
|
success = False
|
|
|
|
if success:
|
|
print(f"✓ {path} is healthy")
|
|
else:
|
|
print(
|
|
f"✗ {path} failed integrity checks",
|
|
file=sys.stderr,
|
|
)
|
|
|
|
print()
|
|
|
|
return success
|
|
|
|
|
|
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:
|
|
print(
|
|
"ERROR: no tracked SQLite databases found.",
|
|
file=sys.stderr,
|
|
)
|
|
return 1
|
|
|
|
print("Databases:")
|
|
for database in databases:
|
|
print(f" - {database}")
|
|
|
|
print()
|
|
|
|
failed: list[Path] = []
|
|
|
|
for database in databases:
|
|
if not check_database(database):
|
|
failed.append(database)
|
|
|
|
print("=" * 72)
|
|
|
|
if failed:
|
|
print(
|
|
"DATABASE INTEGRITY CHECK FAILED",
|
|
file=sys.stderr,
|
|
)
|
|
|
|
for database in failed:
|
|
print(
|
|
f" ✗ {database}",
|
|
file=sys.stderr,
|
|
)
|
|
|
|
return 1
|
|
|
|
print(
|
|
f"✓ All {len(databases)} database(s) "
|
|
"passed integrity checks."
|
|
)
|
|
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main()) |