Python oslo_db.exception.DBMigrationError() Examples

The following are 23 code examples of oslo_db.exception.DBMigrationError(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module oslo_db.exception , or try the search function .
Example #1
Source File: migration.py    From masakari with Apache License 2.0 6 votes vote down vote up
def db_sync(version=None, init_version=INIT_VERSION, engine=None):
    if engine is None:
        engine = db_api.get_engine()

    current_db_version = get_backend().db_version(engine,
                                                  MIGRATE_REPO_PATH,
                                                  init_version)

    if version and int(version) < current_db_version:
        msg = _('Database schema downgrade is not allowed.')
        raise exception.InvalidInput(reason=msg)

    if version and int(version) > db.MAX_INT:
        message = _('Version should be less than or equal to %(max_version)d.'
                    ) % {'max_version': db.MAX_INT}
        raise exception.InvalidInput(reason=message)

    try:
        return get_backend().db_sync(engine=engine,
                                     abs_path=MIGRATE_REPO_PATH,
                                     version=version,
                                     init_version=init_version)
    except oslo_exception.DBMigrationError as exc:
        raise exception.InvalidInput(reason=exc) 
Example #2
Source File: __init__.py    From vitrage with Apache License 2.0 6 votes vote down vote up
def create_schema(config=None, engine=None):
    """Create database schema from models description.

    Can be used for initial installation instead of upgrade('head').
    """
    if engine is None:
        engine = enginefacade.writer.get_engine()

    # NOTE(viktors): If we will use metadata.create_all() for non empty db
    #                schema, it will only add the new tables, but leave
    #                existing as is. So we should avoid of this situation.
    if version(engine=engine) is not None:
        raise db_exc.DBMigrationError("DB schema is already under version"
                                      " control. Use upgrade() instead")

    models.Base.metadata.create_all(engine)
    stamp('head', config=config) 
Example #3
Source File: migration.py    From watcher with Apache License 2.0 6 votes vote down vote up
def create_schema(config=None, engine=None):
    """Create database schema from models description.

    Can be used for initial installation instead of upgrade('head').
    """
    if engine is None:
        engine = sqla_api.get_engine()

    # NOTE(viktors): If we will use metadata.create_all() for non empty db
    #                schema, it will only add the new tables, but leave
    #                existing as is. So we should avoid of this situation.
    if version(engine=engine) is not None:
        raise db_exc.DBMigrationError(
            _("Watcher database schema is already under version control; "
              "use upgrade() instead"))

    models.Base.metadata.create_all(engine)
    stamp('head', config=config) 
Example #4
Source File: migration.py    From oslo.db with Apache License 2.0 6 votes vote down vote up
def db_version(engine, abs_path, init_version):
    """Show the current version of the repository.

    :param engine:  SQLAlchemy engine instance for a given database
    :param abs_path: Absolute path to migrate repository
    :param init_version:  Initial database version
    """
    repository = _find_migrate_repo(abs_path)
    try:
        return versioning_api.db_version(engine, repository)
    except versioning_exceptions.DatabaseNotControlledError:
        meta = sqlalchemy.MetaData()
        meta.reflect(bind=engine)
        tables = meta.tables
        if (len(tables) == 0 or 'alembic_version' in tables or
                'migrate_version' in tables):
            db_version_control(engine, abs_path, version=init_version)
            return versioning_api.db_version(engine, repository)
        else:
            raise exception.DBMigrationError(
                _("The database is not under version control, but has "
                  "tables. Please stamp the current version of the schema "
                  "manually.")) 
Example #5
Source File: migration.py    From oslo.db with Apache License 2.0 6 votes vote down vote up
def db_version_control(engine, abs_path, version=None):
    """Mark a database as under this repository's version control.

    Once a database is under version control, schema changes should
    only be done via change scripts in this repository.

    :param engine:  SQLAlchemy engine instance for a given database
    :param abs_path: Absolute path to migrate repository
    :param version:  Initial database version
    """
    repository = _find_migrate_repo(abs_path)

    try:
        versioning_api.version_control(engine, repository, version)
    except versioning_exceptions.InvalidVersionError as ex:
        raise exception.DBMigrationError("Invalid version : %s" % ex)
    except versioning_exceptions.DatabaseAlreadyControlledError:
        raise exception.DBMigrationError("Database is already controlled.")

    return version 
Example #6
Source File: manager.py    From oslo.db with Apache License 2.0 6 votes vote down vote up
def downgrade(self, revision):
        """Downgrade database with available backends."""
        # a revision exists only in a single plugin. Until we reached it, we
        # should upgrade to the plugins' first revision.
        # revision=None is a special case meaning initial revision.
        rev_in_plugins = [p.has_revision(revision) for p in self._plugins]
        if not any(rev_in_plugins) and revision is not None:
            raise exception.DBMigrationError('Revision does not exist')

        # downgrading should be performed in reversed order
        results = []
        for plugin, has_revision in zip(reversed(self._plugins),
                                        reversed(rev_in_plugins)):
            if not has_revision or revision is None:
                results.append(plugin.downgrade(None))
            else:
                results.append(plugin.downgrade(revision))
                break
        return results 
Example #7
Source File: migration.py    From zun with Apache License 2.0 6 votes vote down vote up
def create_schema(config=None, engine=None):
    """Create database schema from models description.

    Can be used for initial installation instead of upgrade('head').
    """
    if engine is None:
        engine = enginefacade.get_legacy_facade().get_engine()

    # NOTE(viktors): If we will use metadata.create_all() for non empty db
    #                schema, it will only add the new tables, but leave
    #                existing as is. So we should avoid of this situation.
    if version(engine=engine) is not None:
        raise db_exc.DBMigrationError("DB schema is already under version"
                                      " control. Use upgrade() instead")
    models.Base.metadata.create_all(engine)
    stamp('head', config=config) 
Example #8
Source File: test_migrate_cli.py    From oslo.db with Apache License 2.0 6 votes vote down vote up
def test_upgrade_checks_rev_existence(self):
        self.first_ext.obj.has_revision.return_value = False
        self.second_ext.obj.has_revision.return_value = False

        # upgrade to a specific non-existent revision should fail
        self.assertRaises(exception.DBMigrationError,
                          self.migration_manager.upgrade, 100)

        # upgrade to the "head" should succeed
        self.assertEqual([100, 200], self.migration_manager.upgrade(None))

        # let's assume the second ext has the revision, upgrade should succeed
        self.second_ext.obj.has_revision.return_value = True
        self.assertEqual([100, 200], self.migration_manager.upgrade(200))

        # upgrade to the "head" should still succeed
        self.assertEqual([100, 200], self.migration_manager.upgrade(None)) 
Example #9
Source File: test_migrate_cli.py    From oslo.db with Apache License 2.0 6 votes vote down vote up
def test_downgrade_checks_rev_existence(self):
        self.first_ext.obj.has_revision.return_value = False
        self.second_ext.obj.has_revision.return_value = False

        # upgrade to a specific non-existent revision should fail
        self.assertRaises(exception.DBMigrationError,
                          self.migration_manager.downgrade, 100)

        # downgrade to the "base" should succeed
        self.assertEqual([100, 0], self.migration_manager.downgrade(None))

        # let's assume the second ext has the revision, downgrade should
        # succeed
        self.first_ext.obj.has_revision.return_value = True
        self.assertEqual([100, 0], self.migration_manager.downgrade(200))

        # downgrade to the "base" should still succeed
        self.assertEqual([100, 0], self.migration_manager.downgrade(None))
        self.assertEqual([100, 0], self.migration_manager.downgrade('base')) 
Example #10
Source File: test_migrations.py    From oslo.db with Apache License 2.0 5 votes vote down vote up
def _fake_upgrade_boom(*args, **kwargs):
        raise exc.DBMigrationError("boom") 
Example #11
Source File: manage.py    From karbor with Apache License 2.0 5 votes vote down vote up
def sync(self, version=None):
        """Sync the database up to the most recent version."""
        if version is not None and version > db.MAX_INT:
            print(_('Version should be less than or equal to '
                    '%(max_version)d.') % {'max_version': db.MAX_INT})
            sys.exit(1)
        try:
            return db_migration.db_sync(version)
        except db_exc.DBMigrationError as ex:
            print("Error during database migration: %s" % ex)
            sys.exit(1) 
Example #12
Source File: test_cmd.py    From karbor with Apache License 2.0 5 votes vote down vote up
def test_db_commands_script_not_present(self, db_sync):
        db_sync.side_effect = db_exc.DBMigrationError(None)
        db_cmds = karbor_manage.DbCommands()
        exit = self.assertRaises(SystemExit, db_cmds.sync, 101)
        self.assertEqual(1, exit.code) 
Example #13
Source File: migration.py    From cyborg with Apache License 2.0 5 votes vote down vote up
def create_schema(config=None, engine=None):
    """Create database schema from models description.

    Can be used for initial installation instead of upgrade('head').
    """
    if engine is None:
        engine = enginefacade.get_legacy_facade().get_engine()

    if version(engine=engine) is not None:
        raise db_exc.DBMigrationError("DB schema is already under version"
                                      " control. Use upgrade() instead")

    models.Base.metadata.create_all(engine)
    stamp('head', config=config) 
Example #14
Source File: test_migration_common.py    From oslo.db with Apache License 2.0 5 votes vote down vote up
def test_db_sync_script_not_present(self, upgrade):
        # For non existent migration script file sqlalchemy-migrate will raise
        # VersionNotFoundError which will be wrapped in DBMigrationError.
        upgrade.side_effect = migrate_exception.VersionNotFoundError
        self.assertRaises(db_exception.DBMigrationError,
                          migration.db_sync, self.engine, self.path,
                          self.test_version + 1) 
Example #15
Source File: test_migration_common.py    From oslo.db with Apache License 2.0 5 votes vote down vote up
def test_db_sync_wrong_version(self):
        self.assertRaises(db_exception.DBMigrationError,
                          migration.db_sync, self.engine, self.path, 'foo') 
Example #16
Source File: test_migration_common.py    From oslo.db with Apache License 2.0 5 votes vote down vote up
def test_db_version_raise_not_controlled_error_tables(self):
        with mock.patch.object(sqlalchemy, 'MetaData') as mock_meta:
            self.mock_api_db_version.side_effect = \
                migrate_exception.DatabaseNotControlledError('oups')
            my_meta = mock.MagicMock()
            my_meta.tables = {'a': 1, 'b': 2}
            mock_meta.return_value = my_meta

            self.assertRaises(
                db_exception.DBMigrationError, migration.db_version,
                self.engine, self.path, self.init_version) 
Example #17
Source File: test_migration_common.py    From oslo.db with Apache License 2.0 5 votes vote down vote up
def test_db_version_control_version_greater_than_actual_version(
            self, mock_version_control, mock_find_repo):
        mock_find_repo.return_value = self.return_value
        mock_version_control.side_effect = (migrate_exception.
                                            InvalidVersionError)
        self.assertRaises(db_exception.DBMigrationError,
                          migration.db_version_control, self.engine,
                          self.path, self.test_version + 1) 
Example #18
Source File: test_migration_common.py    From oslo.db with Apache License 2.0 5 votes vote down vote up
def test_db_version_control_version_less_than_actual_version(
            self, mock_version_control, mock_find_repo):
        mock_find_repo.return_value = self.return_value
        mock_version_control.side_effect = (migrate_exception.
                                            DatabaseAlreadyControlledError)
        self.assertRaises(db_exception.DBMigrationError,
                          migration.db_version_control, self.engine,
                          self.path, self.test_version - 1) 
Example #19
Source File: test_migrations.py    From oslo.db with Apache License 2.0 5 votes vote down vote up
def test_migrate_up_fail(self):
        version = 141
        self.migration_api.db_version.return_value = version
        expected_output = (u"Failed to migrate to version %(version)s on "
                           "engine %(engine)s\n" %
                           {'version': version, 'engine': self.engine})

        with mock.patch.object(self.migration_api,
                               'upgrade',
                               side_effect=self._fake_upgrade_boom):
            log = self.useFixture(fixtures.FakeLogger())
            self.assertRaises(exc.DBMigrationError, self.migrate_up, version)
            self.assertEqual(expected_output, log.output) 
Example #20
Source File: test_migrations.py    From oslo.db with Apache License 2.0 5 votes vote down vote up
def migrate_up(self, version, with_data=False):
        """Migrate up to a new version of the db.

        :param version: id of revision to upgrade.
        :type version: str
        :keyword with_data: Whether to verify the applied changes with data,
            see :ref:`Auxiliary Methods <auxiliary-dynamic-methods>`.
        :type with_data: Bool
        """
        # NOTE(sdague): try block is here because it's impossible to debug
        # where a failed data migration happens otherwise
        try:
            if with_data:
                data = None
                pre_upgrade = getattr(
                    self, "_pre_upgrade_%03d" % version, None)
                if pre_upgrade:
                    data = pre_upgrade(self.migrate_engine)

            self.migration_api.upgrade(self.migrate_engine,
                                       self.REPOSITORY, version)
            self.assertEqual(version,
                             self.migration_api.db_version(self.migrate_engine,
                                                           self.REPOSITORY))
            if with_data:
                check = getattr(self, "_check_%03d" % version, None)
                if check:
                    check(self.migrate_engine, data)
        except exc.DBMigrationError:
            msg = "Failed to migrate to version %(ver)s on engine %(eng)s"
            LOG.error(msg, {"ver": version, "eng": self.migrate_engine})
            raise 
Example #21
Source File: migration.py    From oslo.db with Apache License 2.0 5 votes vote down vote up
def _find_migrate_repo(abs_path):
    """Get the project's change script repository

    :param abs_path: Absolute path to migrate repository
    """
    if not os.path.exists(abs_path):
        raise exception.DBMigrationError("Path %s not found" % abs_path)
    return Repository(abs_path) 
Example #22
Source File: migration.py    From oslo.db with Apache License 2.0 5 votes vote down vote up
def db_sync(engine, abs_path, version=None, init_version=0, sanity_check=True):
    """Upgrade or downgrade a database.

    Function runs the upgrade() or downgrade() functions in change scripts.

    :param engine:       SQLAlchemy engine instance for a given database
    :param abs_path:     Absolute path to migrate repository.
    :param version:      Database will upgrade/downgrade until this version.
                         If None - database will update to the latest
                         available version.
    :param init_version: Initial database version
    :param sanity_check: Require schema sanity checking for all tables
    """

    if version is not None:
        try:
            version = int(version)
        except ValueError:
            raise exception.DBMigrationError(_("version should be an integer"))

    current_version = db_version(engine, abs_path, init_version)
    repository = _find_migrate_repo(abs_path)
    if sanity_check:
        _db_schema_sanity_check(engine)
    if version is None or version > current_version:
        try:
            migration = versioning_api.upgrade(engine, repository, version)
        except Exception as ex:
            raise exception.DBMigrationError(ex)
    else:
        migration = versioning_api.downgrade(engine, repository,
                                             version)
    if sanity_check:
        _db_schema_sanity_check(engine)

    return migration 
Example #23
Source File: test_migrations.py    From zun with Apache License 2.0 5 votes vote down vote up
def test_upgrade_and_create_schema(self):
        with patch_with_engine(self.engine):
            self.migration_api.upgrade('2d1354bbf76e')
            self.assertRaises(db_exc.DBMigrationError,
                              self.migration_api.create_schema)