mbligh | fa29a2a | 2008-05-16 22:48:09 +0000 | [diff] [blame] | 1 | #!/usr/bin/python -u |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 2 | |
Dennis Jeffrey | d65bbd1 | 2013-03-05 10:59:18 -0800 | [diff] [blame] | 3 | import os, sys, re, tempfile |
mbligh | b090f14 | 2008-02-27 21:33:46 +0000 | [diff] [blame] | 4 | from optparse import OptionParser |
mbligh | 9b907d6 | 2008-05-13 17:56:24 +0000 | [diff] [blame] | 5 | import common |
Dennis Jeffrey | d65bbd1 | 2013-03-05 10:59:18 -0800 | [diff] [blame] | 6 | from autotest_lib.client.common_lib import utils |
showard | 0e73c85 | 2008-10-03 10:15:50 +0000 | [diff] [blame] | 7 | from autotest_lib.database import database_connection |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 8 | |
| 9 | MIGRATE_TABLE = 'migrate_info' |
showard | 50c0e71 | 2008-09-22 16:20:37 +0000 | [diff] [blame] | 10 | |
| 11 | _AUTODIR = os.path.join(os.path.dirname(__file__), '..') |
| 12 | _MIGRATIONS_DIRS = { |
| 13 | 'AUTOTEST_WEB' : os.path.join(_AUTODIR, 'frontend', 'migrations'), |
| 14 | 'TKO' : os.path.join(_AUTODIR, 'tko', 'migrations'), |
| 15 | } |
| 16 | _DEFAULT_MIGRATIONS_DIR = 'migrations' # use CWD |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 17 | |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 18 | class Migration(object): |
Dennis Jeffrey | d65bbd1 | 2013-03-05 10:59:18 -0800 | [diff] [blame] | 19 | """Represents a database migration.""" |
showard | eca1f21 | 2009-05-13 20:28:12 +0000 | [diff] [blame] | 20 | _UP_ATTRIBUTES = ('migrate_up', 'UP_SQL') |
| 21 | _DOWN_ATTRIBUTES = ('migrate_down', 'DOWN_SQL') |
| 22 | |
| 23 | def __init__(self, name, version, module): |
| 24 | self.name = name |
| 25 | self.version = version |
| 26 | self.module = module |
| 27 | self._check_attributes(self._UP_ATTRIBUTES) |
| 28 | self._check_attributes(self._DOWN_ATTRIBUTES) |
| 29 | |
| 30 | |
| 31 | @classmethod |
| 32 | def from_file(cls, filename): |
Dennis Jeffrey | d65bbd1 | 2013-03-05 10:59:18 -0800 | [diff] [blame] | 33 | """Instantiates a Migration from a file. |
| 34 | |
| 35 | @param filename: Name of a migration file. |
| 36 | |
| 37 | @return An instantiated Migration object. |
| 38 | |
| 39 | """ |
showard | eca1f21 | 2009-05-13 20:28:12 +0000 | [diff] [blame] | 40 | version = int(filename[:3]) |
| 41 | name = filename[:-3] |
| 42 | module = __import__(name, globals(), locals(), []) |
| 43 | return cls(name, version, module) |
| 44 | |
| 45 | |
| 46 | def _check_attributes(self, attributes): |
| 47 | method_name, sql_name = attributes |
| 48 | assert (hasattr(self.module, method_name) or |
| 49 | hasattr(self.module, sql_name)) |
| 50 | |
| 51 | |
| 52 | def _execute_migration(self, attributes, manager): |
| 53 | method_name, sql_name = attributes |
| 54 | method = getattr(self.module, method_name, None) |
| 55 | if method: |
| 56 | assert callable(method) |
| 57 | method(manager) |
| 58 | else: |
| 59 | sql = getattr(self.module, sql_name) |
| 60 | assert isinstance(sql, basestring) |
| 61 | manager.execute_script(sql) |
showard | decbe50 | 2008-03-28 16:31:10 +0000 | [diff] [blame] | 62 | |
| 63 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 64 | def migrate_up(self, manager): |
Dennis Jeffrey | d65bbd1 | 2013-03-05 10:59:18 -0800 | [diff] [blame] | 65 | """Performs an up migration (to a newer version). |
| 66 | |
| 67 | @param manager: A MigrationManager object. |
| 68 | |
| 69 | """ |
showard | eca1f21 | 2009-05-13 20:28:12 +0000 | [diff] [blame] | 70 | self._execute_migration(self._UP_ATTRIBUTES, manager) |
showard | decbe50 | 2008-03-28 16:31:10 +0000 | [diff] [blame] | 71 | |
| 72 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 73 | def migrate_down(self, manager): |
Dennis Jeffrey | d65bbd1 | 2013-03-05 10:59:18 -0800 | [diff] [blame] | 74 | """Performs a down migration (to an older version). |
| 75 | |
| 76 | @param manager: A MigrationManager object. |
| 77 | |
| 78 | """ |
showard | eca1f21 | 2009-05-13 20:28:12 +0000 | [diff] [blame] | 79 | self._execute_migration(self._DOWN_ATTRIBUTES, manager) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 80 | |
| 81 | |
| 82 | class MigrationManager(object): |
Dennis Jeffrey | d65bbd1 | 2013-03-05 10:59:18 -0800 | [diff] [blame] | 83 | """Managest database migrations.""" |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 84 | connection = None |
| 85 | cursor = None |
| 86 | migrations_dir = None |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 87 | |
showard | 0e73c85 | 2008-10-03 10:15:50 +0000 | [diff] [blame] | 88 | def __init__(self, database_connection, migrations_dir=None, force=False): |
| 89 | self._database = database_connection |
showard | 50c0e71 | 2008-09-22 16:20:37 +0000 | [diff] [blame] | 90 | self.force = force |
showard | 12454c6 | 2010-01-15 19:15:14 +0000 | [diff] [blame] | 91 | # A boolean, this will only be set to True if this migration should be |
| 92 | # simulated rather than actually taken. For use with migrations that |
| 93 | # may make destructive queries |
| 94 | self.simulate = False |
showard | 0e73c85 | 2008-10-03 10:15:50 +0000 | [diff] [blame] | 95 | self._set_migrations_dir(migrations_dir) |
| 96 | |
| 97 | |
| 98 | def _set_migrations_dir(self, migrations_dir=None): |
jamesren | 92afa56 | 2010-03-23 00:19:26 +0000 | [diff] [blame] | 99 | config_section = self._config_section() |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 100 | if migrations_dir is None: |
showard | 50c0e71 | 2008-09-22 16:20:37 +0000 | [diff] [blame] | 101 | migrations_dir = os.path.abspath( |
showard | 0e73c85 | 2008-10-03 10:15:50 +0000 | [diff] [blame] | 102 | _MIGRATIONS_DIRS.get(config_section, _DEFAULT_MIGRATIONS_DIR)) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 103 | self.migrations_dir = migrations_dir |
| 104 | sys.path.append(migrations_dir) |
showard | 0e73c85 | 2008-10-03 10:15:50 +0000 | [diff] [blame] | 105 | assert os.path.exists(migrations_dir), migrations_dir + " doesn't exist" |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 106 | |
| 107 | |
jamesren | 92afa56 | 2010-03-23 00:19:26 +0000 | [diff] [blame] | 108 | def _config_section(self): |
| 109 | return self._database.global_config_section |
| 110 | |
| 111 | |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 112 | def get_db_name(self): |
Dennis Jeffrey | d65bbd1 | 2013-03-05 10:59:18 -0800 | [diff] [blame] | 113 | """Gets the database name.""" |
showard | 0e73c85 | 2008-10-03 10:15:50 +0000 | [diff] [blame] | 114 | return self._database.get_database_info()['db_name'] |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 115 | |
| 116 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 117 | def execute(self, query, *parameters): |
Dennis Jeffrey | d65bbd1 | 2013-03-05 10:59:18 -0800 | [diff] [blame] | 118 | """Executes a database query. |
| 119 | |
| 120 | @param query: The query to execute. |
| 121 | @param parameters: Associated parameters for the query. |
| 122 | |
| 123 | @return The result of the query. |
| 124 | |
| 125 | """ |
showard | 0e73c85 | 2008-10-03 10:15:50 +0000 | [diff] [blame] | 126 | return self._database.execute(query, parameters) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 127 | |
| 128 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 129 | def execute_script(self, script): |
Dennis Jeffrey | d65bbd1 | 2013-03-05 10:59:18 -0800 | [diff] [blame] | 130 | """Executes a set of database queries. |
| 131 | |
| 132 | @param script: A string of semicolon-separated queries. |
| 133 | |
| 134 | """ |
showard | b1e5187 | 2008-10-07 11:08:18 +0000 | [diff] [blame] | 135 | sql_statements = [statement.strip() |
| 136 | for statement in script.split(';') |
| 137 | if statement.strip()] |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 138 | for statement in sql_statements: |
showard | b1e5187 | 2008-10-07 11:08:18 +0000 | [diff] [blame] | 139 | self.execute(statement) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 140 | |
| 141 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 142 | def check_migrate_table_exists(self): |
Dennis Jeffrey | d65bbd1 | 2013-03-05 10:59:18 -0800 | [diff] [blame] | 143 | """Checks whether the migration table exists.""" |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 144 | try: |
| 145 | self.execute("SELECT * FROM %s" % MIGRATE_TABLE) |
| 146 | return True |
showard | 0e73c85 | 2008-10-03 10:15:50 +0000 | [diff] [blame] | 147 | except self._database.DatabaseError, exc: |
| 148 | # we can't check for more specifics due to differences between DB |
| 149 | # backends (we can't even check for a subclass of DatabaseError) |
| 150 | return False |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 151 | |
| 152 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 153 | def create_migrate_table(self): |
Dennis Jeffrey | d65bbd1 | 2013-03-05 10:59:18 -0800 | [diff] [blame] | 154 | """Creates the migration table.""" |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 155 | if not self.check_migrate_table_exists(): |
| 156 | self.execute("CREATE TABLE %s (`version` integer)" % |
| 157 | MIGRATE_TABLE) |
| 158 | else: |
| 159 | self.execute("DELETE FROM %s" % MIGRATE_TABLE) |
| 160 | self.execute("INSERT INTO %s VALUES (0)" % MIGRATE_TABLE) |
showard | 0e73c85 | 2008-10-03 10:15:50 +0000 | [diff] [blame] | 161 | assert self._database.rowcount == 1 |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 162 | |
| 163 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 164 | def set_db_version(self, version): |
Dennis Jeffrey | d65bbd1 | 2013-03-05 10:59:18 -0800 | [diff] [blame] | 165 | """Sets the database version. |
| 166 | |
| 167 | @param version: The version to which to set the database. |
| 168 | |
| 169 | """ |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 170 | assert isinstance(version, int) |
| 171 | self.execute("UPDATE %s SET version=%%s" % MIGRATE_TABLE, |
| 172 | version) |
showard | 0e73c85 | 2008-10-03 10:15:50 +0000 | [diff] [blame] | 173 | assert self._database.rowcount == 1 |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 174 | |
| 175 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 176 | def get_db_version(self): |
Dennis Jeffrey | d65bbd1 | 2013-03-05 10:59:18 -0800 | [diff] [blame] | 177 | """Gets the database version. |
| 178 | |
| 179 | @return The database version. |
| 180 | |
| 181 | """ |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 182 | if not self.check_migrate_table_exists(): |
| 183 | return 0 |
showard | 0e73c85 | 2008-10-03 10:15:50 +0000 | [diff] [blame] | 184 | rows = self.execute("SELECT * FROM %s" % MIGRATE_TABLE) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 185 | if len(rows) == 0: |
| 186 | return 0 |
| 187 | assert len(rows) == 1 and len(rows[0]) == 1 |
| 188 | return rows[0][0] |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 189 | |
| 190 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 191 | def get_migrations(self, minimum_version=None, maximum_version=None): |
Dennis Jeffrey | d65bbd1 | 2013-03-05 10:59:18 -0800 | [diff] [blame] | 192 | """Gets the list of migrations to perform. |
| 193 | |
| 194 | @param minimum_version: The minimum database version. |
| 195 | @param maximum_version: The maximum database version. |
| 196 | |
| 197 | @return A list of Migration objects. |
| 198 | |
| 199 | """ |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 200 | migrate_files = [filename for filename |
| 201 | in os.listdir(self.migrations_dir) |
| 202 | if re.match(r'^\d\d\d_.*\.py$', filename)] |
| 203 | migrate_files.sort() |
showard | eca1f21 | 2009-05-13 20:28:12 +0000 | [diff] [blame] | 204 | migrations = [Migration.from_file(filename) |
| 205 | for filename in migrate_files] |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 206 | if minimum_version is not None: |
| 207 | migrations = [migration for migration in migrations |
| 208 | if migration.version >= minimum_version] |
| 209 | if maximum_version is not None: |
| 210 | migrations = [migration for migration in migrations |
| 211 | if migration.version <= maximum_version] |
| 212 | return migrations |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 213 | |
| 214 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 215 | def do_migration(self, migration, migrate_up=True): |
Dennis Jeffrey | d65bbd1 | 2013-03-05 10:59:18 -0800 | [diff] [blame] | 216 | """Performs a migration. |
| 217 | |
| 218 | @param migration: The Migration to perform. |
| 219 | @param migrate_up: Whether to migrate up (if not, then migrates down). |
| 220 | |
| 221 | """ |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 222 | print 'Applying migration %s' % migration.name, # no newline |
| 223 | if migrate_up: |
| 224 | print 'up' |
| 225 | assert self.get_db_version() == migration.version - 1 |
| 226 | migration.migrate_up(self) |
| 227 | new_version = migration.version |
| 228 | else: |
| 229 | print 'down' |
| 230 | assert self.get_db_version() == migration.version |
| 231 | migration.migrate_down(self) |
| 232 | new_version = migration.version - 1 |
| 233 | self.set_db_version(new_version) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 234 | |
| 235 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 236 | def migrate_to_version(self, version): |
Dennis Jeffrey | d65bbd1 | 2013-03-05 10:59:18 -0800 | [diff] [blame] | 237 | """Performs a migration to a specified version. |
| 238 | |
| 239 | @param version: The version to which to migrate the database. |
| 240 | |
| 241 | """ |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 242 | current_version = self.get_db_version() |
jamesren | 92afa56 | 2010-03-23 00:19:26 +0000 | [diff] [blame] | 243 | if current_version == 0 and self._config_section() == 'AUTOTEST_WEB': |
| 244 | self._migrate_from_base() |
| 245 | current_version = self.get_db_version() |
| 246 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 247 | if current_version < version: |
| 248 | lower, upper = current_version, version |
| 249 | migrate_up = True |
| 250 | else: |
| 251 | lower, upper = version, current_version |
| 252 | migrate_up = False |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 253 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 254 | migrations = self.get_migrations(lower + 1, upper) |
| 255 | if not migrate_up: |
| 256 | migrations.reverse() |
| 257 | for migration in migrations: |
| 258 | self.do_migration(migration, migrate_up) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 259 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 260 | assert self.get_db_version() == version |
| 261 | print 'At version', version |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 262 | |
| 263 | |
jamesren | 92afa56 | 2010-03-23 00:19:26 +0000 | [diff] [blame] | 264 | def _migrate_from_base(self): |
| 265 | self.confirm_initialization() |
| 266 | |
| 267 | migration_script = utils.read_file( |
| 268 | os.path.join(os.path.dirname(__file__), 'schema_051.sql')) |
jamesren | ef69298 | 2010-04-05 18:14:07 +0000 | [diff] [blame] | 269 | migration_script = migration_script % ( |
| 270 | dict(username=self._database.get_database_info()['username'])) |
jamesren | 92afa56 | 2010-03-23 00:19:26 +0000 | [diff] [blame] | 271 | self.execute_script(migration_script) |
| 272 | |
| 273 | self.create_migrate_table() |
| 274 | self.set_db_version(51) |
| 275 | |
| 276 | |
| 277 | def confirm_initialization(self): |
Dennis Jeffrey | d65bbd1 | 2013-03-05 10:59:18 -0800 | [diff] [blame] | 278 | """Confirms with the user that we should initialize the database. |
| 279 | |
| 280 | @raises Exception, if the user chooses to abort the migration. |
| 281 | |
| 282 | """ |
jamesren | 92afa56 | 2010-03-23 00:19:26 +0000 | [diff] [blame] | 283 | if not self.force: |
| 284 | response = raw_input( |
| 285 | 'Your %s database does not appear to be initialized. Do you ' |
| 286 | 'want to recreate it (this will result in loss of any existing ' |
| 287 | 'data) (yes/No)? ' % self.get_db_name()) |
| 288 | if response != 'yes': |
| 289 | raise Exception('User has chosen to abort migration') |
| 290 | |
| 291 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 292 | def get_latest_version(self): |
Dennis Jeffrey | d65bbd1 | 2013-03-05 10:59:18 -0800 | [diff] [blame] | 293 | """Gets the latest database version.""" |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 294 | migrations = self.get_migrations() |
| 295 | return migrations[-1].version |
showard | d2d4e2c | 2008-03-12 21:32:46 +0000 | [diff] [blame] | 296 | |
| 297 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 298 | def migrate_to_latest(self): |
Dennis Jeffrey | d65bbd1 | 2013-03-05 10:59:18 -0800 | [diff] [blame] | 299 | """Migrates the database to the latest version.""" |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 300 | latest_version = self.get_latest_version() |
| 301 | self.migrate_to_version(latest_version) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 302 | |
| 303 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 304 | def initialize_test_db(self): |
Dennis Jeffrey | d65bbd1 | 2013-03-05 10:59:18 -0800 | [diff] [blame] | 305 | """Initializes a test database.""" |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 306 | db_name = self.get_db_name() |
showard | 0e73c85 | 2008-10-03 10:15:50 +0000 | [diff] [blame] | 307 | test_db_name = 'test_' + db_name |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 308 | # first, connect to no DB so we can create a test DB |
showard | 0e73c85 | 2008-10-03 10:15:50 +0000 | [diff] [blame] | 309 | self._database.connect(db_name='') |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 310 | print 'Creating test DB', test_db_name |
| 311 | self.execute('CREATE DATABASE ' + test_db_name) |
showard | 0e73c85 | 2008-10-03 10:15:50 +0000 | [diff] [blame] | 312 | self._database.disconnect() |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 313 | # now connect to the test DB |
showard | 0e73c85 | 2008-10-03 10:15:50 +0000 | [diff] [blame] | 314 | self._database.connect(db_name=test_db_name) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 315 | |
| 316 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 317 | def remove_test_db(self): |
Dennis Jeffrey | d65bbd1 | 2013-03-05 10:59:18 -0800 | [diff] [blame] | 318 | """Removes a test database.""" |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 319 | print 'Removing test DB' |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 320 | self.execute('DROP DATABASE ' + self.get_db_name()) |
showard | 0e73c85 | 2008-10-03 10:15:50 +0000 | [diff] [blame] | 321 | # reset connection back to real DB |
| 322 | self._database.disconnect() |
| 323 | self._database.connect() |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 324 | |
| 325 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 326 | def get_mysql_args(self): |
Dennis Jeffrey | d65bbd1 | 2013-03-05 10:59:18 -0800 | [diff] [blame] | 327 | """Returns the mysql arguments as a string.""" |
showard | 0e73c85 | 2008-10-03 10:15:50 +0000 | [diff] [blame] | 328 | return ('-u %(username)s -p%(password)s -h %(host)s %(db_name)s' % |
| 329 | self._database.get_database_info()) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 330 | |
| 331 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 332 | def migrate_to_version_or_latest(self, version): |
Dennis Jeffrey | d65bbd1 | 2013-03-05 10:59:18 -0800 | [diff] [blame] | 333 | """Migrates to either a specified version, or the latest version. |
| 334 | |
| 335 | @param version: The version to which to migrate the database, |
| 336 | or None in order to migrate to the latest version. |
| 337 | |
| 338 | """ |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 339 | if version is None: |
| 340 | self.migrate_to_latest() |
| 341 | else: |
| 342 | self.migrate_to_version(version) |
mbligh | aa383b7 | 2008-03-12 20:11:56 +0000 | [diff] [blame] | 343 | |
| 344 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 345 | def do_sync_db(self, version=None): |
Dennis Jeffrey | d65bbd1 | 2013-03-05 10:59:18 -0800 | [diff] [blame] | 346 | """Migrates the database. |
| 347 | |
| 348 | @param version: The version to which to migrate the database. |
| 349 | |
| 350 | """ |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 351 | print 'Migration starting for database', self.get_db_name() |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 352 | self.migrate_to_version_or_latest(version) |
| 353 | print 'Migration complete' |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 354 | |
| 355 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 356 | def test_sync_db(self, version=None): |
Dennis Jeffrey | d65bbd1 | 2013-03-05 10:59:18 -0800 | [diff] [blame] | 357 | """Create a fresh database and run all migrations on it. |
| 358 | |
| 359 | @param version: The version to which to migrate the database. |
| 360 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 361 | """ |
| 362 | self.initialize_test_db() |
| 363 | try: |
showard | eab66ce | 2009-12-23 00:03:56 +0000 | [diff] [blame] | 364 | print 'Starting migration test on DB', self.get_db_name() |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 365 | self.migrate_to_version_or_latest(version) |
| 366 | # show schema to the user |
| 367 | os.system('mysqldump %s --no-data=true ' |
| 368 | '--add-drop-table=false' % |
| 369 | self.get_mysql_args()) |
| 370 | finally: |
| 371 | self.remove_test_db() |
| 372 | print 'Test finished successfully' |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 373 | |
| 374 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 375 | def simulate_sync_db(self, version=None): |
Dennis Jeffrey | d65bbd1 | 2013-03-05 10:59:18 -0800 | [diff] [blame] | 376 | """Creates a fresh DB, copies existing DB to it, then synchronizes it. |
| 377 | |
| 378 | @param version: The version to which to migrate the database. |
| 379 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 380 | """ |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 381 | db_version = self.get_db_version() |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 382 | # don't do anything if we're already at the latest version |
| 383 | if db_version == self.get_latest_version(): |
| 384 | print 'Skipping simulation, already at latest version' |
| 385 | return |
| 386 | # get existing data |
showard | 12454c6 | 2010-01-15 19:15:14 +0000 | [diff] [blame] | 387 | self.initialize_and_fill_test_db() |
| 388 | try: |
| 389 | print 'Starting migration test on DB', self.get_db_name() |
| 390 | self.migrate_to_version_or_latest(version) |
| 391 | finally: |
| 392 | self.remove_test_db() |
| 393 | print 'Test finished successfully' |
| 394 | |
| 395 | |
| 396 | def initialize_and_fill_test_db(self): |
Dennis Jeffrey | d65bbd1 | 2013-03-05 10:59:18 -0800 | [diff] [blame] | 397 | """Initializes and fills up a test database.""" |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 398 | print 'Dumping existing data' |
| 399 | dump_fd, dump_file = tempfile.mkstemp('.migrate_dump') |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 400 | os.system('mysqldump %s >%s' % |
| 401 | (self.get_mysql_args(), dump_file)) |
| 402 | # fill in test DB |
| 403 | self.initialize_test_db() |
| 404 | print 'Filling in test DB' |
| 405 | os.system('mysql %s <%s' % (self.get_mysql_args(), dump_file)) |
showard | 0e73c85 | 2008-10-03 10:15:50 +0000 | [diff] [blame] | 406 | os.close(dump_fd) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 407 | os.remove(dump_file) |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 408 | |
| 409 | |
mbligh | c2f2445 | 2008-03-31 16:46:13 +0000 | [diff] [blame] | 410 | USAGE = """\ |
| 411 | %s [options] sync|test|simulate|safesync [version] |
| 412 | Options: |
| 413 | -d --database Which database to act on |
Dennis Jeffrey | d65bbd1 | 2013-03-05 10:59:18 -0800 | [diff] [blame] | 414 | -f --force Don't ask for confirmation |
| 415 | --debug Print all DB queries"""\ |
mbligh | c2f2445 | 2008-03-31 16:46:13 +0000 | [diff] [blame] | 416 | % sys.argv[0] |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 417 | |
| 418 | |
| 419 | def main(): |
Dennis Jeffrey | d65bbd1 | 2013-03-05 10:59:18 -0800 | [diff] [blame] | 420 | """Main function for the migration script.""" |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 421 | parser = OptionParser() |
| 422 | parser.add_option("-d", "--database", |
| 423 | help="which database to act on", |
jamesren | 92afa56 | 2010-03-23 00:19:26 +0000 | [diff] [blame] | 424 | dest="database", |
| 425 | default="AUTOTEST_WEB") |
showard | 50c0e71 | 2008-09-22 16:20:37 +0000 | [diff] [blame] | 426 | parser.add_option("-f", "--force", help="don't ask for confirmation", |
| 427 | action="store_true") |
showard | 049b0fd | 2009-05-29 18:39:07 +0000 | [diff] [blame] | 428 | parser.add_option('--debug', help='print all DB queries', |
| 429 | action='store_true') |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 430 | (options, args) = parser.parse_args() |
showard | 250d84d | 2010-01-12 21:59:48 +0000 | [diff] [blame] | 431 | manager = get_migration_manager(db_name=options.database, |
| 432 | debug=options.debug, force=options.force) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 433 | |
| 434 | if len(args) > 0: |
| 435 | if len(args) > 1: |
| 436 | version = int(args[1]) |
| 437 | else: |
| 438 | version = None |
| 439 | if args[0] == 'sync': |
| 440 | manager.do_sync_db(version) |
| 441 | elif args[0] == 'test': |
showard | 12454c6 | 2010-01-15 19:15:14 +0000 | [diff] [blame] | 442 | manager.simulate=True |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 443 | manager.test_sync_db(version) |
| 444 | elif args[0] == 'simulate': |
showard | 12454c6 | 2010-01-15 19:15:14 +0000 | [diff] [blame] | 445 | manager.simulate=True |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 446 | manager.simulate_sync_db(version) |
| 447 | elif args[0] == 'safesync': |
| 448 | print 'Simluating migration' |
showard | 12454c6 | 2010-01-15 19:15:14 +0000 | [diff] [blame] | 449 | manager.simulate=True |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 450 | manager.simulate_sync_db(version) |
| 451 | print 'Performing real migration' |
showard | 12454c6 | 2010-01-15 19:15:14 +0000 | [diff] [blame] | 452 | manager.simulate=False |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 453 | manager.do_sync_db(version) |
| 454 | else: |
| 455 | print USAGE |
| 456 | return |
| 457 | |
| 458 | print USAGE |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 459 | |
| 460 | |
showard | 250d84d | 2010-01-12 21:59:48 +0000 | [diff] [blame] | 461 | def get_migration_manager(db_name, debug, force): |
Dennis Jeffrey | d65bbd1 | 2013-03-05 10:59:18 -0800 | [diff] [blame] | 462 | """Creates a MigrationManager object. |
| 463 | |
| 464 | @param db_name: The database name. |
| 465 | @param debug: Whether to print debug messages. |
| 466 | @param force: Whether to force migration without asking for confirmation. |
| 467 | |
| 468 | @return A created MigrationManager object. |
| 469 | |
| 470 | """ |
showard | 250d84d | 2010-01-12 21:59:48 +0000 | [diff] [blame] | 471 | database = database_connection.DatabaseConnection(db_name) |
| 472 | database.debug = debug |
| 473 | database.reconnect_enabled = False |
| 474 | database.connect() |
| 475 | return MigrationManager(database, force=force) |
| 476 | |
| 477 | |
mbligh | e8819cd | 2008-02-15 16:48:40 +0000 | [diff] [blame] | 478 | if __name__ == '__main__': |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 479 | main() |