This repository has been archived by the owner on Jul 7, 2022. It is now read-only.
forked from Crown-Commercial-Service/digitalmarketplace-api
-
Notifications
You must be signed in to change notification settings - Fork 6
/
migrations.py
207 lines (134 loc) · 5.69 KB
/
migrations.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import six
import sys
import io
import os
from migra import Migration
from sqlbag import S, temporary_database as temporary_db, load_sql_from_file, load_sql_from_folder
from config import configs
import subprocess
from app import models # noqa
from app import create_app, db
if six.PY2:
input = raw_input
def prompt(question):
print(question + ' ', end='')
return input().strip().lower() == 'y'
def load_current_production_state(dburl):
with S(dburl) as s:
load_sql_from_file(s, 'DB/dumps/prod.schema.dump.sql')
def load_current_staging_state(dburl):
with S(dburl) as s:
load_sql_from_file(s, 'DB/dumps/staging.schema.dump.sql')
def load_post_migration_state(dburl):
load_current_production_state(dburl)
with S(dburl) as s:
load_sql_from_folder(s, 'DB/migration/pending')
def load_from_app_model(dburl):
def create_tables(include=None, exclude=None):
tables = set(
t for t in db.metadata.tables.values()
if ((include is None) or t.name in include)
and ((exclude is None) or t.name not in exclude)
)
db.metadata.create_all(s.bind.engine, tables=tables)
with S(dburl) as s:
load_sql_from_file(s, 'DB/migration/setup-pre.sql')
create_tables()
with S(dburl) as s:
load_sql_from_file(s, 'DB/migration/setup-post.sql')
def load_test_fixtures(dburl):
with S(dburl) as s:
load_sql_from_file(s, 'DB/data/test_fixtures.sql')
def sync():
DB_URL = get_current_app_db_url()
with temporary_db() as TEMP_DB_URL:
load_from_app_model(TEMP_DB_URL)
with S(DB_URL) as s_current, S(TEMP_DB_URL) as s_target:
m = Migration(s_current, s_target)
m.set_safety(False)
m.add_all_changes()
if m.statements:
print('THE FOLLOWING CHANGES ARE PENDING:', end='\n\n')
print(m.sql)
print('Applying...')
m.apply()
else:
print('Already synced.')
def pending(write_to_file=False):
with temporary_db() as CURRENT_DB_URL, temporary_db() as TARGET_DB_URL:
load_current_production_state(CURRENT_DB_URL)
load_current_staging_state(TARGET_DB_URL)
with S(CURRENT_DB_URL) as s_current, S(TARGET_DB_URL) as s_target:
m = Migration(s_current, s_target)
m.set_safety(False)
m.add_all_changes()
print('-- Pending (prod -> staging):\n\n{}'.format(m.sql))
if write_to_file:
with io.open('DB/migration/pending/pending.sql', 'w') as w:
w.write(m.sql)
with temporary_db() as CURRENT_DB_URL, temporary_db() as TARGET_DB_URL:
load_current_staging_state(CURRENT_DB_URL)
load_from_app_model(TARGET_DB_URL)
with S(CURRENT_DB_URL) as s_current, S(TARGET_DB_URL) as s_target:
m = Migration(s_current, s_target)
m.set_safety(False)
m.add_all_changes()
print('-- Pending (staging -> models):\n\n{}'.format(m.sql))
if write_to_file:
with io.open('DB/migration/pending/pending.sql', 'w') as w:
w.write(m.sql)
def check_migration_result():
with temporary_db() as CURRENT_DB_URL, temporary_db() as TARGET_DB_URL:
load_post_migration_state(CURRENT_DB_URL)
load_from_app_model(TARGET_DB_URL)
with S(CURRENT_DB_URL) as s_current, S(TARGET_DB_URL) as s_target:
m = Migration(s_current, s_target)
m.set_safety(False)
m.add_all_changes()
print('Differences:\n{}'.format(m.sql))
def staging_vs_app():
with temporary_db() as CURRENT_DB_URL, temporary_db() as TARGET_DB_URL:
load_current_staging_state(CURRENT_DB_URL)
load_from_app_model(TARGET_DB_URL)
with S(CURRENT_DB_URL) as s_current, S(TARGET_DB_URL) as s_target:
m = Migration(s_current, s_target)
m.set_safety(False)
m.add_all_changes()
print('Differences:\n{}'.format(m.sql))
def staging_vs_prod():
with temporary_db() as CURRENT_DB_URL, temporary_db() as TARGET_DB_URL:
load_current_staging_state(CURRENT_DB_URL)
load_current_production_state(TARGET_DB_URL)
with S(CURRENT_DB_URL) as s_current, S(TARGET_DB_URL) as s_target:
m = Migration(s_current, s_target)
m.set_safety(False)
m.add_all_changes()
print('Differences:\n{}'.format(m.sql))
def prod_vs_app():
with temporary_db() as CURRENT_DB_URL, temporary_db() as TARGET_DB_URL:
load_current_production_state(CURRENT_DB_URL)
load_from_app_model(TARGET_DB_URL)
with S(CURRENT_DB_URL) as s_current, S(TARGET_DB_URL) as s_target:
m = Migration(s_current, s_target)
m.set_safety(False)
m.add_all_changes()
print('Differences:\n{}'.format(m.sql))
def staging_errors():
with temporary_db() as CURRENT_DB_URL, temporary_db() as TARGET_DB_URL:
load_current_staging_state(CURRENT_DB_URL)
load_current_production_state(TARGET_DB_URL)
with S(CURRENT_DB_URL) as s_current, S(TARGET_DB_URL) as s_target:
m = Migration(s_current, s_target)
m.set_safety(False)
m.add_all_changes()
print('Differences:\n{}'.format(m.sql))
def get_current_app_db_url():
app = create_app(os.getenv('DM_ENVIRONMENT') or 'development')
return app.config['SQLALCHEMY_DATABASE_URI']
if __name__ == '__main__':
try:
task_method = getattr(sys.modules[__name__], sys.argv[1])
except AttributeError:
print('no such task')
sys.exit(1)
task_method(*sys.argv[2:])