1
+ from uuid import uuid4
2
+
1
3
import sqlalchemy as sa
2
- from uuid import UUID , uuid4
4
+
3
5
4
6
def get_resource_type_id_by_name (conn , name ):
5
7
result = conn .execute (
@@ -78,12 +80,12 @@ def create_missing_modify_note_workflows(conn):
78
80
79
81
def create_workflows (conn , new ):
80
82
"""
81
- Create a new workflow
83
+ Create a new workflow.
82
84
83
85
Args:
84
86
conn: DB connection as available in migration main file
85
87
new: an dict of your workflow data
86
-
88
+
87
89
Example:
88
90
>>> new_workflows = {
89
91
"workflow_name": {
@@ -121,13 +123,13 @@ def create_workflows(conn, new):
121
123
122
124
def create_fixed_inputs (conn , product_id , new ):
123
125
"""
124
- Created fixed inputs for a product
126
+ Create a fixed inputs for a product.
125
127
126
128
Args:
127
129
conn: DB connection as available in migration main file
128
130
product_id: UUID of the product to link to
129
131
new: an dict of your workflow data
130
-
132
+
131
133
Example:
132
134
>>> new = {
133
135
"fixed_input_1": "value",
@@ -138,23 +140,26 @@ def create_fixed_inputs(conn, product_id, new):
138
140
for key , value in new .items ():
139
141
uuids [key ] = uuid4 ()
140
142
conn .execute (
141
- sa .text ("""
143
+ sa .text (
144
+ """
142
145
INSERT INTO fixed_inputs (fixed_input_id, name, value, created_at, product_id)
143
146
VALUES (:fixed_input_id, :key, :value, now(), :product_id)
144
147
ON CONFLICT DO NOTHING;
145
- """ ),
148
+ """
149
+ ),
146
150
{"fixed_input_id" : uuids [key ], "key" : key , "value" : value , "product_id" : product_id },
147
151
)
148
152
return uuids
149
153
154
+
150
155
def create_products (conn , new ):
151
156
"""
152
- Create a new workflow
157
+ Create a new workflow.
153
158
154
159
Args:
155
160
conn: DB connection as available in migration main file
156
161
new: an dict of your workflow data
157
-
162
+
158
163
Example:
159
164
>>> new = {
160
165
"Example Product": {
@@ -186,37 +191,39 @@ def create_products(conn, new):
186
191
current_uuid = product ["product_id" ]
187
192
uuids [name ] = current_uuid
188
193
conn .execute (
189
- sa .text ("""
194
+ sa .text (
195
+ """
190
196
INSERT INTO products (product_id, name, description, product_type, tag, status, created_at)
191
197
VALUES (:product_id, :name, :description, :product_type, :tag, :status, now())
192
198
ON CONFLICT DO NOTHING;
193
- """ ),
199
+ """
200
+ ),
194
201
product ,
195
202
)
196
203
if "product_block_ids" in product :
197
204
for product_block_uuid in product ["product_block_ids" ]:
198
205
# Link many-to-many if product blocks are given.
199
- conn .execute (
200
- sa .text ("INSERT INTO product_product_blocks VALUES (:product_id, :product_block_id)" ),
201
- {
202
- "product_id" : current_uuid ,
203
- "product_block_id" : product_block_uuid ,
204
- },
205
- )
206
+ conn .execute (
207
+ sa .text ("INSERT INTO product_product_blocks VALUES (:product_id, :product_block_id)" ),
208
+ {
209
+ "product_id" : current_uuid ,
210
+ "product_block_id" : product_block_uuid ,
211
+ },
212
+ )
206
213
if "fixed_inputs" in product :
207
214
create_fixed_inputs (conn , current_uuid , product ["fixed_inputs" ])
208
215
return uuids
209
216
210
217
211
218
def create_product_blocks (conn , new ):
212
219
"""
213
- Create a new workflow
220
+ Create a new workflow.
214
221
215
222
Args:
216
223
conn: DB connection as available in migration main file
217
224
new: an dict of your workflow data
218
225
products: list of product block ids to link these product blocks to
219
-
226
+
220
227
Example:
221
228
>>> new = {
222
229
"Example Product Block": {
@@ -239,14 +246,16 @@ def create_product_blocks(conn, new):
239
246
product_block ["product_block_id" ] = str (product_block .get ("product_block_id" , uuid4 ()))
240
247
uuids [name ] = product_block ["product_block_id" ]
241
248
conn .execute (
242
- sa .text ("""
249
+ sa .text (
250
+ """
243
251
INSERT INTO product_blocks (product_block_id, name, description, tag, status, created_at)
244
252
VALUES (:product_block_id, :name, :description, :tag, :status, now())
245
253
ON CONFLICT DO NOTHING;
246
- """ ),
254
+ """
255
+ ),
247
256
product_block ,
248
257
)
249
-
258
+
250
259
return uuids
251
260
252
261
@@ -336,10 +345,9 @@ def delete_resource_types(conn, delete):
336
345
conn .execute (sa .text ("DELETE FROM resource_types WHERE resource_type in :obsolete" ), obsolete = tuple (delete ))
337
346
338
347
339
-
340
348
def create (conn , new ):
341
349
"""
342
- Call other functions in this file based on the schema
350
+ Call other functions in this file based on the schema.
343
351
344
352
Args:
345
353
conn: DB connection as available in migration main file
@@ -381,7 +389,7 @@ def create(conn, new):
381
389
"status": "active",
382
390
"resources": {
383
391
"resource_type1": "Resource description",
384
- "resource_type2": "Resource description"
392
+ "resource_type2": "Resource description"
385
393
}
386
394
},
387
395
"Generated UUID Product Block": {
@@ -391,14 +399,14 @@ def create(conn, new):
391
399
"status": "active",
392
400
"resources": {
393
401
"resource_type1": "Resource description",
394
- "resource_type3": "Resource description"
402
+ "resource_type3": "Resource description"
395
403
}
396
404
}
397
405
},
398
406
"resources": {
399
407
"Existing Product": {
400
408
"resource_type4": "Resource description",
401
- "resource_type5": "Resource description"
409
+ "resource_type5": "Resource description"
402
410
}
403
411
},
404
412
"workflows": {
@@ -413,7 +421,6 @@ def create(conn, new):
413
421
"""
414
422
resources = new .get ("resources" , {})
415
423
product_block_uuids = {}
416
- product_uuids = {}
417
424
418
425
if "product_blocks" in new :
419
426
for product_block_name , product_block in new ["product_blocks" ].items ():
@@ -423,7 +430,7 @@ def create(conn, new):
423
430
resources .update (res_dict )
424
431
del product_block ["resources" ]
425
432
product_block_uuids = create_product_blocks (conn , new ["product_blocks" ])
426
-
433
+
427
434
if "products" in new :
428
435
for product in new .get ("product_blocks" , {}).values ():
429
436
if "product_blocks" in product :
@@ -435,13 +442,13 @@ def create(conn, new):
435
442
except KeyError :
436
443
try :
437
444
product ["product_block_ids" ].append (get_product_block_id_by_name (conn , product_block_name ))
438
- except :
445
+ except Exception :
439
446
raise ValueError (f"{ product_block_name } is not a valid product block." )
440
447
del product ["product_blocks" ]
441
- product_uuids = create_products (conn , new ["products" ])
442
-
448
+ create_products (conn , new ["products" ])
449
+
443
450
if resources :
444
451
create_resource_types_for_product_blocks (conn , resources )
445
-
452
+
446
453
if "workflows" in new :
447
454
create_workflows (conn , new ["workflows" ])
0 commit comments