def test_migrate(self): connection = mock.Mock() executor = migration_executor.MigrationExecutor(connection) first = migration.Migration('1', None) second = migration.Migration('2', '1') third = migration.Migration('3', '2') with mock.patch.object(executor, 'migrations') as migrations: migrations.return_value = [first, second, third] migrated = {'1': True, '2': False, '3': False} with mock.patch.object(executor, '_migration_status_map', migrated): executor.migrate() self.assertEqual(migrated, {'1': True, '2': True, '3': True})
def test_rollback(self): connection = mock.Mock() executor = migration_executor.MigrationExecutor( connection, self.TEST_MIGRATIONS_DIR ) first = migration.Migration("1", None, "1") second = migration.Migration("2", "1", "2") third = migration.Migration("3", "2", "3") with mock.patch.object(executor, "migrations") as migrations: migrations.return_value = [first, second, third] migrated = {"1": True, "2": False, "3": False} with mock.patch.object(executor, "_migration_status_map", migrated): executor.rollback("1") self.assertEqual(migrated, {"1": False, "2": False, "3": False})
def test_filter_migrations_error_on_bad_last_migration(self): connection = mock.Mock() executor = migration_executor.MigrationExecutor(connection) first = migration.Migration('1', None) second = migration.Migration('2', '1') third = migration.Migration('3', '2') migrations = [first, second, third] migrated = {'1': True, '2': False, '3': False} with mock.patch.object(executor, '_migration_status_map', migrated): with self.assertRaises(error.SpannerError): executor._filter_migrations(migrations, False, '1') with self.assertRaises(error.SpannerError): executor._filter_migrations(migrations, False, '4')
def test_filter_migrations_error_on_bad_last_migration(self): connection = mock.Mock() executor = migration_executor.MigrationExecutor( connection, self.TEST_MIGRATIONS_DIR ) first = migration.Migration("1", None, "1") second = migration.Migration("2", "1", "2") third = migration.Migration("3", "2", "3") migrations = [first, second, third] migrated = {"1": True, "2": False, "3": False} with mock.patch.object(executor, "_migration_status_map", migrated): with self.assertRaises(error.SpannerError): executor._filter_migrations(migrations, False, "1") with self.assertRaises(error.SpannerError): executor._filter_migrations(migrations, False, "4")
def test_validate_migrations_error_on_unmigrated_first(self): connection = mock.Mock() executor = migration_executor.MigrationExecutor(connection) first = migration.Migration('2', '1') with mock.patch.object(executor, 'migrations') as migrations: migrations.return_value = [first] migrated = {'1': False} with mock.patch.object(executor, '_migration_status_map', migrated): with self.assertRaises(error.SpannerError): executor._validate_migrations() migrated = {} with mock.patch.object(executor, '_migration_status_map', migrated): with self.assertRaises(error.SpannerError): executor._validate_migrations()
def test_show_migrations(self): connection = mock.Mock() executor = migration_executor.MigrationExecutor( connection, self.TEST_MIGRATIONS_DIR ) first = migration.Migration("abcdef", None, "1") second = migration.Migration("012345", "abcdef", "2") third = migration.Migration("6abcde", "012345", "3") with mock.patch.object(executor, "migrations") as migrations: migrations.return_value = [first, second, third] migrated = {"abcdef": True, "012345": False, "6abcde": False} with mock.patch.object(executor, "_migration_status_map", migrated): with mock.patch("sys.stdout", new_callable=StringIO) as mock_stdout: executor.show_migrations() self.assertEqual( "[ ] 6abcde, 3\n[ ] 012345, 2\n[X] abcdef, 1\n", mock_stdout.getvalue(), )
def test_validate_migrations_error_on_unmigrated_first(self): connection = mock.Mock() executor = migration_executor.MigrationExecutor( connection, self.TEST_MIGRATIONS_DIR ) first = migration.Migration("2", "1", "2") with mock.patch.object(executor, "migrations") as migrations: migrations.return_value = [first] migrated = {"1": False} with mock.patch.object(executor, "_migration_status_map", migrated): with self.assertRaises(error.SpannerError): executor._validate_migrations() migrated = {} with mock.patch.object(executor, "_migration_status_map", migrated): with self.assertRaises(error.SpannerError): executor._validate_migrations()
def test_filter_migrations(self): connection = mock.Mock() executor = migration_executor.MigrationExecutor(connection) first = migration.Migration('1', None) second = migration.Migration('2', '1') third = migration.Migration('3', '2') migrations = [first, second, third] migrated = {'1': True, '2': False, '3': False} with mock.patch.object(executor, '_migration_status_map', migrated): filtered = executor._filter_migrations(migrations, False, None) self.assertEqual(filtered, [second, third]) filtered = executor._filter_migrations(migrations, False, '2') self.assertEqual(filtered, [second]) filtered = executor._filter_migrations(reversed(migrations), True, '1') self.assertEqual(filtered, [first])
def test_validate_migrations(self): connection = mock.Mock() executor = migration_executor.MigrationExecutor( connection, self.TEST_MIGRATIONS_DIR) first = migration.Migration('1', None) second = migration.Migration('2', '1') third = migration.Migration('3', '2') with mock.patch.object(executor, 'migrations') as migrations: migrations.return_value = [first, second, third] migrated = {'1': True, '2': False, '3': False} with mock.patch.object(executor, '_migration_status_map', migrated): executor._validate_migrations() migrated = {'1': False, '2': False, '3': False} with mock.patch.object(executor, '_migration_status_map', migrated): executor._validate_migrations()
def test_validate_migrations_error_on_unmigrated_after_migrated(self): connection = mock.Mock() executor = migration_executor.MigrationExecutor(connection) first = migration.Migration('1', None) second = migration.Migration('2', '1') third = migration.Migration('3', '2') with mock.patch.object(executor, 'migrations') as migrations: migrations.return_value = [first, second, third] migrated = {'1': False, '2': True, '3': False} with mock.patch.object(executor, '_migration_status_map', migrated): with self.assertRaises(error.SpannerError): executor._validate_migrations() migrated = {'1': False, '2': False, '3': True} with mock.patch.object(executor, '_migration_status_map', migrated): with self.assertRaises(error.SpannerError): executor._validate_migrations()
def test_filter_migrations(self): connection = mock.Mock() executor = migration_executor.MigrationExecutor( connection, self.TEST_MIGRATIONS_DIR ) first = migration.Migration("1", None, "1") second = migration.Migration("2", "1", "2") third = migration.Migration("3", "2", "3") migrations = [first, second, third] migrated = {"1": True, "2": False, "3": False} with mock.patch.object(executor, "_migration_status_map", migrated): filtered = executor._filter_migrations(migrations, False, None) self.assertEqual(filtered, [second, third]) filtered = executor._filter_migrations(migrations, False, "2") self.assertEqual(filtered, [second]) filtered = executor._filter_migrations(reversed(migrations), True, "1") self.assertEqual(filtered, [first])
def rollback(args: Any) -> None: connection = api.SpannerConnection(args.instance, args.database) executor = migration_executor.MigrationExecutor(connection, args.directory) executor.rollback(args.name)
def show_migrations(args: Any) -> None: connection = api.SpannerConnection(args.instance, args.database) executor = migration_executor.MigrationExecutor(connection, args.directory) executor.show_migrations()