|
16 | 16 | import sys |
17 | 17 |
|
18 | 18 | import datetime |
| 19 | +import mongoengine as me |
19 | 20 |
|
20 | 21 | from st2common.constants import action as action_constants |
| 22 | +from st2common.fields import ComplexDateTimeField |
| 23 | +from st2common.fields import JSONDictEscapedFieldCompatibilityField |
21 | 24 | from st2common.models.db import stormbase |
22 | 25 | from st2common.models.db.execution import ActionExecutionDB |
23 | 26 | from st2common.models.db.liveaction import LiveActionDB |
| 27 | +from st2common.models.db.notification import NotificationSchema |
24 | 28 | from st2common.models.db.workflow import WorkflowExecutionDB |
25 | 29 | from st2common.models.db.workflow import TaskExecutionDB |
26 | 30 | from st2common.models.db.trigger import TriggerInstanceDB |
|
31 | 35 | from st2common.persistence.trigger import TriggerInstance |
32 | 36 | from st2common.constants.triggers import TRIGGER_INSTANCE_PROCESSED |
33 | 37 | from st2common.constants.triggers import TRIGGER_INSTANCE_PENDING |
| 38 | +from st2common.util import date as date_utils |
34 | 39 |
|
35 | 40 | from st2tests import DbTestCase |
36 | 41 |
|
|
41 | 46 |
|
42 | 47 | import st2_migrate_db_dict_field_values as migration_module |
43 | 48 |
|
44 | | - |
45 | 49 | MOCK_RESULT_1 = { |
46 | 50 | "foo": "bar1", |
47 | 51 | "bar": 1, |
@@ -79,19 +83,171 @@ def test_migrate_executions(self): |
79 | 83 | LiveActionDB._meta["allow_inheritance"] = True |
80 | 84 |
|
81 | 85 | class ActionExecutionDB_OldFieldType(ActionExecutionDB): |
| 86 | + |
82 | 87 | result = stormbase.EscapedDynamicField(default={}) |
83 | 88 | liveaction = stormbase.EscapedDictField(required=True) |
84 | 89 | parameters = stormbase.EscapedDynamicField(default={}) |
85 | 90 |
|
| 91 | + workflow_execution = me.StringField() |
| 92 | + task_execution = me.StringField() |
| 93 | + status = me.StringField( |
| 94 | + required=True, help_text="The current status of the liveaction." |
| 95 | + ) |
| 96 | + start_timestamp = ComplexDateTimeField( |
| 97 | + default=date_utils.get_datetime_utc_now, |
| 98 | + help_text="The timestamp when the liveaction was created.", |
| 99 | + ) |
| 100 | + end_timestamp = ComplexDateTimeField( |
| 101 | + help_text="The timestamp when the liveaction has finished." |
| 102 | + ) |
| 103 | + action = stormbase.EscapedDictField(required=True) |
| 104 | + context = me.DictField( |
| 105 | + default={}, help_text="Contextual information on the action execution." |
| 106 | + ) |
| 107 | + delay = me.IntField(min_value=0) |
| 108 | + |
| 109 | + # diff from liveaction |
| 110 | + runner = stormbase.EscapedDictField(required=True) |
| 111 | + trigger = stormbase.EscapedDictField() |
| 112 | + trigger_type = stormbase.EscapedDictField() |
| 113 | + trigger_instance = stormbase.EscapedDictField() |
| 114 | + rule = stormbase.EscapedDictField() |
| 115 | + result_size = me.IntField(default=0, help_text="Serialized result size in bytes") |
| 116 | + parent = me.StringField() |
| 117 | + children = me.ListField(field=me.StringField()) |
| 118 | + log = me.ListField(field=me.DictField()) |
| 119 | + # Do not use URLField for web_url. If host doesn't have FQDN set, URLField validation blows. |
| 120 | + web_url = me.StringField(required=False) |
| 121 | + |
86 | 122 | class LiveActionDB_OldFieldType(LiveActionDB): |
87 | 123 | result = stormbase.EscapedDynamicField(default={}) |
88 | | - |
89 | | - # todo(aj) need ActionExecutionDB_NewFieldType to be used for update |
90 | | - # here. the model field type has changed to string in current models |
| 124 | + workflow_execution = me.StringField() |
| 125 | + task_execution = me.StringField() |
| 126 | + # TODO: Can status be an enum at the Mongo layer? |
| 127 | + status = me.StringField( |
| 128 | + required=True, help_text="The current status of the liveaction." |
| 129 | + ) |
| 130 | + start_timestamp = ComplexDateTimeField( |
| 131 | + default=date_utils.get_datetime_utc_now, |
| 132 | + help_text="The timestamp when the liveaction was created.", |
| 133 | + ) |
| 134 | + end_timestamp = ComplexDateTimeField( |
| 135 | + help_text="The timestamp when the liveaction has finished." |
| 136 | + ) |
| 137 | + action = me.StringField( |
| 138 | + required=True, help_text="Reference to the action that has to be executed." |
| 139 | + ) |
| 140 | + parameters = JSONDictEscapedFieldCompatibilityField( |
| 141 | + default={}, |
| 142 | + help_text="The key-value pairs passed as to the action runner & execution.", |
| 143 | + ) |
| 144 | + context = me.DictField( |
| 145 | + default={}, help_text="Contextual information on the action execution." |
| 146 | + ) |
| 147 | + delay = me.IntField( |
| 148 | + min_value=0, |
| 149 | + help_text="How long (in milliseconds) to delay the execution before scheduling.", |
| 150 | + ) |
| 151 | + |
| 152 | + # diff from action execution |
| 153 | + action_is_workflow = me.BooleanField( |
| 154 | + default=False, |
| 155 | + help_text="A flag indicating whether the referenced action is a workflow.", |
| 156 | + ) |
| 157 | + callback = me.DictField( |
| 158 | + default={}, |
| 159 | + help_text="Callback information for the on completion of action execution.", |
| 160 | + ) |
| 161 | + notify = me.EmbeddedDocumentField(NotificationSchema) |
| 162 | + runner_info = me.DictField( |
| 163 | + default={}, |
| 164 | + help_text="Information about the runner which executed this live action (hostname, pid).", |
| 165 | + ) |
| 166 | + |
| 167 | + class LiveActionDB_NewFieldType(LiveActionDB): |
| 168 | + result = JSONDictEscapedFieldCompatibilityField( |
| 169 | + default={}, help_text="Action defined result." |
| 170 | + ) |
| 171 | + workflow_execution = me.StringField() |
| 172 | + task_execution = me.StringField() |
| 173 | + # TODO: Can status be an enum at the Mongo layer? |
| 174 | + status = me.StringField( |
| 175 | + required=True, help_text="The current status of the liveaction." |
| 176 | + ) |
| 177 | + start_timestamp = ComplexDateTimeField( |
| 178 | + default=date_utils.get_datetime_utc_now, |
| 179 | + help_text="The timestamp when the liveaction was created.", |
| 180 | + ) |
| 181 | + end_timestamp = ComplexDateTimeField( |
| 182 | + help_text="The timestamp when the liveaction has finished." |
| 183 | + ) |
| 184 | + action = me.StringField( |
| 185 | + required=True, help_text="Reference to the action that has to be executed." |
| 186 | + ) |
| 187 | + parameters = JSONDictEscapedFieldCompatibilityField( |
| 188 | + default={}, |
| 189 | + help_text="The key-value pairs passed as to the action runner & execution.", |
| 190 | + ) |
| 191 | + context = me.DictField( |
| 192 | + default={}, help_text="Contextual information on the action execution." |
| 193 | + ) |
| 194 | + delay = me.IntField( |
| 195 | + min_value=0, |
| 196 | + help_text="How long (in milliseconds) to delay the execution before scheduling.", |
| 197 | + ) |
| 198 | + |
| 199 | + # diff from action execution |
| 200 | + action_is_workflow = me.BooleanField( |
| 201 | + default=False, |
| 202 | + help_text="A flag indicating whether the referenced action is a workflow.", |
| 203 | + ) |
| 204 | + callback = me.DictField( |
| 205 | + default={}, |
| 206 | + help_text="Callback information for the on completion of action execution.", |
| 207 | + ) |
| 208 | + notify = me.EmbeddedDocumentField(NotificationSchema) |
| 209 | + runner_info = me.DictField( |
| 210 | + default={}, |
| 211 | + help_text="Information about the runner which executed this live action (hostname, pid).", |
| 212 | + ) |
91 | 213 |
|
92 | 214 | class ActionExecutionDB_NewFieldType(ActionExecutionDB): |
93 | 215 | liveaction = stormbase.EscapedDictField(required=True) |
94 | 216 | parameters = stormbase.EscapedDynamicField(default={}) |
| 217 | + result = JSONDictEscapedFieldCompatibilityField( |
| 218 | + default={}, help_text="Action defined result." |
| 219 | + ) |
| 220 | + |
| 221 | + workflow_execution = me.StringField() |
| 222 | + task_execution = me.StringField() |
| 223 | + status = me.StringField( |
| 224 | + required=True, help_text="The current status of the liveaction." |
| 225 | + ) |
| 226 | + start_timestamp = ComplexDateTimeField( |
| 227 | + default=date_utils.get_datetime_utc_now, |
| 228 | + help_text="The timestamp when the liveaction was created.", |
| 229 | + ) |
| 230 | + end_timestamp = ComplexDateTimeField( |
| 231 | + help_text="The timestamp when the liveaction has finished." |
| 232 | + ) |
| 233 | + action = stormbase.EscapedDictField(required=True) |
| 234 | + context = me.DictField( |
| 235 | + default={}, help_text="Contextual information on the action execution." |
| 236 | + ) |
| 237 | + delay = me.IntField(min_value=0) |
| 238 | + |
| 239 | + # diff from liveaction |
| 240 | + runner = stormbase.EscapedDictField(required=True) |
| 241 | + trigger = stormbase.EscapedDictField() |
| 242 | + trigger_type = stormbase.EscapedDictField() |
| 243 | + trigger_instance = stormbase.EscapedDictField() |
| 244 | + rule = stormbase.EscapedDictField() |
| 245 | + result_size = me.IntField(default=0, help_text="Serialized result size in bytes") |
| 246 | + parent = me.StringField() |
| 247 | + children = me.ListField(field=me.StringField()) |
| 248 | + log = me.ListField(field=me.DictField()) |
| 249 | + # Do not use URLField for web_url. If host doesn't have FQDN set, URLField validation blows. |
| 250 | + web_url = me.StringField(required=False) |
95 | 251 |
|
96 | 252 | execution_dbs = ActionExecution.query( |
97 | 253 | __raw__={ |
@@ -233,15 +389,23 @@ class ActionExecutionDB_NewFieldType(ActionExecutionDB): |
233 | 389 | "$type": "object", |
234 | 390 | }, |
235 | 391 | } |
236 | | - ).update(set___cls="ActionExecutionDB") |
237 | | - |
| 392 | + ).update(set___cls="ActionExecutionDB.ActionExecutionDB_NewFieldType") |
| 393 | + execution_dbs = ActionExecution.query( |
| 394 | + __raw__={ |
| 395 | + "result": { |
| 396 | + "$not": { |
| 397 | + "$type": "binData", |
| 398 | + }, |
| 399 | + } |
| 400 | + } |
| 401 | + ) |
238 | 402 | LiveAction.query( |
239 | 403 | __raw__={ |
240 | 404 | "result": { |
241 | 405 | "$type": "object", |
242 | 406 | }, |
243 | 407 | } |
244 | | - ).update(set___cls="LiveActionDB") |
| 408 | + ).update(set___cls="LiveActionDB.LiveActionDB_NewFieldType") |
245 | 409 |
|
246 | 410 | # 2. Run migration |
247 | 411 | start_dt = datetime.datetime.utcnow().replace( |
|
0 commit comments