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
This commit is contained in:
Anton Arefiev 2017-02-07 16:46:25 +02:00
parent 6bf5bc8228
commit 500623f989
2 changed files with 20 additions and 15 deletions

View File

@ -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:

View File

@ -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):