Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion backend/src/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ const GAME_COLS = `
ratings_count AS "ratingsCount",
playtime,
esrb_rating AS "esrbRating",
dominant_color AS "dominantColor",
background_image AS "backgroundImage",
background_image_additional AS "backgroundImageAdditional",
COALESCE(screenshots, '{}') AS "screenshots"
Expand Down
1 change: 0 additions & 1 deletion backend/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ export const typeDefs = `#graphql
ratingsCount: Int!,
playtime: Int,
esrbRating: String,
dominantColor: String,
backgroundImage: String,
backgroundImageAdditional: String,
screenshots: [String!]!
Expand Down
49 changes: 30 additions & 19 deletions db/load.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
PARQUET = os.environ.get("PARQUET_PATH", str(Path.home() / "data" / "videogames_data.parquet"))
DATABASE_URL = os.environ["DATABASE_URL"]
FORCE = "--force" in sys.argv
WIPE_USERS = "--wipe-user-data" in sys.argv
# Tags used by fewer games than this are dropped (long tail of one-off, noisy tags).
MIN_TAG_GAMES = int(os.environ.get("MIN_TAG_GAMES", "50"))

Expand Down Expand Up @@ -140,18 +141,30 @@ def copy_csv(cur: psycopg.Cursor, table: str, columns: str, path: Path) -> None:
while chunk := f.read(1 << 20):
cp.write(chunk)

def has_user_data(cur) -> bool:
for table in ("users", "reviews"):
cur.execute("SELECT to_regclass(%s)", (f"public.{table}",))
if cur.fetchone()[0] is None:
continue
cur.execute(f"SELECT EXISTS (SELECT 1 FROM {table})")
if cur.fetchone()[0]:
return True
return False

def main() -> None:
with tempfile.TemporaryDirectory() as tmp:
out = Path(tmp)
print("Exporting CSVs from Parquet...")
export_csvs(out)
with psycopg.connect(DATABASE_URL) as conn:
cur = conn.cursor()
cur.execute("SELECT to_regclass('public.games')")
if cur.fetchone()[0] is not None and not FORCE:
sys.exit("games table already exists. Rerun with --force to force reload.")

with psycopg.connect(DATABASE_URL) as conn:
cur = conn.cursor()
cur.execute("SELECT to_regclass('public.games')")
if cur.fetchone()[0] is not None and not FORCE:
sys.exit("games table already exists. Rerun with --force to wipe and reload (deletes reviews too).")
if has_user_data(cur) and not WIPE_USERS:
sys.exit("users/reviews contain data. Add --wipe-user-data to delete it.")

with tempfile.TemporaryDirectory() as tmp:
out = Path(tmp)
print("Exporting CSVs from Parquet...")
export_csvs(out)

print("Creating tables...")
cur.execute((HERE / "schema.sql").read_text())
Expand All @@ -162,17 +175,15 @@ def main() -> None:
copy_csv(cur, table, "id, name", out / f"{table}.csv")
copy_csv(cur, junction, f"game_id, {fk}", out / f"{junction}.csv")

print("Creating indexes...")
cur.execute((HERE / "indexes.sql").read_text())
conn.commit()

cur.execute("ANALYZE")
conn.commit()

for table in ["games"] + [r[1] for r in RELATIONS] + [r[2] for r in RELATIONS]:
cur.execute(f"SELECT count(*) FROM {table}")
print(f"{table}: {cur.fetchone()[0]}")
print("Creating indexes...")
cur.execute((HERE / "indexes.sql").read_text())
conn.commit()

cur.execute("ANALYZE")
conn.commit()

for table in ["games"] + [r[1] for r in RELATIONS] + [r[2] for r in RELATIONS]:
cur.execute(f"SELECT count(*) FROM {table}")
print(f"{table}: {cur.fetchone()[0]}")
if __name__ == "__main__":
main()