-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit_db.py
More file actions
76 lines (55 loc) · 2.13 KB
/
Copy pathinit_db.py
File metadata and controls
76 lines (55 loc) · 2.13 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import argparse
from sqlalchemy import create_engine
from application.config import CONFIG
DSN = 'postgresql://{user}:{password}@{host}:{port}/{database}'
ADMIN_DB_URL = DSN.format(user='postgres', password='postgres', database='postgres', host='localhost', port=5432)
admin_engine = create_engine(ADMIN_DB_URL, isolation_level='AUTOCOMMIT')
def parse_args() -> None:
parser = argparse.ArgumentParser('Manage Entities App database initializer')
parser.add_argument(
'mode',
type=str,
help='Use "setup" to create db and "setup_test" to create db for tests '
'or "teardown" and "teardown_test" to delete all database data associated with this application.',
)
return parser.parse_args()
def setup_db(config) -> None:
db_name = config['database']
db_user = config['user']
db_pass = config['password']
conn = admin_engine.connect()
conn.execute('DROP DATABASE IF EXISTS %s' % db_name)
conn.execute('DROP ROLE IF EXISTS %s' % db_user)
conn.execute("CREATE USER %s WITH PASSWORD '%s'" % (db_user, db_pass))
conn.execute('CREATE DATABASE %s' % db_name)
conn.execute('GRANT ALL PRIVILEGES ON DATABASE %s TO %s' % (db_name, db_user))
conn.close()
def teardown_db(config) -> None:
db_name = config['database']
db_user = config['user']
conn = admin_engine.connect()
conn.execute(
"""
SELECT pg_terminate_backend(pg_stat_activity.pid)
FROM pg_stat_activity
WHERE pg_stat_activity.datname = '%s'
AND pid <> pg_backend_pid();"""
% db_name
)
conn.execute('DROP DATABASE IF EXISTS %s' % db_name)
conn.execute('DROP ROLE IF EXISTS %s' % db_user)
conn.close()
if __name__ == '__main__':
db_url = DSN.format(**CONFIG['postgres'])
engine = create_engine(db_url)
args = parse_args()
mode = args.mode
if mode == 'setup':
setup_db(CONFIG['postgres'])
elif mode == 'setup_test':
setup_db(CONFIG['postgres_test'])
elif mode == 'teardown':
teardown_db(CONFIG['postgres'])
elif mode == 'teardown_test':
teardown_db(CONFIG['postgres_test'])
print('Done!')