-
Notifications
You must be signed in to change notification settings - Fork 4
/
schema_change.py
314 lines (231 loc) · 8.39 KB
/
schema_change.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#!/usr/bin/env python3
import os
import codecs
from glob import glob
import yaml
import argparse
import pymysql.cursors
import psycopg2.extras
import psycopg2
# Parse arguments
parser = argparse.ArgumentParser()
parser.add_argument("-c", "--config", type=str, help="Config file location (default: ~/.dbschema.yml)")
parser.add_argument("-t", "--tag", type=str, help="Database tag")
parser.add_argument("-r", "--rollback", type=str, help="Rollback a migration")
args = parser.parse_args()
def getConfig():
"""
Get config file
"""
# Set location
configPath = os.path.expanduser('~') + '/.dbschema.yml'
if args.config:
configPath = args.config
# Check if the config file exists
checExists(configPath)
# Load config
with open(configPath) as f:
# use safe_load instead load
config = yaml.safe_load(f)
return config
def checExists(path, type='file'):
"""
Check if a file or a folder exists
"""
if type == 'file':
if not os.path.isfile(path):
raise RuntimeError('The file `%s` does not exist.' % path)
else:
if not os.path.isdir(path):
raise RuntimeError('The folder `%s` does not exist.' % path)
def getMigrationsFiles(path):
"""
List migrations folders
"""
return glob(path + '*/up.sql')
def getMigrationName(file):
"""
Returns the migration name, for example:
`/path/to/migrations/migration1/up.sql` -> `migration1`
"""
return os.path.basename(os.path.dirname(file))
def getMigrationSource(file):
"""
Returns migration source code
"""
with open(file, "r") as f:
return f.read()
def getConnection(engine, host, user, port, password, database):
"""
Returns a PostgreSQL or MySQL connection
"""
if engine == 'mysql':
# Connection
return getMysqlConnection(host, user, port, password, database)
elif engine == 'postgresql':
# Connection
return getPgConnection(host, user, port, password, database)
else:
raise RuntimeError('`%s` is not a valid engine.' % engine)
def getMysqlConnection(host, user, port, password, database):
"""
MySQL connection
"""
connection = pymysql.connect(host=host,
user=user,
port=port,
password=password,
db=database,
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
return connection
def getPgConnection(host, user, port, password, database):
"""
PostgreSQL connection
"""
connection = psycopg2.connect(host=host,
user=user,
port=port,
password=password,
dbname=database)
return connection
def runMigration(connection, queries):
"""
Apply a migration to the SQL server
"""
# Execute query
with connection.cursor() as cursorMig:
cursorMig.execute(queries)
connection.commit()
def saveMigration(connection, basename):
"""
Save a migration in `migrations_applied` table
"""
# Prepare query
sql = "INSERT INTO migrations_applied (name, date) VALUES (%s, NOW())"
# Run
with connection.cursor() as cursor:
cursor.execute(sql, (basename,))
connection.commit()
def deleteMigration(connection, basename):
"""
Delete a migration in `migrations_applied` table
"""
# Prepare query
sql = "DELETE FROM migrations_applied WHERE name = %s"
# Run
with connection.cursor() as cursor:
cursor.execute(sql, (basename,))
connection.commit()
def isApplied(migrationsApplied, migrationName):
"""
Check if a migration we want to run is already in the list of applied migrations
"""
return [True for migrationApplied in migrationsApplied if migrationApplied['name'] == migrationName]
def getMigrationsApplied(engine, connection):
"""
Get list of migrations already applied
"""
try:
# Get cursor based on engine
if engine == 'postgresql':
cursor = connection.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
else:
cursor = connection.cursor()
sql = "SELECT id, name, date FROM migrations_applied"
cursor.execute(sql)
rows = cursor.fetchall()
# print (rows);
return rows
except psycopg2.ProgrammingError:
raise RuntimeError('The table `migrations_applied` is missing. Please refer to the project documentation at https://github.com/gabfl/dbschema.')
except pymysql.err.ProgrammingError:
raise RuntimeError('The table `migrations_applied` is missing. Please refer to the project documentation at https://github.com/gabfl/dbschema.')
def applyMigrations(engine, connection, path):
"""
Apply all migrations in a chronological order
"""
# Get migrations applied
migrationsApplied = getMigrationsApplied(engine, connection)
# print(migrationsApplied)
# Get migrations folder
for file in getMigrationsFiles(path):
# Set vars
basename = os.path.basename(os.path.dirname(file))
# Skip migrations if they are already applied
if isApplied(migrationsApplied, basename):
continue
# Get migration source
source = getMigrationSource(file)
# print (source);
# Run migration
runMigration(connection, source)
# Save migration
saveMigration(connection, basename)
# Log
print(' -> Migration `%s` applied' % (basename))
# Log
print(' * Migrations applied')
def rollbackMigration(engine, connection, path, migrationToRollback):
"""
Rollback a migration
"""
# Get migrations applied
migrationsApplied = getMigrationsApplied(engine, connection)
# Ensure that the migration was previously applied
if not isApplied(migrationsApplied, migrationToRollback):
raise RuntimeError('`%s` is not in the list of previously applied migrations.' % (migrationToRollback))
# Rollback file
file = path + migrationToRollback + '/down.sql'
# Ensure that the file exists
checExists(file)
# Set vars
basename = os.path.basename(os.path.dirname(file))
# Get migration source
source = getMigrationSource(file)
# print (source);
# Run migration rollback
runMigration(connection, source)
# Delete migration
deleteMigration(connection, basename)
# Log
print(' -> Migration `%s` has been rolled back' % (basename))
def main():
# Load config
config = getConfig()
databases = config['databases']
# If we are rolling back, ensure that we have a database tag
if args.rollback and not args.tag:
raise RuntimeError('To rollback a migration you need to specify the database tag with `--tag`')
for database in databases:
# If a tag is specified, skip other tags
if args.tag and args.tag != database:
continue
# Set vars
engine = databases[database].get('engine', 'mysql')
host = databases[database].get('127.0.0.1', 'localhost')
port = databases[database].get('port', 3306)
user = databases[database].get('user')
password = databases[database].get('password')
db = databases[database].get('db')
path = databases[database].get('path')
preMigration = databases[database].get('pre_migration')
postMigration = databases[database].get('post_migration')
# Check if the migration path exists
checExists(path, 'dir')
# Get database connection
connection = getConnection(engine, host, user, port, password, db)
# Run pre migration queries
if preMigration:
runMigration(connection, preMigration)
if args.rollback:
print(' * Rolling back %s -> `%s`' % (engine, db))
rollbackMigration(engine, connection, path, args.rollback)
else:
print(' * Applying migrations for %s -> `%s`' % (engine, db))
applyMigrations(engine, connection, path)
# Run post migration queries
if postMigration:
runMigration(connection, postMigration)
if __name__ == "__main__":
main()