Fix DB migrations after alembic integration
With a change to swap sqlalchemy-migrate with alembic [1] a `db_sync --check` was broken. This is due to both `upgrades.get_db_version` and `upgrades.get_current_heads` are actually checking "current" state of the dabase by calling _get_current_heads[2][3], while obvious intention was to compare intended state with current state. With that we're introducing upgrade.get_head_revisions which will fetch revisions not from the database, but from the environment [4] As a result `db_sync --check` does compare desired state of the DB with actual state and exists with corresponsive status again. [1]f174b4fa7c[2] https://opendev.org/openstack/keystone/src/branch/master/keystone/common/sql/upgrades.py#L147 [3] https://opendev.org/openstack/keystone/src/branch/master/keystone/common/sql/upgrades.py#L191 [4] https://alembic.sqlalchemy.org/en/latest/api/runtime.html#alembic.runtime.environment.EnvironmentContext.get_head_revisions Closes-Bug: #2080542 Change-Id: I854d37e3b4a34a7880f157564466bde61a3f886a (cherry picked from commit5125d9feed)
This commit is contained in:
@@ -366,7 +366,7 @@ class DbSync(BaseApp):
|
||||
except db_exception.DBMigrationError:
|
||||
contract_version = None
|
||||
|
||||
heads = upgrades.get_current_heads()
|
||||
heads = upgrades.get_head_revisions()
|
||||
|
||||
if (
|
||||
upgrades.EXPAND_BRANCH not in heads or
|
||||
|
||||
@@ -19,6 +19,7 @@ import os
|
||||
from alembic import command as alembic_api
|
||||
from alembic import config as alembic_config
|
||||
from alembic import migration as alembic_migration
|
||||
from alembic.runtime import environment as alembic_environment
|
||||
from alembic import script as alembic_script
|
||||
from oslo_db import exception as db_exception
|
||||
from oslo_log import log as logging
|
||||
@@ -111,6 +112,11 @@ def _get_current_heads(engine, config):
|
||||
context = alembic_migration.MigrationContext.configure(conn)
|
||||
heads = context.get_current_heads()
|
||||
|
||||
heads_map = _get_head_maps(heads, script)
|
||||
return heads_map
|
||||
|
||||
|
||||
def _get_head_maps(heads, script):
|
||||
heads_map = {}
|
||||
|
||||
for head in heads:
|
||||
@@ -146,6 +152,17 @@ def get_current_heads():
|
||||
return heads
|
||||
|
||||
|
||||
def get_head_revisions():
|
||||
"""Get the available head for each the expand and contract branches."""
|
||||
config = _find_alembic_conf()
|
||||
script = alembic_script.ScriptDirectory.from_config(config)
|
||||
context = alembic_environment.EnvironmentContext(config, script)
|
||||
heads = context.get_head_revisions()
|
||||
heads_map = _get_head_maps(heads, script)
|
||||
|
||||
return heads_map
|
||||
|
||||
|
||||
def _is_database_under_alembic_control(engine):
|
||||
with engine.connect() as conn:
|
||||
context = alembic_migration.MigrationContext.configure(conn)
|
||||
|
||||
Reference in New Issue
Block a user