1 """Automations models
2
3 Revision ID: 07ed05dfd4ec
4 Revises: bacc60edce16
5 Create Date: 2024-04-03 11:16:18.668168
6
7 """
8
9 from typing import List 1 ctx 1a
10
11 import sqlalchemy as sa 1 ctx 1a
12 from alembic import op 1 ctx 1a
13
14 import prefect 1 ctx 1a
15 from prefect . server . events . actions import ServerActionTypes 1 ctx 1a
16 from prefect . server . events . schemas . automations import Firing , ServerTriggerTypes 1 ctx 1a
17 from prefect . server . events . schemas . events import ReceivedEvent 1 ctx 1a
18
19 # revision identifiers, used by Alembic.
20 revision = "07ed05dfd4ec" 1 ctx 1a
21 down_revision = "bacc60edce16" 1 ctx 1a
22 branch_labels = None 1 ctx 1a
23 depends_on = None 1 ctx 1a
24
25
26 def upgrade ( ) : 1 ctx 1a
27 op . create_table ( 1 ctx 1a
28 "automation" ,
29 sa . Column ( "name" , sa . String ( ) , nullable = False ) ,
30 sa . Column ( "description" , sa . String ( ) , nullable = False ) ,
31 sa . Column ( "enabled" , sa . Boolean ( ) , server_default = "1" , nullable = False ) ,
32 sa . Column (
33 "trigger" ,
34 prefect . server . utilities . database . Pydantic ( ServerTriggerTypes ) ,
35 nullable = False ,
36 ) ,
37 sa . Column (
38 "actions" ,
39 prefect . server . utilities . database . Pydantic ( List [ ServerActionTypes ] ) ,
40 nullable = False ,
41 ) ,
42 sa . Column (
43 "actions_on_trigger" ,
44 prefect . server . utilities . database . Pydantic ( List [ ServerActionTypes ] ) ,
45 server_default = "[]" ,
46 nullable = False ,
47 ) ,
48 sa . Column (
49 "actions_on_resolve" ,
50 prefect . server . utilities . database . Pydantic ( List [ ServerActionTypes ] ) ,
51 server_default = "[]" ,
52 nullable = False ,
53 ) ,
54 sa . Column (
55 "id" ,
56 prefect . server . utilities . database . UUID ( ) ,
57 server_default = sa . text (
58 "(\n (\n lower(hex(randomblob(4)))\n || '-'\n || lower(hex(randomblob(2)))\n || '-4'\n || substr(lower(hex(randomblob(2))),2)\n || '-'\n || substr('89ab',abs(random()) % 4 + 1, 1)\n || substr(lower(hex(randomblob(2))),2)\n || '-'\n || lower(hex(randomblob(6)))\n )\n )"
59 ) ,
60 nullable = False ,
61 ) ,
62 sa . Column (
63 "created" ,
64 prefect . server . utilities . database . Timestamp ( timezone = True ) ,
65 server_default = sa . text ( "(strftime('%Y-%m-%d %H:%M:%f000', 'now'))" ) ,
66 nullable = False ,
67 ) ,
68 sa . Column (
69 "updated" ,
70 prefect . server . utilities . database . Timestamp ( timezone = True ) ,
71 server_default = sa . text ( "(strftime('%Y-%m-%d %H:%M:%f000', 'now'))" ) ,
72 nullable = False ,
73 ) ,
74 sa . PrimaryKeyConstraint ( "id" , name = op . f ( "pk_automation" ) ) ,
75 )
76 with op . batch_alter_table ( "automation" , schema = None ) as batch_op : 1 ctx 1a
77 batch_op . create_index ( 1 ctx 1a
78 batch_op . f ( "ix_automation__updated" ) , [ "updated" ] , unique = False
79 )
80
81 op . create_table ( 1 ctx 1a
82 "automation_bucket" ,
83 sa . Column (
84 "automation_id" , prefect . server . utilities . database . UUID ( ) , nullable = False
85 ) ,
86 sa . Column (
87 "trigger_id" , prefect . server . utilities . database . UUID ( ) , nullable = True
88 ) ,
89 sa . Column (
90 "bucketing_key" ,
91 prefect . server . utilities . database . JSON ( ) ,
92 nullable = False ,
93 ) ,
94 sa . Column (
95 "last_event" ,
96 prefect . server . utilities . database . Pydantic ( ReceivedEvent ) ,
97 nullable = True ,
98 ) ,
99 sa . Column (
100 "start" ,
101 prefect . server . utilities . database . Timestamp ( timezone = True ) ,
102 nullable = False ,
103 ) ,
104 sa . Column (
105 "end" ,
106 prefect . server . utilities . database . Timestamp ( timezone = True ) ,
107 nullable = False ,
108 ) ,
109 sa . Column ( "count" , sa . Integer ( ) , nullable = False ) ,
110 sa . Column ( "last_operation" , sa . String ( ) , nullable = True ) ,
111 sa . Column (
112 "triggered_at" ,
113 prefect . server . utilities . database . Timestamp ( timezone = True ) ,
114 nullable = False ,
115 ) ,
116 sa . Column (
117 "id" ,
118 prefect . server . utilities . database . UUID ( ) ,
119 server_default = sa . text (
120 "(\n (\n lower(hex(randomblob(4)))\n || '-'\n || lower(hex(randomblob(2)))\n || '-4'\n || substr(lower(hex(randomblob(2))),2)\n || '-'\n || substr('89ab',abs(random()) % 4 + 1, 1)\n || substr(lower(hex(randomblob(2))),2)\n || '-'\n || lower(hex(randomblob(6)))\n )\n )"
121 ) ,
122 nullable = False ,
123 ) ,
124 sa . Column (
125 "created" ,
126 prefect . server . utilities . database . Timestamp ( timezone = True ) ,
127 server_default = sa . text ( "(strftime('%Y-%m-%d %H:%M:%f000', 'now'))" ) ,
128 nullable = False ,
129 ) ,
130 sa . Column (
131 "updated" ,
132 prefect . server . utilities . database . Timestamp ( timezone = True ) ,
133 server_default = sa . text ( "(strftime('%Y-%m-%d %H:%M:%f000', 'now'))" ) ,
134 nullable = False ,
135 ) ,
136 sa . ForeignKeyConstraint (
137 [ "automation_id" ] ,
138 [ "automation.id" ] ,
139 name = op . f ( "fk_automation_bucket__automation_id__automation" ) ,
140 ondelete = "CASCADE" ,
141 ) ,
142 sa . PrimaryKeyConstraint ( "id" , name = op . f ( "pk_automation_bucket" ) ) ,
143 )
144 with op . batch_alter_table ( "automation_bucket" , schema = None ) as batch_op : 1 ctx 1a
145 batch_op . create_index ( 1 ctx 1a
146 "ix_automation_bucket__automation_id__end" ,
147 [ "automation_id" , "end" ] ,
148 unique = False ,
149 )
150 batch_op . create_index ( 1 ctx 1a
151 batch_op . f ( "ix_automation_bucket__updated" ) , [ "updated" ] , unique = False
152 )
153 batch_op . create_index ( 1 ctx 1a
154 "uq_automation_bucket__automation_id__bucketing_key" ,
155 [ "automation_id" , "bucketing_key" ] ,
156 unique = True ,
157 )
158
159 op . create_table ( 1 ctx 1a
160 "automation_related_resource" ,
161 sa . Column (
162 "automation_id" , prefect . server . utilities . database . UUID ( ) , nullable = False
163 ) ,
164 sa . Column ( "resource_id" , sa . String ( ) , nullable = True ) ,
165 sa . Column (
166 "automation_owned_by_resource" ,
167 sa . Boolean ( ) ,
168 server_default = "0" ,
169 nullable = False ,
170 ) ,
171 sa . Column (
172 "id" ,
173 prefect . server . utilities . database . UUID ( ) ,
174 server_default = sa . text (
175 "(\n (\n lower(hex(randomblob(4)))\n || '-'\n || lower(hex(randomblob(2)))\n || '-4'\n || substr(lower(hex(randomblob(2))),2)\n || '-'\n || substr('89ab',abs(random()) % 4 + 1, 1)\n || substr(lower(hex(randomblob(2))),2)\n || '-'\n || lower(hex(randomblob(6)))\n )\n )"
176 ) ,
177 nullable = False ,
178 ) ,
179 sa . Column (
180 "created" ,
181 prefect . server . utilities . database . Timestamp ( timezone = True ) ,
182 server_default = sa . text ( "(strftime('%Y-%m-%d %H:%M:%f000', 'now'))" ) ,
183 nullable = False ,
184 ) ,
185 sa . Column (
186 "updated" ,
187 prefect . server . utilities . database . Timestamp ( timezone = True ) ,
188 server_default = sa . text ( "(strftime('%Y-%m-%d %H:%M:%f000', 'now'))" ) ,
189 nullable = False ,
190 ) ,
191 sa . ForeignKeyConstraint (
192 [ "automation_id" ] ,
193 [ "automation.id" ] ,
194 name = op . f ( "fk_automation_related_resource__automation_id__automation" ) ,
195 ondelete = "CASCADE" ,
196 ) ,
197 sa . PrimaryKeyConstraint ( "id" , name = op . f ( "pk_automation_related_resource" ) ) ,
198 )
199 with op . batch_alter_table ( "automation_related_resource" , schema = None ) as batch_op : 1 ctx 1a
200 batch_op . create_index ( 1 ctx 1a
201 batch_op . f ( "ix_automation_related_resource__resource_id" ) ,
202 [ "resource_id" ] ,
203 unique = False ,
204 )
205 batch_op . create_index ( 1 ctx 1a
206 batch_op . f ( "ix_automation_related_resource__updated" ) ,
207 [ "updated" ] ,
208 unique = False ,
209 )
210 batch_op . create_index ( 1 ctx 1a
211 "uq_automation_related_resource__automation_id__resource_id" ,
212 [ "automation_id" , "resource_id" ] ,
213 unique = True ,
214 )
215
216 op . create_table ( 1 ctx 1a
217 "composite_trigger_child_firing" ,
218 sa . Column (
219 "automation_id" , prefect . server . utilities . database . UUID ( ) , nullable = False
220 ) ,
221 sa . Column (
222 "parent_trigger_id" ,
223 prefect . server . utilities . database . UUID ( ) ,
224 nullable = False ,
225 ) ,
226 sa . Column (
227 "child_trigger_id" , prefect . server . utilities . database . UUID ( ) , nullable = False
228 ) ,
229 sa . Column (
230 "child_firing_id" , prefect . server . utilities . database . UUID ( ) , nullable = False
231 ) ,
232 sa . Column (
233 "child_fired_at" ,
234 prefect . server . utilities . database . Timestamp ( timezone = True ) ,
235 nullable = True ,
236 ) ,
237 sa . Column (
238 "child_firing" ,
239 prefect . server . utilities . database . Pydantic ( Firing ) ,
240 nullable = False ,
241 ) ,
242 sa . Column (
243 "id" ,
244 prefect . server . utilities . database . UUID ( ) ,
245 server_default = sa . text (
246 "(\n (\n lower(hex(randomblob(4)))\n || '-'\n || lower(hex(randomblob(2)))\n || '-4'\n || substr(lower(hex(randomblob(2))),2)\n || '-'\n || substr('89ab',abs(random()) % 4 + 1, 1)\n || substr(lower(hex(randomblob(2))),2)\n || '-'\n || lower(hex(randomblob(6)))\n )\n )"
247 ) ,
248 nullable = False ,
249 ) ,
250 sa . Column (
251 "created" ,
252 prefect . server . utilities . database . Timestamp ( timezone = True ) ,
253 server_default = sa . text ( "(strftime('%Y-%m-%d %H:%M:%f000', 'now'))" ) ,
254 nullable = False ,
255 ) ,
256 sa . Column (
257 "updated" ,
258 prefect . server . utilities . database . Timestamp ( timezone = True ) ,
259 server_default = sa . text ( "(strftime('%Y-%m-%d %H:%M:%f000', 'now'))" ) ,
260 nullable = False ,
261 ) ,
262 sa . ForeignKeyConstraint (
263 [ "automation_id" ] ,
264 [ "automation.id" ] ,
265 name = op . f ( "fk_composite_trigger_child_firing__automation_id__automation" ) ,
266 ondelete = "CASCADE" ,
267 ) ,
268 sa . PrimaryKeyConstraint ( "id" , name = op . f ( "pk_composite_trigger_child_firing" ) ) ,
269 )
270 with op . batch_alter_table ( 1 ctx 1a
271 "composite_trigger_child_firing" , schema = None
272 ) as batch_op :
273 batch_op . create_index ( 1 ctx 1a
274 batch_op . f ( "ix_composite_trigger_child_firing__updated" ) ,
275 [ "updated" ] ,
276 unique = False ,
277 )
278 batch_op . create_index ( 1 ctx 1a
279 "uq_composite_trigger_child_firing__a_id__pt_id__ct__id" ,
280 [ "automation_id" , "parent_trigger_id" , "child_trigger_id" ] ,
281 unique = True ,
282 )
283
284
285 def downgrade ( ) : 1 ctx 1a
286 with op . batch_alter_table (
287 "composite_trigger_child_firing" , schema = None
288 ) as batch_op :
289 batch_op . drop_index ( "uq_composite_trigger_child_firing__a_id__pt_id__ct__id" )
290 batch_op . drop_index ( batch_op . f ( "ix_composite_trigger_child_firing__updated" ) )
291
292 op . drop_table ( "composite_trigger_child_firing" )
293 with op . batch_alter_table ( "automation_related_resource" , schema = None ) as batch_op :
294 batch_op . drop_index (
295 "uq_automation_related_resource__automation_id__resource_id"
296 )
297 batch_op . drop_index ( batch_op . f ( "ix_automation_related_resource__updated" ) )
298 batch_op . drop_index ( batch_op . f ( "ix_automation_related_resource__resource_id" ) )
299
300 op . drop_table ( "automation_related_resource" )
301 with op . batch_alter_table ( "automation_bucket" , schema = None ) as batch_op :
302 batch_op . drop_index ( "uq_automation_bucket__automation_id__bucketing_key" )
303 batch_op . drop_index ( batch_op . f ( "ix_automation_bucket__updated" ) )
304 batch_op . drop_index ( "ix_automation_bucket__automation_id__end" )
305
306 op . drop_table ( "automation_bucket" )
307 with op . batch_alter_table ( "automation" , schema = None ) as batch_op :
308 batch_op . drop_index ( batch_op . f ( "ix_automation__updated" ) )
309
310 op . drop_table ( "automation" )