-
Notifications
You must be signed in to change notification settings - Fork 2
/
manage.py
69 lines (47 loc) · 2.21 KB
/
manage.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import os
from typing import Optional
from flask import Flask
from flask_migrate import Migrate, MigrateCommand
from flask_script import Manager
from flask_sqlalchemy import SQLAlchemy # type:ignore
from invisible_flow.app_factory import app
from invisible_flow.constants import COPA_DB_BIND_KEY
"""
The following section should write the env variable to fs.
use os.environ.get('MY_ENVIRONMENT_VARIABLE') to set some stuff based on env.
use some sort of file system writer to write a file to this directory.
"""
if os.environ.get('ENVIRONMENT') == 'heroku':
credentials_file = os.environ.get("GOOGLE_APPLICATION_CREDENTIALS")
credentials_content = os.environ.get("GOOGLE_CREDENTIALS")
with open("/app/invisible_flow/{credentials_file}".format(credentials_file=credentials_file), "w") as writer:
writer.write("{credentials_content}".format(credentials_content=credentials_content))
copa_db_filename: Optional[str] = ''
if os.environ.get("ENVIRONMENT") == 'local' or os.environ.get('ENVIRONMENT') == 'travis':
copa_db_filename = 'postgres+psycopg2://invisible_flow@localhost:5432/invisible_flow_testing'
elif os.environ.get("ENVIRONMENT") == 'docker':
copa_db_filename = 'postgres+psycopg2://invisible_flow_testing@localhost:5432/invisible_flow_testing'
elif os.environ.get("ENVIRONMENT") == 'development':
copa_db_filename = 'postgres+psycopg2://invisible_flow_testing@localhost:5432/invisible_flow_testing'
elif os.environ.get('ENVIRONMENT') == 'heroku':
heroku_db_url = str(os.environ.get('DATABASE_URL'))
split_heroku_db_url = heroku_db_url.split(':', 1)
copa_db_filename = split_heroku_db_url[0] + '+psycopg2:' + split_heroku_db_url[1]
else:
raise Exception('Unable to determine environment when setting database URL')
def setup_db(_app: Flask) -> SQLAlchemy:
db_config = {
'SQLALCHEMY_BINDS': {
COPA_DB_BIND_KEY: copa_db_filename
},
'SQLALCHEMY_DATABASE_URI': copa_db_filename,
'SQLALCHEMY_TRACK_MODIFICATIONS': False
}
_app.config.update(db_config)
return SQLAlchemy(_app)
db = setup_db(app)
migrate = Migrate(app, db)
manager = Manager(app)
manager.add_command('db', MigrateCommand)
if __name__ == '__main__':
manager.run()