-
Notifications
You must be signed in to change notification settings - Fork 0
/
init_db.py
45 lines (33 loc) · 1.02 KB
/
init_db.py
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
45
from sqlalchemy import create_engine, MetaData
from poem_for_the_day.db import users
import os
DSN = "postgresql://{user}:{password}@{host}:{port}/{database}"
def create_tables(engine):
meta = MetaData()
meta.create_all(bind=engine, tables=[users])
def drop_tables(engine):
meta = MetaData()
meta.drop_all(bind=engine, tables=[users])
def sample_data(engine):
conn = engine.connect()
conn.execute(users.insert(), [
{
'id': 132341234,
'username': '[email protected]',
'first_name': 'Test',
'last_name': 'User',
}
])
conn.close()
if __name__ == '__main__':
db_url = DSN.format(
user=os.environ['POSTGRES_USER'],
password=os.environ['POSTGRES_PASSWORD'],
host=os.environ['POSTGRES_HOST'],
port=os.environ['POSTGRES_PORT'],
database=os.environ['POSTGRES_DB'],
)
engine = create_engine(db_url)
create_tables(engine)
sample_data(engine)
print(engine.execute(users.select()).first())