2121from pydantic import ConfigDict
2222from sqlalchemy import TEXT , Column , String , UniqueConstraint
2323from sqlalchemy .dialects .mysql import MEDIUMTEXT
24- from sqlalchemy .orm import joinedload
24+ from sqlalchemy .orm import joinedload , selectinload
2525from sqlalchemy .sql .base import ExecutableOption
2626from sqlmodel import Field , Relationship , SQLModel
2727
5050from zenml .zen_stores .schemas .constants import MODEL_VERSION_TABLENAME
5151from zenml .zen_stores .schemas .pipeline_deployment_schemas import (
5252 PipelineDeploymentSchema ,
53+ StepConfigurationSchema ,
5354)
5455from zenml .zen_stores .schemas .pipeline_run_schemas import PipelineRunSchema
5556from zenml .zen_stores .schemas .project_schemas import ProjectSchema
@@ -187,6 +188,14 @@ class StepRunSchema(NamedSchema, RunMetadataInterface, table=True):
187188 original_step_run : Optional ["StepRunSchema" ] = Relationship (
188189 sa_relationship_kwargs = {"remote_side" : "StepRunSchema.id" }
189190 )
191+ step_configuration_schema : Optional ["StepConfigurationSchema" ] = (
192+ Relationship (
193+ sa_relationship_kwargs = dict (
194+ viewonly = True ,
195+ primaryjoin = "and_(foreign(StepConfigurationSchema.name) == StepRunSchema.name, foreign(StepConfigurationSchema.deployment_id) == StepRunSchema.deployment_id)" ,
196+ ),
197+ )
198+ )
190199
191200 model_config = ConfigDict (protected_namespaces = ()) # type: ignore[assignment]
192201
@@ -209,17 +218,25 @@ def get_query_options(
209218 Returns:
210219 A list of query options.
211220 """
212- from zenml .zen_stores .schemas import ModelVersionSchema
221+ from zenml .zen_stores .schemas import (
222+ ArtifactVersionSchema ,
223+ ModelVersionSchema ,
224+ )
213225
214226 options = [
215- joinedload (jl_arg (StepRunSchema .deployment )),
216- joinedload (jl_arg (StepRunSchema .pipeline_run )),
227+ selectinload (jl_arg (StepRunSchema .deployment )).load_only (
228+ jl_arg (PipelineDeploymentSchema .pipeline_configuration )
229+ ),
230+ selectinload (jl_arg (StepRunSchema .pipeline_run )).load_only (
231+ jl_arg (PipelineRunSchema .start_time )
232+ ),
233+ joinedload (jl_arg (StepRunSchema .step_configuration_schema )),
217234 ]
218235
219236 if include_metadata :
220237 options .extend (
221238 [
222- joinedload (jl_arg (StepRunSchema .logs )),
239+ selectinload (jl_arg (StepRunSchema .logs )),
223240 # joinedload(jl_arg(StepRunSchema.parents)),
224241 # joinedload(jl_arg(StepRunSchema.run_metadata)),
225242 ]
@@ -228,12 +245,28 @@ def get_query_options(
228245 if include_resources :
229246 options .extend (
230247 [
231- joinedload (jl_arg (StepRunSchema .model_version )).joinedload (
248+ selectinload (
249+ jl_arg (StepRunSchema .model_version )
250+ ).joinedload (
232251 jl_arg (ModelVersionSchema .model ), innerjoin = True
233252 ),
234- joinedload (jl_arg (StepRunSchema .user )),
235- # joinedload(jl_arg(StepRunSchema.input_artifacts)),
236- # joinedload(jl_arg(StepRunSchema.output_artifacts)),
253+ selectinload (jl_arg (StepRunSchema .user )),
254+ selectinload (jl_arg (StepRunSchema .input_artifacts ))
255+ .joinedload (
256+ jl_arg (StepRunInputArtifactSchema .artifact_version ),
257+ innerjoin = True ,
258+ )
259+ .joinedload (
260+ jl_arg (ArtifactVersionSchema .artifact ), innerjoin = True
261+ ),
262+ selectinload (jl_arg (StepRunSchema .output_artifacts ))
263+ .joinedload (
264+ jl_arg (StepRunOutputArtifactSchema .artifact_version ),
265+ innerjoin = True ,
266+ )
267+ .joinedload (
268+ jl_arg (ArtifactVersionSchema .artifact ), innerjoin = True
269+ ),
237270 ]
238271 )
239272
@@ -290,10 +323,7 @@ def to_model(
290323 """
291324 step = None
292325 if self .deployment is not None :
293- step_configurations = self .deployment .get_step_configurations (
294- include = [self .name ]
295- )
296- if step_configurations :
326+ if self .step_configuration_schema :
297327 pipeline_configuration = (
298328 PipelineConfiguration .model_validate_json (
299329 self .deployment .pipeline_configuration
@@ -304,7 +334,7 @@ def to_model(
304334 inplace = True ,
305335 )
306336 step = Step .from_dict (
307- json .loads (step_configurations [ 0 ] .config ),
337+ json .loads (self . step_configuration_schema .config ),
308338 pipeline_configuration = pipeline_configuration ,
309339 )
310340 if not step and self .step_configuration :
0 commit comments