23 lines
811 B
Python
23 lines
811 B
Python
import os
|
|
from pathlib import Path
|
|
import psycopg
|
|
from psycopg.rows import dict_row
|
|
|
|
def connect():
|
|
# Production passes credentials as discrete libpq fields. This avoids
|
|
# treating characters in a generated password as URL syntax. Local and
|
|
# test environments retain DATABASE_URL compatibility.
|
|
if os.environ.get('DATABASE_HOST'):
|
|
return psycopg.connect(
|
|
host=os.environ['DATABASE_HOST'],
|
|
dbname=os.environ['DATABASE_NAME'],
|
|
user=os.environ['DATABASE_USER'],
|
|
password=os.environ['DATABASE_PASSWORD'],
|
|
row_factory=dict_row,
|
|
)
|
|
return psycopg.connect(os.environ['DATABASE_URL'], row_factory=dict_row)
|
|
|
|
def initialize():
|
|
with connect() as c:
|
|
c.execute(Path(__file__).with_name('schema.sql').read_text())
|