
import sqlite3
from pathlib import Path

DATABASES = {
    "LIVE": Path("/home/skeversu/skever-portal-v1/db.sqlite3"),
    "BACKUP": Path("/home/skeversu/skever-portal-v1/db copy"),
}

def inspect_database(label, path):
    print(f"\n===== {label} =====")
    print("File exists:", path.is_file())

    if not path.is_file():
        return

    print("Size:", path.stat().st_size, "bytes")

    try:
        uri = path.resolve().as_uri() + "?mode=ro"

        with sqlite3.connect(uri, uri=True, timeout=5) as conn:
            conn.execute("PRAGMA query_only = ON")

            tables = conn.execute("""
                SELECT name
                FROM sqlite_master
                WHERE type = 'table'
                ORDER BY name
            """).fetchall()

            print("Tables found:", len(tables))

            for (table,) in tables:
                try:
                    count = conn.execute(
                        f'SELECT COUNT(*) FROM "{table}"'
                    ).fetchone()[0]
                    print(f"{table}: {count} rows")
                except sqlite3.DatabaseError as exc:
                    print(f"{table}: READ ERROR - {exc}")

            try:
                users = conn.execute("""
                    SELECT username, is_active, is_superuser
                    FROM auth_user
                    ORDER BY username
                """).fetchall()
                print("Admin/user accounts:", users)
            except sqlite3.DatabaseError as exc:
                print("User inspection error:", exc)

    except sqlite3.DatabaseError as exc:
        print("DATABASE ERROR:", repr(exc))

for label, path in DATABASES.items():
    inspect_database(label, path)

print("\n===== COMPARISON FINISHED =====")
