@@ -183,38 +183,65 @@ ExtendedJobVersionRow upsertJobVersion(
183183 /**
184184 * Used to link an input dataset to a given job version.
185185 *
186- * @param jobVersionUuid The unique ID of the job version.
187186 * @param inputDatasetUuid The unique ID of the input dataset.
187+ * @param jobUuid The unique ID of the job.
188188 */
189- default void upsertInputDatasetFor (UUID jobVersionUuid , UUID inputDatasetUuid ) {
190- upsertInputOrOutputDatasetFor (jobVersionUuid , inputDatasetUuid , IoType .INPUT );
189+ default void upsertInputDatasetFor (UUID jobVersionUuid , UUID inputDatasetUuid , UUID jobUuid ) {
190+ markVersionIOMappingNotCurrent (jobVersionUuid , jobUuid , IoType .INPUT );
191+ upsertCurrentInputOrOutputDatasetFor (jobVersionUuid , inputDatasetUuid , jobUuid , IoType .INPUT );
192+ // TODO: include this in test -> check if jobUuid is set
191193 }
192194
193195 /**
194196 * Used to link an output dataset to a given job version.
195197 *
196- * @param jobVersionUuid The unique ID of the job version.
197198 * @param outputDatasetUuid The unique ID of the output dataset.
199+ * @param jobUuid The unique ID of the job.
198200 */
199- default void upsertOutputDatasetFor (UUID jobVersionUuid , UUID outputDatasetUuid ) {
200- upsertInputOrOutputDatasetFor (jobVersionUuid , outputDatasetUuid , IoType .OUTPUT );
201+ default void upsertOutputDatasetFor (UUID jobVersionUuid , UUID outputDatasetUuid , UUID jobUuid ) {
202+ markVersionIOMappingNotCurrent (jobVersionUuid , jobUuid , IoType .OUTPUT );
203+ upsertCurrentInputOrOutputDatasetFor (jobVersionUuid , outputDatasetUuid , jobUuid , IoType .OUTPUT );
204+ // TODO: include this in test -> check if jobUuid is set
201205 }
202206
207+ @ SqlUpdate (
208+ """
209+ UPDATE job_versions_io_mapping
210+ SET is_job_version_current = FALSE
211+ WHERE (job_uuid = :jobUuid OR symlink_target_job_uuid = :jobUuid)
212+ AND job_version_uuid != :jobVersionUuid
213+ AND io_type = :ioType
214+ AND is_job_version_current = TRUE;
215+ """ )
216+ void markVersionIOMappingNotCurrent (UUID jobVersionUuid , UUID jobUuid , IoType ioType );
217+
218+ @ SqlUpdate (
219+ """
220+ UPDATE job_versions_io_mapping
221+ SET is_job_version_current = FALSE
222+ WHERE (job_uuid = :jobUuid OR symlink_target_job_uuid = :jobUuid)
223+ AND io_type = :ioType
224+ AND is_job_version_current = TRUE;
225+ """ )
226+ void markVersionIOMappingNotCurrent (UUID jobUuid , IoType ioType );
227+
203228 /**
204229 * Used to upsert an input or output dataset to a given job version.
205230 *
206231 * @param jobVersionUuid The unique ID of the job version.
207232 * @param datasetUuid The unique ID of the output dataset
208233 * @param ioType The {@link IoType} of the dataset.
234+ * @param jobUuid The unique ID of the job.
209235 */
210236 @ SqlUpdate (
211237 """
212238 INSERT INTO job_versions_io_mapping (
213- job_version_uuid, dataset_uuid, io_type)
214- VALUES (:jobVersionUuid, :datasetUuid, :ioType)
215- ON CONFLICT DO NOTHING
239+ job_version_uuid, dataset_uuid, io_type, job_uuid, is_job_version_current )
240+ VALUES (:jobVersionUuid, :datasetUuid, :ioType, :jobUuid, TRUE )
241+ ON CONFLICT (job_version_uuid, dataset_uuid, io_type, job_uuid) DO UPDATE SET is_job_version_current = TRUE
216242 """ )
217- void upsertInputOrOutputDatasetFor (UUID jobVersionUuid , UUID datasetUuid , IoType ioType );
243+ void upsertCurrentInputOrOutputDatasetFor (
244+ UUID jobVersionUuid , UUID datasetUuid , UUID jobUuid , IoType ioType );
218245
219246 /**
220247 * Returns the input datasets to a given job version.
@@ -344,14 +371,14 @@ default BagOfJobVersionInfo upsertJobVersionOnRunTransition(
344371 jobVersionInputs .forEach (
345372 jobVersionInput -> {
346373 jobVersionDao .upsertInputDatasetFor (
347- jobVersionRow .getUuid (), jobVersionInput .getDatasetUuid ());
374+ jobVersionRow .getUuid (), jobVersionInput .getDatasetUuid (), jobRow . getUuid () );
348375 });
349376
350377 // Link the output datasets to the job version.
351378 jobVersionOutputs .forEach (
352379 jobVersionOutput -> {
353380 jobVersionDao .upsertOutputDatasetFor (
354- jobVersionRow .getUuid (), jobVersionOutput .getDatasetUuid ());
381+ jobVersionRow .getUuid (), jobVersionOutput .getDatasetUuid (), jobRow . getUuid () );
355382 });
356383
357384 // Link the job version to the run.
0 commit comments