Add tools/check_databases.py
Database Integrity At least one Ravenous database failed an integrity check.
Ravenous/ravenous_databases/pipeline/head There was a failure building this commit
Details
Database Integrity At least one Ravenous database failed an integrity check.
Ravenous/ravenous_databases/pipeline/head There was a failure building this commit
Details
This commit is contained in:
parent
d831f8e242
commit
5a32f0dafb
|
|
@ -0,0 +1,201 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import subprocess
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
try:
|
||||
import sqlite3
|
||||
except ModuleNotFoundError:
|
||||
import pysqlite3 as sqlite3
|
||||
|
||||
|
||||
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 check_pragma(
|
||||
cursor: sqlite3.Cursor,
|
||||
pragma: str,
|
||||
) -> bool:
|
||||
print(f" {pragma} ... ", end="", flush=True)
|
||||
|
||||
rows = cursor.execute(
|
||||
f"PRAGMA {pragma}"
|
||||
).fetchall()
|
||||
|
||||
if rows == [("ok",)]:
|
||||
print("OK")
|
||||
return True
|
||||
|
||||
print("FAILED")
|
||||
|
||||
for row in rows:
|
||||
print(
|
||||
f" {row}",
|
||||
file=sys.stderr,
|
||||
)
|
||||
|
||||
return False
|
||||
|
||||
|
||||
def check_database(path: Path) -> bool:
|
||||
print("=" * 72)
|
||||
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:
|
||||
print(
|
||||
f"✗ Cannot open database: {error}",
|
||||
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,
|
||||
)
|
||||
success = False
|
||||
|
||||
finally:
|
||||
connection.close()
|
||||
|
||||
if success:
|
||||
print(f"✓ {path} is healthy")
|
||||
else:
|
||||
print(
|
||||
f"✗ {path} failed integrity checks",
|
||||
file=sys.stderr,
|
||||
)
|
||||
|
||||
print()
|
||||
|
||||
return success
|
||||
|
||||
|
||||
def main() -> int:
|
||||
databases = tracked_databases()
|
||||
|
||||
if not databases:
|
||||
print(
|
||||
"ERROR: no tracked SQLite databases found.",
|
||||
file=sys.stderr,
|
||||
)
|
||||
return 1
|
||||
|
||||
print("SQLite implementation:")
|
||||
print(f" {sqlite3.sqlite_version}")
|
||||
print()
|
||||
|
||||
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())
|
||||
Loading…
Reference in New Issue