def list_migrations(self): """List migrations metadata :rtype dict: :returns: list migrations indexed by version with a dict as value with 'applied' and 'class' as keys """ self.db.select_db(self.db_name) migration_manager = MigrationManager(self.db, self.db_name) return migration_manager.list_migrations(self.migration_app)
def fake_migration(self, target_version): """Fake a migration to a target version by simulating the apply/unapply migrations. :param int target_version: the version to simulate the migration :rtype: bool :returns: True if the migration was successfully faked """ self.db.select_db(self.db_name) migration_manager = MigrationManager(self.db, self.db_name) return migration_manager.fake_migration(self.migration_app, target_version)
def migrate(self, version=None): """Migrate both mysql schema and data :param int version: indicating which version to migrate. None migrates to the lastest version. :rtype bool: :returns: True if any migration has been performed """ self.db.select_db(self.db_name) migration_manager = MigrationManager(self.db, self.db_name) return migration_manager.migrate(self.migration_app, version)
def migrate(self, version=None, **kwargs): """Migrate both mysql schema and data :param int version: indicating which version to migrate. None migrates to the lastest version. :rtype bool: :returns: True if any migration has been performed """ self.db.select_db(self.db_name) migration_manager = MigrationManager(self.db, self.db_name) return migration_manager.migrate(self.migration_app, version, **kwargs)
def test_apps(self): manager = MigrationManager(self.db, self.db_name, 'test_migrations') for v in manager.migrations.keys(): del manager.migrations[v] self.assertEquals(manager.apps(), []) self._register_do_nothing_migrations(self.app) self._register_do_nothing_migrations('la_cena') self.assertEquals(manager.apps(), ['la_cena', self.app]) for v in manager.migrations.keys(): del manager.migrations[v]
def _register_do_nothing_migrations(self, app): # Register a do-nothing migration into app @migration(app, 1) class DoNothingMigration(Migration): @classmethod def apply(cls, cur, **kwargs): cur.execute('SELECT 1 FROM test_migrations') @classmethod def unapply(cls, cur): cur.execute('SELECT 2 FROM test_migrations') MigrationManager.add_migration(app, 2, DoNothingMigration)
def migrate(self, version=None, **kwargs): """Migrate directory schema and data :param int version: indicating which version to migrate. None migrates to the lastest version. :param named lp: samba Loadparm context :param named cred: samba credentials :param named names: samba provision name :param named setup_path: method to get the setup ldif files path :rtype bool: :returns: True if any migration has been performed """ self.db.select_db(self.db_name) migration_manager = MigrationManager(self.db, self.db_name) return migration_manager.migrate(self.migration_app, version, **kwargs)
def test_setup(self): manager = MigrationManager(self.db, self.db_name, 'test_migrations') self.assertIsNotNone(manager) cur = self.db.cursor() cur.execute('DESCRIBE test_migrations') row = cur.fetchone() self.assertIsNotNone(row)
def test_list_migrations(self): manager = MigrationManager(self.db, self.db_name, 'test_migrations') self.assertEquals(len(manager.list_migrations(self.app).keys()), 0) self._register_do_nothing_migrations(self.app) self.assertEquals(len(manager.list_migrations(self.app).keys()), 2) self.assertTrue(all([m['applied'] is None for m in manager.list_migrations(self.app).itervalues()])) self.assertTrue(all(['class' in m for m in manager.list_migrations(self.app).itervalues()])) manager.migrate(self.app) for m in manager.list_migrations(self.app).itervalues(): self.assertTrue(m['applied']) self.assertTrue(len(m['class'].description)) for v in manager.migrations.keys(): del manager.migrations[v]
def test_fake_migrations(self): manager = MigrationManager(self.db, self.db_name, 'test_migrations') self.assertFalse(manager.fake_migration('fake app', None)) self._register_do_nothing_migrations(self.app) self.assertFalse(manager.fake_migration(self.app, None)) self.assertTrue(manager.fake_migration(self.app, 3)) self.assertEquals(manager.version(self.app), 2) self.assertFalse(manager.fake_migration(self.app, 2)) self.assertTrue(manager.fake_migration(self.app, 1)) self.assertEquals(manager.version(self.app), 1) for v in manager.migrations.keys(): del manager.migrations[v]
def test_multiapp(self): manager = MigrationManager(self.db, self.db_name, 'test_migrations') new_app = 'wannadie' self._register_do_nothing_migrations(self.app) self._register_do_nothing_migrations(new_app) migrated = manager.migrate(self.app, target_version=1) self.assertTrue(migrated) migrated = manager.migrate(new_app) self.assertTrue(migrated) self.assertEquals(manager.version(self.app), 1) self.assertEquals(manager.version(new_app), 2) self.assertLessEqual(manager.applied_migration(self.app, 1), datetime.now()) self.assertLessEqual(manager.applied_migration(new_app, 2), datetime.now()) self.assertEquals(len(manager.list_migrations(self.app).keys()), 2) self.assertEquals(len(manager.list_migrations(new_app).keys()), 2) for v in manager.migrations.keys(): del manager.migrations[v]
def test_version(self): manager = MigrationManager(self.db, self.db_name, 'test_migrations') self.assertIsNone(manager.version(self.app)) manager.apply_version(self.app, 1) self.assertIsNotNone(manager.version(self.app)) self.assertEquals(manager.version(self.app), 1) manager.apply_version(self.app, 3) self.assertEquals(manager.version(self.app), 3) manager.unapply_version(self.app, 3) self.assertEquals(manager.version(self.app), 1) manager.unapply_version(self.app, 1) self.assertIsNone(manager.version(self.app))
def test_go_forward_and_backwards(self): manager = MigrationManager(self.db, self.db_name, 'test_migrations') self._register_do_nothing_migrations(self.app) migrated = manager.migrate(self.app, target_version=1) self.assertTrue(migrated) self.assertEquals(manager.version(self.app), 1) self.assertLessEqual(manager.applied_migration(self.app, 1), datetime.now()) migrated = manager.migrate(self.app) self.assertTrue(migrated) self.assertEquals(manager.version(self.app), 2) self.assertLessEqual(manager.applied_migration(self.app, 2), datetime.now()) migrated = manager.migrate(self.app) self.assertFalse(migrated) migrated = manager.migrate(self.app, target_version=1) self.assertTrue(migrated) self.assertIsNone(manager.applied_migration(self.app, 2)) migrated = manager.migrate(self.app) self.assertTrue(migrated) migrated = manager.migrate(self.app, target_version=0) self.assertTrue(migrated) self.assertIsNone(manager.version(self.app)) self.assertIsNone(manager.applied_migration(self.app, 1)) self.assertIsNone(manager.applied_migration(self.app, 2)) migrated = manager.migrate(self.app, target_version=0) self.assertFalse(migrated) for v in manager.migrations.keys(): del manager.migrations[v]