init_postgres.py
1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
"""Initialize PostgreSQL schema for lyric dedup storage."""
from __future__ import annotations
import argparse
import sys
from pathlib import Path
PROJECT_ROOT = Path(__file__).resolve().parents[1]
SCHEMA_PATH = PROJECT_ROOT / "scripts" / "postgres_schema.sql"
def main() -> None:
parser = argparse.ArgumentParser(description="Initialize PostgreSQL schema for lyric dedup.")
parser.add_argument("--dsn", required=True, help="PostgreSQL DSN, e.g. postgresql://user:pass@localhost:5432/lyric_dedup")
parser.add_argument("--schema", default=str(SCHEMA_PATH))
args = parser.parse_args()
psycopg = _import_psycopg()
schema_sql = Path(args.schema).read_text(encoding="utf-8")
with psycopg.connect(args.dsn) as conn:
with conn.cursor() as cursor:
cursor.execute(schema_sql)
conn.commit()
print(f"initialized schema from {args.schema}")
def _import_psycopg():
try:
import psycopg
return psycopg
except ModuleNotFoundError:
print(
"Missing dependency: psycopg. Install it with:\n"
" python -m pip install 'psycopg[binary]'",
file=sys.stderr,
)
raise SystemExit(1)
if __name__ == "__main__":
main()