-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdatabase.py
55 lines (46 loc) · 1.75 KB
/
database.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
46
47
48
49
50
51
52
53
54
55
from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base
from geoalchemy2 import *
import os, subprocess
import yaml
import initialize_environment
config = yaml.load(open('config.yaml', 'r'))
postgis_extensions_dir = config['database']['postgis_extensions_dir']
database_type = ''
try:
if os.environ['FLASK_ENV'] == 'test':
database_type = 'test'
database_name = config['database']['test']['db']
database_user = config['database']['test']['user']
database_pass = config['database']['test']['pass']
else:
database_name = config['database']['development']['db']
database_user = config['database']['development']['user']
database_pass = config['database']['development']['pass']
except:
database_type = 'development'
database_name = config['database']['development']['db']
database_user = config['database']['development']['user']
database_pass = config['database']['development']['pass']
engine = create_engine('postgresql://'+database_user+':'+database_pass+'@localhost/' + database_name)
session = scoped_session(sessionmaker(bind=engine))
metadata = MetaData(engine)
Base = declarative_base(metadata=metadata)
Base.query = session.query_property()
def init_db():
import models
metadata.create_all(bind=engine)
session.commit()
def drop_db():
import models
session.close()
metadata.drop_all(bind=engine)
def destroy_db():
postgres_engine = create_engine('postgresql://'+database_user+':'+database_pass+'@localhost/postgres')
conn = postgres_engine.connect()
conn.execute('commit')
conn.execute('drop database ' + database_name)
conn.close()
def create_db():
initialize_environment.main(database_type)