From 4f07468bc8e8fc4575ecbe260ae78d2ef8c19863 Mon Sep 17 00:00:00 2001 From: Michal Gershenzon Date: Wed, 9 Nov 2016 17:53:29 +0000 Subject: [PATCH] Migrate mistral task_type In order to be able to get full info from the API for old tasks, it is needed to get the type from the spec column to the type column. The alternative was adding a release note explaining the inconsistency. Change-Id: I48dd286f749ceb491ab32ee15af62a8db54acdc7 --- .../020_add_type_to_task_execution.py | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/mistral/db/sqlalchemy/migration/alembic_migrations/versions/020_add_type_to_task_execution.py b/mistral/db/sqlalchemy/migration/alembic_migrations/versions/020_add_type_to_task_execution.py index f34cc13ee..fc729be58 100644 --- a/mistral/db/sqlalchemy/migration/alembic_migrations/versions/020_add_type_to_task_execution.py +++ b/mistral/db/sqlalchemy/migration/alembic_migrations/versions/020_add_type_to_task_execution.py @@ -26,8 +26,22 @@ revision = '020' down_revision = '019' from alembic import op +from mistral.db.sqlalchemy import types as st import sqlalchemy as sa +# A simple model of the task executions table with only the fields needed for +# the migration. +task_executions = sa.Table( + 'task_executions_v2', + sa.MetaData(), + sa.Column('id', sa.String(36), nullable=False), + sa.Column( + 'spec', + st.JsonMediumDictType() + ), + sa.Column('type', sa.String(10), nullable=True) +) + def upgrade(): @@ -35,3 +49,25 @@ def upgrade(): 'task_executions_v2', sa.Column('type', sa.String(length=10), nullable=True) ) + + session = sa.orm.Session(bind=op.get_bind()) + values = [] + + for row in session.query(task_executions): + values.append({'id': row[0], + 'spec': row[1]}) + + with session.begin(subtransactions=True): + for value in values: + task_type = "ACTION" + if "workflow" in value['spec']: + task_type = "WORKFLOW" + + session.execute( + task_executions.update().values(type=task_type).where( + task_executions.c.id == value['id'] + ) + ) + + # this commit appears to be necessary + session.commit()