Skip to content

Commit 9781591

Browse files
committed
fix migration test for 3.5
1 parent 402cf7a commit 9781591

1 file changed

Lines changed: 171 additions & 7 deletions

File tree

st2common/tests/unit/migrations/test_v35_migrate_db_dict_field_values.py

Lines changed: 171 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,15 @@
1616
import sys
1717

1818
import datetime
19+
import mongoengine as me
1920

2021
from st2common.constants import action as action_constants
22+
from st2common.fields import ComplexDateTimeField
23+
from st2common.fields import JSONDictEscapedFieldCompatibilityField
2124
from st2common.models.db import stormbase
2225
from st2common.models.db.execution import ActionExecutionDB
2326
from st2common.models.db.liveaction import LiveActionDB
27+
from st2common.models.db.notification import NotificationSchema
2428
from st2common.models.db.workflow import WorkflowExecutionDB
2529
from st2common.models.db.workflow import TaskExecutionDB
2630
from st2common.models.db.trigger import TriggerInstanceDB
@@ -31,6 +35,7 @@
3135
from st2common.persistence.trigger import TriggerInstance
3236
from st2common.constants.triggers import TRIGGER_INSTANCE_PROCESSED
3337
from st2common.constants.triggers import TRIGGER_INSTANCE_PENDING
38+
from st2common.util import date as date_utils
3439

3540
from st2tests import DbTestCase
3641

@@ -41,7 +46,6 @@
4146

4247
import st2_migrate_db_dict_field_values as migration_module
4348

44-
4549
MOCK_RESULT_1 = {
4650
"foo": "bar1",
4751
"bar": 1,
@@ -79,19 +83,171 @@ def test_migrate_executions(self):
7983
LiveActionDB._meta["allow_inheritance"] = True
8084

8185
class ActionExecutionDB_OldFieldType(ActionExecutionDB):
86+
8287
result = stormbase.EscapedDynamicField(default={})
8388
liveaction = stormbase.EscapedDictField(required=True)
8489
parameters = stormbase.EscapedDynamicField(default={})
8590

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+
86122
class LiveActionDB_OldFieldType(LiveActionDB):
87123
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+
)
91213

92214
class ActionExecutionDB_NewFieldType(ActionExecutionDB):
93215
liveaction = stormbase.EscapedDictField(required=True)
94216
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)
95251

96252
execution_dbs = ActionExecution.query(
97253
__raw__={
@@ -233,15 +389,23 @@ class ActionExecutionDB_NewFieldType(ActionExecutionDB):
233389
"$type": "object",
234390
},
235391
}
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+
)
238402
LiveAction.query(
239403
__raw__={
240404
"result": {
241405
"$type": "object",
242406
},
243407
}
244-
).update(set___cls="LiveActionDB")
408+
).update(set___cls="LiveActionDB.LiveActionDB_NewFieldType")
245409

246410
# 2. Run migration
247411
start_dt = datetime.datetime.utcnow().replace(

0 commit comments

Comments
 (0)