-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
55 lines (40 loc) · 1.32 KB
/
database.py
File metadata and controls
55 lines (40 loc) · 1.32 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
import os
import pwd
import json
import models
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
# sudo -u postgres psql
# create user abacus with password somepass;
# grant ALL on database test to abacus;
config_path = os.path.expanduser('~/code/abacus/config.json')
config = json.load(open(config_path, 'r'))
engine = create_engine(
'postgresql://{db_user}:{db_password}@{db_host}:{db_port}/{db}'.format(
**config),
echo=False)
engine.connect()
Session = sessionmaker()
Session.configure(bind=engine)
def get_session():
return Session()
def show_warnings(session):
cursor = session.execute("""
SELECT 'DROP FUNCTION ' || oid::regprocedure
FROM pg_proc
WHERE proname = 'upsert_event_name'
AND pg_function_is_visible(oid);
""")
all_upsert_functions = zip(*cursor.fetchall())[0]
if len(all_upsert_functions) > 1:
print ("Multiple 'upsert_event_name' functions found."
" Delete all manually and try again.")
print '\n'.join(all_upsert_functions)
def main():
models.Base.metadata.create_all(engine)
class DummyTarget(object):
name = models.EventName.__tablename__
models.create_event_name_upsert_function(DummyTarget, session)
show_warnings(session)
if __name__ == '__main__':
main()