summaryrefslogtreecommitdiff
path: root/migrate/migrate.py
blob: 8bbcb5f970c5368a9d4d3f1da3e74cea525e4603 (plain)
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
315
316
317
318
#!/usr/bin/python -u

import os, sys, re, subprocess, tempfile
import MySQLdb, MySQLdb.constants.ER
from optparse import OptionParser
import common
from autotest_lib.client.common_lib import global_config

MIGRATE_TABLE = 'migrate_info'
DEFAULT_MIGRATIONS_DIR = 'migrations'

class Migration(object):
	def __init__(self, filename):
		self.version = int(filename[:3])
		self.name = filename[:-3]
		self.module = __import__(self.name, globals(), locals(), [])
		assert hasattr(self.module, 'migrate_up')
		assert hasattr(self.module, 'migrate_down')


	def migrate_up(self, manager):
		self.module.migrate_up(manager)


	def migrate_down(self, manager):
		self.module.migrate_down(manager)


class MigrationManager(object):
	connection = None
	cursor = None
	migrations_dir = None

	def __init__(self, database, migrations_dir=None):
		self.database = database
		if migrations_dir is None:
			migrations_dir = os.path.abspath(DEFAULT_MIGRATIONS_DIR)
		self.migrations_dir = migrations_dir
		sys.path.append(migrations_dir)
		assert os.path.exists(migrations_dir)

		self.db_host = None
		self.db_name = None
		self.username = None
		self.password = None


	def read_db_info(self):
		# grab the config file and parse for info
		c = global_config.global_config
		self.db_host = c.get_config_value(self.database, "host")
		self.db_name = c.get_config_value(self.database, "database")
		self.username = c.get_config_value(self.database, "user")
		self.password = c.get_config_value(self.database, "password")


	def connect(self, host, db_name, username, password):
		return MySQLdb.connect(host=host, db=db_name, user=username,
				       passwd=password)


	def open_connection(self):
		self.connection = self.connect(self.db_host, self.db_name,
					       self.username, self.password)
		self.connection.autocommit(True)
		self.cursor = self.connection.cursor()


	def close_connection(self):
		self.connection.close()


	def execute(self, query, *parameters):
		#print 'SQL:', query % parameters
		return self.cursor.execute(query, parameters)


	def execute_script(self, script):
		sql_statements = [statement.strip() for statement
				  in script.split(';')]
		for statement in sql_statements:
			if statement:
				self.execute(statement)


	def check_migrate_table_exists(self):
		try:
			self.execute("SELECT * FROM %s" % MIGRATE_TABLE)
			return True
		except MySQLdb.ProgrammingError, exc:
			error_code, _ = exc.args
			if error_code == MySQLdb.constants.ER.NO_SUCH_TABLE:
				return False
			raise


	def create_migrate_table(self):
		if not self.check_migrate_table_exists():
			self.execute("CREATE TABLE %s (`version` integer)" %
				     MIGRATE_TABLE)
		else:
			self.execute("DELETE FROM %s" % MIGRATE_TABLE)
		self.execute("INSERT INTO %s VALUES (0)" % MIGRATE_TABLE)
		assert self.cursor.rowcount == 1


	def set_db_version(self, version):
		assert isinstance(version, int)
		self.execute("UPDATE %s SET version=%%s" % MIGRATE_TABLE,
			     version)
		assert self.cursor.rowcount == 1


	def get_db_version(self):
		if not self.check_migrate_table_exists():
			return 0
		self.execute("SELECT * FROM %s" % MIGRATE_TABLE)
		rows = self.cursor.fetchall()
		if len(rows) == 0:
			return 0
		assert len(rows) == 1 and len(rows[0]) == 1
		return rows[0][0]


	def get_migrations(self, minimum_version=None, maximum_version=None):
		migrate_files = [filename for filename
				 in os.listdir(self.migrations_dir)
				 if re.match(r'^\d\d\d_.*\.py$', filename)]
		migrate_files.sort()
		migrations = [Migration(filename) for filename in migrate_files]
		if minimum_version is not None:
			migrations = [migration for migration in migrations
				      if migration.version >= minimum_version]
		if maximum_version is not None:
			migrations = [migration for migration in migrations
				      if migration.version <= maximum_version]
		return migrations


	def do_migration(self, migration, migrate_up=True):
		print 'Applying migration %s' % migration.name, # no newline
		if migrate_up:
			print 'up'
			assert self.get_db_version() == migration.version - 1
			migration.migrate_up(self)
			new_version = migration.version
		else:
			print 'down'
			assert self.get_db_version() == migration.version
			migration.migrate_down(self)
			new_version = migration.version - 1
		self.set_db_version(new_version)


	def migrate_to_version(self, version):
		current_version = self.get_db_version()
		if current_version < version:
			lower, upper = current_version, version
			migrate_up = True
		else:
			lower, upper = version, current_version
			migrate_up = False

		migrations = self.get_migrations(lower + 1, upper)
		if not migrate_up:
			migrations.reverse()
		for migration in migrations:
			self.do_migration(migration, migrate_up)

		assert self.get_db_version() == version
		print 'At version', version


	def get_latest_version(self):
		migrations = self.get_migrations()
		return migrations[-1].version


	def migrate_to_latest(self):
		latest_version = self.get_latest_version()
		self.migrate_to_version(latest_version)


	def initialize_test_db(self):
		self.read_db_info()
		test_db_name = 'test_' + self.db_name
		# first, connect to no DB so we can create a test DB
		self.db_name = ''
		self.open_connection()
		print 'Creating test DB', test_db_name
		self.execute('CREATE DATABASE ' + test_db_name)
		self.close_connection()
		# now connect to the test DB
		self.db_name = test_db_name
		self.open_connection()


	def remove_test_db(self):
		print 'Removing test DB'
		self.execute('DROP DATABASE ' + self.db_name)


	def get_mysql_args(self):
		return ('-u %(user)s -p%(password)s -h %(host)s %(db)s' % {
		    'user' : self.username,
		    'password' : self.password,
		    'host' : self.db_host,
		    'db' : self.db_name})


	def migrate_to_version_or_latest(self, version):
		if version is None:
			self.migrate_to_latest()
		else:
			self.migrate_to_version(version)


	def do_sync_db(self, version=None):
		self.read_db_info()
		self.open_connection()
		print 'Migration starting for database', self.db_name
		self.migrate_to_version_or_latest(version)
		print 'Migration complete'


	def test_sync_db(self, version=None):
		"""\
		Create a fresh DB and run all migrations on it.
		"""
		self.initialize_test_db()
		try:
			print 'Starting migration test on DB', self.db_name
			self.migrate_to_version_or_latest(version)
			# show schema to the user
			os.system('mysqldump %s --no-data=true '
				  '--add-drop-table=false' %
				  self.get_mysql_args())
		finally:
			self.remove_test_db()
		print 'Test finished successfully'


	def simulate_sync_db(self, version=None):
		"""\
		Create a fresh DB, copy the existing DB to it, and then
		try to synchronize it.
		"""
		self.read_db_info()
		self.open_connection()
		db_version = self.get_db_version()
		self.close_connection()
		# don't do anything if we're already at the latest version
		if db_version == self.get_latest_version():
			print 'Skipping simulation, already at latest version'
			return
		# get existing data
		self.read_db_info()
		print 'Dumping existing data'
		dump_fd, dump_file = tempfile.mkstemp('.migrate_dump')
		os.close(dump_fd)
		os.system('mysqldump %s >%s' %
			  (self.get_mysql_args(), dump_file))
		# fill in test DB
		self.initialize_test_db()
		print 'Filling in test DB'
		os.system('mysql %s <%s' % (self.get_mysql_args(), dump_file))
		os.remove(dump_file)
		try:
			print 'Starting migration test on DB', self.db_name
			self.migrate_to_version_or_latest(version)
		finally:
			self.remove_test_db()
		print 'Test finished successfully'


USAGE = """\
%s [options] sync|test|simulate|safesync [version]
Options:
    -d --database   Which database to act on
    -a --action     Which action to perform"""\
    % sys.argv[0]


def main():
	parser = OptionParser()
	parser.add_option("-d", "--database",
			  help="which database to act on",
			  dest="database")
	parser.add_option("-a", "--action", help="what action to perform",
			  dest="action")
	(options, args) = parser.parse_args()
	manager = MigrationManager(options.database)
	
	if len(args) > 0:
		if len(args) > 1:
			version = int(args[1])
		else:
			version = None
		if args[0] == 'sync':
			manager.do_sync_db(version)
		elif args[0] == 'test':
			manager.test_sync_db(version)
		elif args[0] == 'simulate':
			manager.simulate_sync_db(version)
		elif args[0] == 'safesync':
			print 'Simluating migration'
			manager.simulate_sync_db(version)
			print 'Performing real migration'
			manager.do_sync_db(version)
		else:
			print USAGE
		return
	
	print USAGE


if __name__ == '__main__':
	main()