From 500623f9899f2bae1860ab543916fdb309860350 Mon Sep 17 00:00:00 2001 From: Anton Arefiev Date: Tue, 7 Feb 2017 16:46:25 +0200 Subject: [PATCH] Fix updating rows in d00d6e3f38c4 migration Add `where` clause to timestamp update, otherwise existing nodes will contain timestamp of the last node. Change-Id: I6a729522e2d077ecf84e48fedb0bf92c16cde838 Closes-Bug: #1662504 --- ...e3f38c4_change_created_finished_at_type.py | 7 +++-- ironic_inspector/test/unit/test_migrations.py | 28 +++++++++++-------- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/ironic_inspector/migrations/versions/d00d6e3f38c4_change_created_finished_at_type.py b/ironic_inspector/migrations/versions/d00d6e3f38c4_change_created_finished_at_type.py index faa28376c..15fad6a29 100644 --- a/ironic_inspector/migrations/versions/d00d6e3f38c4_change_created_finished_at_type.py +++ b/ironic_inspector/migrations/versions/d00d6e3f38c4_change_created_finished_at_type.py @@ -37,22 +37,23 @@ def upgrade(): nullable=True) temp_finished_at = sa.Column("temp_finished_at", sa.types.DateTime, nullable=True) + uuid = sa.Column("uuid", sa.String(36), primary_key=True) op.add_column("nodes", temp_started_at) op.add_column("nodes", temp_finished_at) t = sa.table('nodes', started_at, finished_at, - temp_started_at, temp_finished_at) + temp_started_at, temp_finished_at, uuid) conn = op.get_bind() - rows = conn.execute(sa.select([t.c.started_at, t.c.finished_at])) + rows = conn.execute(sa.select([t.c.started_at, t.c.finished_at, t.c.uuid])) for row in rows: temp_started = datetime.datetime.utcfromtimestamp(row['started_at']) temp_finished = row['finished_at'] # Note(milan) this is just a precaution; sa.null shouldn't happen here if temp_finished is not None: temp_finished = datetime.datetime.utcfromtimestamp(temp_finished) - conn.execute(t.update().values( + conn.execute(t.update().where(t.c.uuid == row.uuid).values( temp_started_at=temp_started, temp_finished_at=temp_finished)) with op.batch_alter_table('nodes') as batch_op: diff --git a/ironic_inspector/test/unit/test_migrations.py b/ironic_inspector/test/unit/test_migrations.py index 88a18ec9c..2e5f9cb39 100644 --- a/ironic_inspector/test/unit/test_migrations.py +++ b/ironic_inspector/test/unit/test_migrations.py @@ -25,7 +25,6 @@ The test will then use that db and u/p combo to run the tests. import contextlib import datetime -import time import alembic from alembic import script @@ -368,12 +367,14 @@ class MigrationCheckersMixin(object): def _pre_upgrade_d00d6e3f38c4(self, engine): nodes = db_utils.get_table(engine, 'nodes') - for finished_at in (None, time.time()): - data = {'uuid': uuidutils.generate_uuid(), - 'started_at': time.time(), + data = [] + for finished_at in (None, 1234.0): + node = {'uuid': uuidutils.generate_uuid(), + 'started_at': 1232.0, 'finished_at': finished_at, 'error': None} - nodes.insert().values(data).execute() + nodes.insert().values(node).execute() + data.append(node) return data def _check_d00d6e3f38c4(self, engine, data): @@ -387,13 +388,16 @@ class MigrationCheckersMixin(object): self.assertIsInstance(nodes.c.finished_at.type, sqlalchemy.types.DateTime) - node = nodes.select(nodes.c.uuid == data['uuid']).execute().first() - self.assertEqual( - datetime.datetime.utcfromtimestamp(data['started_at']), - node['started_at']) - self.assertEqual( - datetime.datetime.utcfromtimestamp(data['finished_at']), - node['finished_at']) + for node in data: + finished_at = datetime.datetime.utcfromtimestamp( + node['finished_at']) if node['finished_at'] else None + row = nodes.select(nodes.c.uuid == node['uuid']).execute().first() + self.assertEqual( + datetime.datetime.utcfromtimestamp(node['started_at']), + row['started_at']) + self.assertEqual( + finished_at, + row['finished_at']) def test_upgrade_and_version(self): with patch_with_engine(self.engine):