Migration support for postgresql
After recent change in manage command [1] deploying glance with postgres using stack.sh fails as db_sync internally uses E-M-C and it has support only for mysql. Added flag online_migration which will be False in case of db_sync so that enigne check will be eliminated and True in case of running expand, migrate, contract to perform validatio check on engine type. [1] https://review.openstack.org/#/c/433934/ Closes-Bug: #1749466 Change-Id: Ifddeff139e212564118520f3150db198ab1c94f4
This commit is contained in:
parent
f2f8637d9c
commit
28fb47092f
|
@ -141,12 +141,7 @@ class DbCommands(object):
|
|||
|
||||
@args('--version', metavar='<version>', help='Database version')
|
||||
def sync(self, version=None):
|
||||
engine = db_api.get_engine().engine
|
||||
if engine.name == 'postgresql':
|
||||
if version is None:
|
||||
version = db_migration.LATEST_REVISION
|
||||
self._sync(version)
|
||||
return
|
||||
"""Perform a complete (offline) database migration"""
|
||||
|
||||
curr_heads = alembic_migrations.get_current_alembic_heads()
|
||||
contract = alembic_migrations.get_alembic_branch_head(
|
||||
|
@ -157,9 +152,11 @@ class DbCommands(object):
|
|||
sys.exit()
|
||||
|
||||
try:
|
||||
self.expand()
|
||||
self.migrate()
|
||||
self.contract()
|
||||
# NOTE(abhishekk): db_sync should not be used for online
|
||||
# migrations.
|
||||
self.expand(online_migration=False)
|
||||
self.migrate(online_migration=False)
|
||||
self.contract(online_migration=False)
|
||||
print(_('Database is synced successfully.'))
|
||||
except exception.GlanceException as e:
|
||||
sys.exit(_('Failed to sync database: ERROR: %s') % e)
|
||||
|
@ -186,7 +183,7 @@ class DbCommands(object):
|
|||
def _validate_engine(self, engine):
|
||||
"""Check engine is valid or not.
|
||||
|
||||
MySql is only supported for Glance EMC.
|
||||
MySql is only supported for online upgrade.
|
||||
Adding sqlite as engine to support existing functional test cases.
|
||||
|
||||
:param engine: database engine name
|
||||
|
@ -195,9 +192,10 @@ class DbCommands(object):
|
|||
sys.exit(_('Rolling upgrades are currently supported only for '
|
||||
'MySQL and Sqlite'))
|
||||
|
||||
def expand(self):
|
||||
"""Run the expansion phase of a rolling upgrade procedure."""
|
||||
self._validate_engine(db_api.get_engine())
|
||||
def expand(self, online_migration=True):
|
||||
"""Run the expansion phase of a database migration."""
|
||||
if online_migration:
|
||||
self._validate_engine(db_api.get_engine())
|
||||
|
||||
curr_heads = alembic_migrations.get_current_alembic_heads()
|
||||
expand_head = alembic_migrations.get_alembic_branch_head(
|
||||
|
@ -225,9 +223,10 @@ class DbCommands(object):
|
|||
else:
|
||||
print(_('Database expansion is up to date. No expansion needed.'))
|
||||
|
||||
def contract(self):
|
||||
"""Run the contraction phase of a rolling upgrade procedure."""
|
||||
self._validate_engine(db_api.get_engine())
|
||||
def contract(self, online_migration=True):
|
||||
"""Run the contraction phase of a database migration."""
|
||||
if online_migration:
|
||||
self._validate_engine(db_api.get_engine())
|
||||
|
||||
curr_heads = alembic_migrations.get_current_alembic_heads()
|
||||
contract_head = alembic_migrations.get_alembic_branch_head(
|
||||
|
@ -264,8 +263,10 @@ class DbCommands(object):
|
|||
'%(curr_revs)s ') % {'e_rev': expand_head,
|
||||
'curr_revs': curr_heads})
|
||||
|
||||
def migrate(self):
|
||||
self._validate_engine(db_api.get_engine())
|
||||
def migrate(self, online_migration=True):
|
||||
"""Run the data migration phase of a database migration."""
|
||||
if online_migration:
|
||||
self._validate_engine(db_api.get_engine())
|
||||
|
||||
curr_heads = alembic_migrations.get_current_alembic_heads()
|
||||
contract_head = alembic_migrations.get_alembic_branch_head(
|
||||
|
|
|
@ -237,9 +237,9 @@ class TestManage(TestManageBase):
|
|||
mock_get_current_alembic_heads.return_value = ['ocata_contract01']
|
||||
mock_get_alembic_branch_head.return_value = ['pike_contract01']
|
||||
self.db.sync()
|
||||
mock_expand.assert_called_once_with()
|
||||
mock_migrate.assert_called_once_with()
|
||||
mock_contract.assert_called_once_with()
|
||||
mock_expand.assert_called_once_with(online_migration=False)
|
||||
mock_migrate.assert_called_once_with(online_migration=False)
|
||||
mock_contract.assert_called_once_with(online_migration=False)
|
||||
self.assertIn('Database is synced successfully.',
|
||||
self.output.getvalue())
|
||||
|
||||
|
@ -247,7 +247,12 @@ class TestManage(TestManageBase):
|
|||
'glance.db.sqlalchemy.alembic_migrations.get_current_alembic_heads')
|
||||
@mock.patch(
|
||||
'glance.db.sqlalchemy.alembic_migrations.get_alembic_branch_head')
|
||||
def test_sync_db_is_already_sync(self, mock_get_alembic_branch_head,
|
||||
@mock.patch('glance.db.sqlalchemy.alembic_migrations.'
|
||||
'place_database_under_alembic_control')
|
||||
@mock.patch('alembic.command.upgrade')
|
||||
def test_sync_db_is_already_sync(self, mock_upgrade,
|
||||
mock_db_under_alembic_control,
|
||||
mock_get_alembic_branch_head,
|
||||
mock_get_current_alembic_heads):
|
||||
mock_get_current_alembic_heads.return_value = ['pike_contract01']
|
||||
mock_get_alembic_branch_head.return_value = ['pike_contract01']
|
||||
|
|
Loading…
Reference in New Issue