def _walk_versions(self, engine=None, snake_walk=False, downgrade=True, initial_version=None): # Determine latest version script from the repo, then # upgrade from 1 through to the latest, with no data # in the databases. This just checks that the schema itself # upgrades successfully. def db_version(): return migration_api.db_version(engine, TestMigrations.REPOSITORY) # Place the database under version control init_version = migration.INIT_VERSION if initial_version is not None: init_version = initial_version migration_api.version_control(engine, TestMigrations.REPOSITORY, init_version) self.assertEqual(init_version, db_version()) migration_api.upgrade(engine, TestMigrations.REPOSITORY, init_version + 1) self.assertEqual(init_version + 1, db_version()) LOG.debug('latest version is %s' % TestMigrations.REPOSITORY.latest) for version in xrange(init_version + 2, TestMigrations.REPOSITORY.latest + 1): # upgrade -> downgrade -> upgrade self._migrate_up(engine, version, with_data=True) if snake_walk: self._migrate_down(engine, version - 1, with_data=True) self._migrate_up(engine, version) if downgrade: # Now walk it back down to 0 from the latest, testing # the downgrade paths. for version in reversed( xrange(init_version + 2, TestMigrations.REPOSITORY.latest + 1)): # downgrade -> upgrade -> downgrade self._migrate_down(engine, version - 1) if snake_walk: self._migrate_up(engine, version) self._migrate_down(engine, version - 1) # Ensure we made it all the way back to the first migration self.assertEqual(init_version + 1, db_version())
def _migrate_up(self, engine, version, with_data=False): """migrate up to a new version of the db. We allow for data insertion and post checks at every migration version with special _pre_upgrade_### and _check_### functions in the main test. """ if with_data: data = None pre_upgrade = getattr(self, "_pre_upgrade_%3.3d" % version, None) if pre_upgrade: data = pre_upgrade(engine) migration_api.upgrade(engine, TestMigrations.REPOSITORY, version) self.assertEqual( version, migration_api.db_version(engine, TestMigrations.REPOSITORY)) if with_data: check = getattr(self, "_check_%3.3d" % version, None) if check: check(engine, data)
def _migrate_up(self, engine, version, with_data=False): """migrate up to a new version of the db. We allow for data insertion and post checks at every migration version with special _pre_upgrade_### and _check_### functions in the main test. """ if with_data: data = None pre_upgrade = getattr(self, "_pre_upgrade_%3.3d" % version, None) if pre_upgrade: data = pre_upgrade(engine) migration_api.upgrade(engine, TestMigrations.REPOSITORY, version) self.assertEqual(version, migration_api.db_version(engine, TestMigrations.REPOSITORY)) if with_data: check = getattr(self, "_check_%3.3d" % version, None) if check: check(engine, data)