本文整理汇总了Python中oslo_db.exception.DBMigrationError方法的典型用法代码示例。如果您正苦于以下问题:Python exception.DBMigrationError方法的具体用法?Python exception.DBMigrationError怎么用?Python exception.DBMigrationError使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类oslo_db.exception
的用法示例。
在下文中一共展示了exception.DBMigrationError方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: create_schema
# 需要导入模块: from oslo_db import exception [as 别名]
# 或者: from oslo_db.exception import DBMigrationError [as 别名]
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)
示例2: db_sync
# 需要导入模块: from oslo_db import exception [as 别名]
# 或者: from oslo_db.exception import DBMigrationError [as 别名]
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)
示例3: create_schema
# 需要导入模块: from oslo_db import exception [as 别名]
# 或者: from oslo_db.exception import DBMigrationError [as 别名]
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)
示例4: create_schema
# 需要导入模块: from oslo_db import exception [as 别名]
# 或者: from oslo_db.exception import DBMigrationError [as 别名]
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)
示例5: db_version
# 需要导入模块: from oslo_db import exception [as 别名]
# 或者: from oslo_db.exception import DBMigrationError [as 别名]
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."))
示例6: db_version_control
# 需要导入模块: from oslo_db import exception [as 别名]
# 或者: from oslo_db.exception import DBMigrationError [as 别名]
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
示例7: downgrade
# 需要导入模块: from oslo_db import exception [as 别名]
# 或者: from oslo_db.exception import DBMigrationError [as 别名]
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
示例8: test_upgrade_checks_rev_existence
# 需要导入模块: from oslo_db import exception [as 别名]
# 或者: from oslo_db.exception import DBMigrationError [as 别名]
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))
示例9: test_downgrade_checks_rev_existence
# 需要导入模块: from oslo_db import exception [as 别名]
# 或者: from oslo_db.exception import DBMigrationError [as 别名]
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'))
示例10: test_upgrade_and_create_schema
# 需要导入模块: from oslo_db import exception [as 别名]
# 或者: from oslo_db.exception import DBMigrationError [as 别名]
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)
示例11: db_sync
# 需要导入模块: from oslo_db import exception [as 别名]
# 或者: from oslo_db.exception import DBMigrationError [as 别名]
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
示例12: _find_migrate_repo
# 需要导入模块: from oslo_db import exception [as 别名]
# 或者: from oslo_db.exception import DBMigrationError [as 别名]
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)
示例13: migrate_up
# 需要导入模块: from oslo_db import exception [as 别名]
# 或者: from oslo_db.exception import DBMigrationError [as 别名]
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
示例14: _fake_upgrade_boom
# 需要导入模块: from oslo_db import exception [as 别名]
# 或者: from oslo_db.exception import DBMigrationError [as 别名]
def _fake_upgrade_boom(*args, **kwargs):
raise exc.DBMigrationError("boom")
示例15: test_migrate_up_fail
# 需要导入模块: from oslo_db import exception [as 别名]
# 或者: from oslo_db.exception import DBMigrationError [as 别名]
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)