@@ -87,83 +87,86 @@ async def create_project(
8787) -> AsyncIterator [Callable [..., Awaitable [ProjectAtDB ]]]:
8888 created_project_ids : list [str ] = []
8989
90- async def _ (
91- user : dict [str , Any ],
92- * ,
93- project_nodes_overrides : dict [str , Any ] | None = None ,
94- ** project_overrides ,
95- ) -> ProjectAtDB :
96-
97- project_uuid = uuid4 ()
98- with log_context (
99- logging .INFO ,
100- "Creating new project with uuid=%s" ,
101- project_uuid ,
102- logger = _logger ,
103- ) as log_ctx :
104-
105- project_values = {
106- "uuid" : f"{ project_uuid } " ,
107- "name" : faker .name (),
108- "type" : ProjectType .STANDARD .name ,
109- "description" : faker .text (),
110- "prj_owner" : user ["id" ],
111- "access_rights" : {"1" : {"read" : True , "write" : True , "delete" : True }},
112- "thumbnail" : "" ,
113- ** project_overrides ,
114- }
115- project_workbench = project_values .pop ("workbench" , {})
116-
117- async with sqlalchemy_async_engine .connect () as con , con .begin ():
118- result = await con .execute (
119- projects .insert ()
120- .values (** project_values )
121- .returning (sa .literal_column ("*" ))
90+ async with contextlib .AsyncExitStack () as stack :
91+
92+ async def _ (
93+ user : dict [str , Any ],
94+ * ,
95+ project_nodes_overrides : dict [str , Any ] | None = None ,
96+ ** project_overrides ,
97+ ) -> ProjectAtDB :
98+
99+ project_uuid = uuid4 ()
100+ with log_context (
101+ logging .INFO ,
102+ "Creating new project with uuid=%s" ,
103+ project_uuid ,
104+ logger = _logger ,
105+ ) as log_ctx :
106+
107+ project_values = {
108+ "uuid" : f"{ project_uuid } " ,
109+ "name" : faker .name (),
110+ "type" : ProjectType .STANDARD .name ,
111+ "description" : faker .text (),
112+ "prj_owner" : user ["id" ],
113+ "access_rights" : {
114+ "1" : {"read" : True , "write" : True , "delete" : True }
115+ },
116+ "thumbnail" : "" ,
117+ ** project_overrides ,
118+ }
119+ project_workbench = project_values .pop ("workbench" , {})
120+
121+ project_db_rows = await stack .enter_async_context (
122+ insert_and_get_row_lifespan (
123+ sqlalchemy_async_engine ,
124+ table = projects ,
125+ values = project_values ,
126+ pk_col = projects .c .uuid ,
127+ )
122128 )
123-
124129 inserted_project = ProjectAtDB .model_validate (
125- {** dict ( result . one (). _asdict ()) , "workbench" : project_workbench }
130+ {** project_db_rows , "workbench" : project_workbench }
126131 )
127132
128- project_nodes_repo = ProjectNodesRepo (project_uuid = project_uuid )
129-
130- for node_id , node_data in project_workbench .items ():
131- # NOTE: currently no resources is passed until it becomes necessary
132- node_values = {
133- "required_resources" : {},
134- "key" : random_service_key (fake = faker ),
135- "version" : random_service_version (fake = faker ),
136- "label" : faker .pystr (),
137- ** node_data ,
138- }
139-
140- if project_nodes_overrides :
141- node_values .update (project_nodes_overrides )
142-
143- await project_nodes_repo .add (
144- con ,
145- nodes = [
146- ProjectNodeCreate (node_id = NodeID (node_id ), ** node_values )
147- ],
148- )
149-
150- await con .execute (
151- projects_to_products .insert ().values (
152- project_uuid = f"{ inserted_project .uuid } " ,
153- product_name = product_name ,
133+ async with sqlalchemy_async_engine .connect () as con , con .begin ():
134+ project_nodes_repo = ProjectNodesRepo (project_uuid = project_uuid )
135+
136+ for node_id , node_data in project_workbench .items ():
137+ # NOTE: currently no resources is passed until it becomes necessary
138+ node_values = {
139+ "required_resources" : {},
140+ "key" : random_service_key (fake = faker ),
141+ "version" : random_service_version (fake = faker ),
142+ "label" : faker .pystr (),
143+ ** node_data ,
144+ }
145+
146+ if project_nodes_overrides :
147+ node_values .update (project_nodes_overrides )
148+
149+ await project_nodes_repo .add (
150+ con ,
151+ nodes = [
152+ ProjectNodeCreate (
153+ node_id = NodeID (node_id ), ** node_values
154+ )
155+ ],
156+ )
157+
158+ await con .execute (
159+ projects_to_products .insert ().values (
160+ project_uuid = f"{ inserted_project .uuid } " ,
161+ product_name = product_name ,
162+ )
154163 )
155- )
156- log_ctx .logger .info ("Created project %s" , inserted_project )
157- created_project_ids .append (f"{ inserted_project .uuid } " )
158- return inserted_project
164+ log_ctx .logger .info ("Created project %s" , inserted_project )
165+ created_project_ids .append (f"{ inserted_project .uuid } " )
166+ return inserted_project
159167
160- yield _
168+ yield _
161169
162- # cleanup
163- async with sqlalchemy_async_engine .begin () as con :
164- await con .execute (
165- projects .delete ().where (projects .c .uuid .in_ (created_project_ids ))
166- )
167170 _logger .info ("<-- delete projects %s" , created_project_ids )
168171
169172
0 commit comments