Example #1
0
 def upgrade(self):
     # NOTE(gordc): to minimise memory, only import migration when needed
     from oslo.db.sqlalchemy import migration
     path = os.path.join(os.path.abspath(os.path.dirname(__file__)),
                         '..', '..', 'storage', 'sqlalchemy',
                         'migrate_repo')
     migration.db_sync(self._engine_facade.get_engine(), path)
Example #2
0
def _sync_common_repo(version):
    abs_path = find_migrate_repo()
    init_version = migrate_repo.DB_INIT_VERSION
    engine = sql.get_engine()
    try:
        migration.db_sync(engine,
                          abs_path,
                          version=version,
                          init_version=init_version)
    except ValueError:
        # NOTE(morganfainberg): ValueError is raised from the sanity check (
        # verifies that tables are utf8 under mysql). The region table was not
        # initially built with InnoDB and utf8 as part of the table arguments
        # when the migration was initially created. Bug #1334779 is a scenario
        # where the deployer can get wedged, unable to upgrade or downgrade.
        # This is a workaround to "fix" that table if we're under MySQL.
        if engine.name == 'mysql' and six.text_type(get_db_version()) == '37':
            _fix_migration_37(engine)
            # Try the migration a second time now that we've done the
            # un-wedge work.
            migration.db_sync(engine,
                              abs_path,
                              version=version,
                              init_version=init_version)
        else:
            raise
Example #3
0
def sync_database_to_version(extension=None, version=None):
    if not extension:
        abs_path = find_migrate_repo()
        init_version = migrate_repo.DB_INIT_VERSION
    else:
        init_version = 0
        try:
            package_name = '.'.join((contrib.__name__, extension))
            package = importutils.import_module(package_name)
        except ImportError:
            raise ImportError(_("%s extension does not exist.")
                              % package_name)
        try:
            abs_path = find_migrate_repo(package)
            try:
                migration.db_version_control(sql.get_engine(), abs_path)
            # Register the repo with the version control API
            # If it already knows about the repo, it will throw
            # an exception that we can safely ignore
            except exceptions.DatabaseAlreadyControlledError:
                pass
        except exception.MigrationNotProvided as e:
            print(e)
            sys.exit(1)
    migration.db_sync(sql.get_engine(), abs_path, version=version,
                      init_version=init_version)
Example #4
0
 def upgrade(self):
     # NOTE(gordc): to minimise memory, only import migration when needed
     from oslo.db.sqlalchemy import migration
     path = os.path.join(os.path.abspath(os.path.dirname(__file__)),
                         '..', '..', 'storage', 'sqlalchemy',
                         'migrate_repo')
     migration.db_sync(self._engine_facade.get_engine(), path)
Example #5
0
 def downgrade(self, version=None):
     """Downgrade the database's migration level"""
     migration.db_sync(
         db_api.get_engine(),
         db_migration.MIGRATE_REPO_PATH,
         version,
         sanity_check=self._need_sanity_check())
Example #6
0
 def sync(self, version=None, current_version=None):
     """
     Place a database under migration control and upgrade/downgrade it,
     creating first if necessary.
     """
     if current_version not in (None, 'None'):
         migration.db_version_control(db_api.get_engine(),
                                      db_migration.MIGRATE_REPO_PATH,
                                      version=current_version)
     migration.db_sync(db_api.get_engine(), db_migration.MIGRATE_REPO_PATH,
                       version)
    def test_db_sync_sanity_called(self):
        with test_utils.nested(
            mock.patch('oslo_db.sqlalchemy.migration._find_migrate_repo'),
            mock.patch('oslo_db.sqlalchemy.migration._db_schema_sanity_check'),
            mock.patch.object(versioning_api, 'downgrade')
        ) as (mock_find_repo, mock_sanity, mock_downgrade):

            mock_find_repo.return_value = self.return_value
            migration.db_sync(self.engine, self.path, self.test_version)

            mock_sanity.assert_called_once_with(self.engine)
Example #8
0
    def test_db_sync_sanity_skipped(self):
        with contextlib.nested(
            mock.patch.object(migration, '_find_migrate_repo'),
            mock.patch.object(migration, '_db_schema_sanity_check'),
            mock.patch.object(versioning_api, 'downgrade')
        ) as (mock_find_repo, mock_sanity, mock_downgrade):

            mock_find_repo.return_value = self.return_value
            migration.db_sync(self.engine, self.path, self.test_version, False)

            mock_sanity.assert_not_called()
Example #9
0
 def sync(self, version=None, current_version=None):
     """
     Place a database under migration control and upgrade/downgrade it,
     creating first if necessary.
     """
     if current_version not in (None, 'None'):
         migration.db_version_control(db_api.get_engine(),
                                      db_migration.MIGRATE_REPO_PATH,
                                      version=current_version)
     migration.db_sync(db_api.get_engine(),
                       db_migration.MIGRATE_REPO_PATH,
                       version)
Example #10
0
    def test_db_sync_downgrade(self):
        with contextlib.nested(
            mock.patch.object(migration, '_find_migrate_repo'),
            mock.patch.object(versioning_api, 'downgrade')
        ) as (mock_find_repo, mock_downgrade):

            mock_find_repo.return_value = self.return_value
            self.mock_api_db_version.return_value = self.test_version + 1

            migration.db_sync(self.engine, self.path, self.test_version)

            mock_downgrade.assert_called_once_with(
                self.engine, self.return_value, self.test_version)
    def test_db_sync_sanity_called(self):
        with test_utils.nested(
                mock.patch.object(migration, '_find_migrate_repo'),
                mock.patch.object(migration, '_db_schema_sanity_check'),
                mock.patch.object(versioning_api,
                                  'downgrade')) as (mock_find_repo,
                                                    mock_sanity,
                                                    mock_downgrade):

            mock_find_repo.return_value = self.return_value
            migration.db_sync(self.engine, self.path, self.test_version)

            mock_sanity.assert_called_once_with(self.engine)
Example #12
0
    def test_db_sync_downgrade(self):
        with test_utils.nested(
                mock.patch('oslo_db.sqlalchemy.migration._find_migrate_repo'),
                mock.patch.object(versioning_api,
                                  'downgrade')) as (mock_find_repo,
                                                    mock_downgrade):

            mock_find_repo.return_value = self.return_value
            self.mock_api_db_version.return_value = self.test_version + 1

            migration.db_sync(self.engine, self.path, self.test_version)

            mock_downgrade.assert_called_once_with(self.engine,
                                                   self.return_value,
                                                   self.test_version)
def _setup_database(extensions=None):
    if CONF.database.connection != tests.IN_MEM_DB_CONN_STRING:
        db = tests.dirs.tmp('test.db')
        pristine = tests.dirs.tmp('test.db.pristine')

        if os.path.exists(db):
            os.unlink(db)
        if not os.path.exists(pristine):
            migration.db_sync(sql.get_engine(),
                              migration_helpers.find_migrate_repo())
            for extension in (extensions or []):
                migration_helpers.sync_database_to_version(extension=extension)
            shutil.copyfile(db, pristine)
        else:
            shutil.copyfile(pristine, db)
Example #14
0
def _setup_database(extensions=None):
    if CONF.database.connection != tests.IN_MEM_DB_CONN_STRING:
        db = tests.dirs.tmp('test.db')
        pristine = tests.dirs.tmp('test.db.pristine')

        if os.path.exists(db):
            os.unlink(db)
        if not os.path.exists(pristine):
            migration.db_sync(sql.get_engine(),
                              migration_helpers.find_migrate_repo())
            for extension in (extensions or []):
                migration_helpers.sync_database_to_version(extension=extension)
            shutil.copyfile(db, pristine)
        else:
            shutil.copyfile(pristine, db)
    def test_db_sync_upgrade(self):
        init_ver = 55
        with test_utils.nested(
            mock.patch('oslo_db.sqlalchemy.migration._find_migrate_repo'),
            mock.patch.object(versioning_api, 'upgrade')
        ) as (mock_find_repo, mock_upgrade):

            mock_find_repo.return_value = self.return_value
            self.mock_api_db_version.return_value = self.test_version - 1

            migration.db_sync(self.engine, self.path, self.test_version,
                              init_ver)

            mock_upgrade.assert_called_once_with(
                self.engine, self.return_value, self.test_version)
Example #16
0
def db_sync(engine, version=None):
    path = os.path.join(os.path.abspath(os.path.dirname(__file__)),
                        'migrate_repo')
    return oslo_migration.db_sync(engine,
                                  path,
                                  version,
                                  init_version=INIT_VERSION)
    def test_db_sync_upgrade(self):
        init_ver = 55
        with test_utils.nested(
                mock.patch.object(migration, '_find_migrate_repo'),
                mock.patch.object(versioning_api,
                                  'upgrade')) as (mock_find_repo,
                                                  mock_upgrade):

            mock_find_repo.return_value = self.return_value
            self.mock_api_db_version.return_value = self.test_version - 1

            migration.db_sync(self.engine, self.path, self.test_version,
                              init_ver)

            mock_upgrade.assert_called_once_with(self.engine,
                                                 self.return_value,
                                                 self.test_version)
Example #18
0
    def test_db_sync_sanity_skipped(self):
        with test_utils.nested(
                mock.patch('oslo_db.sqlalchemy.migration._find_migrate_repo'),
                mock.patch(
                    'oslo_db.sqlalchemy.migration._db_schema_sanity_check'),
                mock.patch.object(versioning_api,
                                  'downgrade')) as (mock_find_repo,
                                                    mock_sanity,
                                                    mock_downgrade):

            mock_find_repo.return_value = self.return_value
            migration.db_sync(self.engine,
                              self.path,
                              self.test_version,
                              sanity_check=False)

            self.assertFalse(mock_sanity.called)
def sync_database_to_version(extension=None, version=None):
    if not extension:
        abs_path = find_migrate_repo()
        init_version = migrate_repo.DB_INIT_VERSION
    else:
        init_version = 0
        try:
            package_name = '.'.join((contrib.__name__, extension))
            package = importutils.import_module(package_name)
        except ImportError:
            raise ImportError(_("%s extension does not exist.")
                              % package_name)
        try:
            abs_path = find_migrate_repo(package)
            try:
                migration.db_version_control(sql.get_engine(), abs_path)
            # Register the repo with the version control API
            # If it already knows about the repo, it will throw
            # an exception that we can safely ignore
            except exceptions.DatabaseAlreadyControlledError:
                pass
        except exception.MigrationNotProvided as e:
            print(e)
            sys.exit(1)

    engine = sql.get_engine()
    try:
        migration.db_sync(engine, abs_path, version=version,
                          init_version=init_version)
    except ValueError:
        # NOTE(morganfainberg): ValueError is raised from the sanity check (
        # verifies that tables are utf8 under mysql). The region table was not
        # initially built with InnoDB and utf8 as part of the table arguments
        # when the migration was initially created. Bug #1334779 is a scenario
        # where the deployer can get wedged, unable to upgrade or downgrade.
        # This is a workaround to "fix" that table if we're under MySQL.
        if (not extension and engine.name == 'mysql' and
                six.text_type(get_db_version()) == '37'):
            _fix_migration_37(engine)
            # Try the migration a second time now that we've done the
            # un-wedge work.
            migration.db_sync(engine, abs_path, version=version,
                              init_version=init_version)
        else:
            raise
Example #20
0
def _sync_extension_repo(extension, version):
    init_version = 0
    try:
        package_name = '.'.join((contrib.__name__, extension))
        package = importutils.import_module(package_name)
    except ImportError:
        raise ImportError(_("%s extension does not exist.")
                          % package_name)
    try:
        abs_path = find_migrate_repo(package)
        try:
            migration.db_version_control(sql.get_engine(), abs_path)
        # Register the repo with the version control API
        # If it already knows about the repo, it will throw
        # an exception that we can safely ignore
        except exceptions.DatabaseAlreadyControlledError:
            pass
    except exception.MigrationNotProvided as e:
        print(e)
        sys.exit(1)
    migration.db_sync(sql.get_engine(), abs_path, version=version,
                      init_version=init_version)
Example #21
0
def _sync_common_repo(version):
    abs_path = find_migrate_repo()
    init_version = migrate_repo.DB_INIT_VERSION
    engine = sql.get_engine()
    try:
        migration.db_sync(engine, abs_path, version=version,
                          init_version=init_version)
    except ValueError:
        # NOTE(morganfainberg): ValueError is raised from the sanity check (
        # verifies that tables are utf8 under mysql). The region table was not
        # initially built with InnoDB and utf8 as part of the table arguments
        # when the migration was initially created. Bug #1334779 is a scenario
        # where the deployer can get wedged, unable to upgrade or downgrade.
        # This is a workaround to "fix" that table if we're under MySQL.
        if engine.name == 'mysql' and six.text_type(get_db_version()) == '37':
            _fix_migration_37(engine)
            # Try the migration a second time now that we've done the
            # un-wedge work.
            migration.db_sync(engine, abs_path, version=version,
                              init_version=init_version)
        else:
            raise
Example #22
0
 def downgrade(self, version):
     try:
         #version for migrate should be valid int - else skip
         if version in ('base', None):
             version = self.init_version
         version = int(version)
         return migration.db_sync(self.engine,
                                  self.repository,
                                  version,
                                  init_version=self.init_version)
     except ValueError:
         LOG.error(
             _LE('Migration number for migrate plugin must be valid '
                 'integer or empty, if you want to downgrade '
                 'to initial state'))
         raise
Example #23
0
 def downgrade(self, version):
     try:
         # version for migrate should be valid int - else skip
         if version in ('base', None):
             version = self.init_version
         version = int(version)
         return migration.db_sync(
             self.engine, self.repository, version,
             init_version=self.init_version)
     except ValueError:
         LOG.error(
             _LE('Migration number for migrate plugin must be valid '
                 'integer or empty, if you want to downgrade '
                 'to initial state')
         )
         raise
Example #24
0
 def downgrade(self, version=None):
     """Downgrade the database's migration level"""
     migration.db_sync(db_api.get_engine(),
                       db_migration.MIGRATE_REPO_PATH,
                       version,
                       sanity_check=self._need_sanity_check())
Example #25
0
 def upgrade(self, version):
     version = None if version == 'head' else version
     return migration.db_sync(self.engine,
                              self.repository,
                              version,
                              init_version=self.init_version)
Example #26
0
 def upgrade(self, version=None):
     """Upgrade the database's migration level"""
     migration.db_sync(db_api.get_engine(),
                       db_migration.MIGRATE_REPO_PATH,
                       version)
Example #27
0
 def upgrade(self):
     path = os.path.join(os.path.abspath(os.path.dirname(__file__)), "sqlalchemy", "migrate_repo")
     migration.db_sync(self._engine_facade.get_engine(), path)
Example #28
0
 def upgrade(self):
     path = os.path.join(os.path.abspath(os.path.dirname(__file__)), '..',
                         '..', 'storage', 'sqlalchemy', 'migrate_repo')
     migration.db_sync(self._engine_facade.get_engine(), path)
Example #29
0
def db_sync(engine, version=None):
    path = os.path.join(os.path.abspath(os.path.dirname(__file__)),
                        'migrate_repo')
    return oslo_migration.db_sync(engine, path, version,
                                  init_version=INIT_VERSION)
Example #30
0
 def upgrade(self):
     path = os.path.join(os.path.abspath(os.path.dirname(__file__)),
                         '..', '..', 'storage', 'sqlalchemy',
                         'migrate_repo')
     migration.db_sync(self._engine_facade.get_engine(), path)
Example #31
0
 def upgrade(self, version):
     version = None if version == 'head' else version
     return migration.db_sync(
         self.engine, self.repository, version,
         init_version=self.init_version)
Example #32
0
 def upgrade(self, version=None):
     """Upgrade the database's migration level"""
     migration.db_sync(db_api.get_engine(),
                       db_migration.MIGRATE_REPO_PATH,
                       version)