diff --git a/ironic/db/sqlalchemy/api.py b/ironic/db/sqlalchemy/api.py index d4b8f868d9..c2c39f0ccd 100644 --- a/ironic/db/sqlalchemy/api.py +++ b/ironic/db/sqlalchemy/api.py @@ -1218,11 +1218,18 @@ class Connection(api.Connection): ids = [] for obj in query.slice(0, max_to_migrate): ids.append(obj['id']) - query = model_query(model).filter(model.id.in_(ids)) - - num_migrated = query.update( - {model.version: mapping[model.__name__][0]}, - synchronize_session=False) + num_migrated = ( + model_query(model). + filter(sql.and_(model.id.in_(ids), + model.version.is_(None))). + update({model.version: mapping[model.__name__][0]}, + synchronize_session=False)) + else: + num_migrated = ( + model_query(model). + filter(model.version.is_(None)). + update({model.version: mapping[model.__name__][0]}, + synchronize_session=False)) total_migrated += num_migrated max_to_migrate -= num_migrated diff --git a/releasenotes/notes/backfill_version_column_db_race_condition-713fa05832b93ca5.yaml b/releasenotes/notes/backfill_version_column_db_race_condition-713fa05832b93ca5.yaml new file mode 100644 index 0000000000..febe06281f --- /dev/null +++ b/releasenotes/notes/backfill_version_column_db_race_condition-713fa05832b93ca5.yaml @@ -0,0 +1,7 @@ +--- +fixes: + - | + Fixes an issue when running ``ironic-dbsync online_data_migrations``. The + value of an object's new ``version`` column might have been incorrectly + changed from a newer object version to an older object version, due to a + race condition. This is no longer the case.