@@ -134,17 +134,46 @@ BEGIN TRY
134134
135135 -- Validate the DAG
136136 BEGIN TRY
137- /*
138- TODO: Validate DAG, no circular references allowed...
139- */
140-
137+ -- DAG Check: Ensure that adding @ParentBatchId -> @BatchId does not form a cycle
141138 IF @CheckDag = ' Y'
142139 BEGIN
143- PRINT (' There should be some clever SQL added here to warn of circular relations...' )
140+ ;WITH Ancestors AS (
141+ SELECT BATCH_ID, PARENT_BATCH_ID
142+ FROM [omd].[BATCH_HIERARCHY]
143+ WHERE BATCH_ID = @ParentBatchId
144+
145+ UNION ALL
146+
147+ SELECT h .BATCH_ID , h .PARENT_BATCH_ID
148+ FROM [omd].[BATCH_HIERARCHY] h
149+ INNER JOIN Ancestors a ON h .BATCH_ID = a .PARENT_BATCH_ID
150+ )
151+ SELECT BATCH_ID
152+ INTO # DagViolation
153+ FROM Ancestors
154+ WHERE PARENT_BATCH_ID = @BatchId;
155+
156+ IF EXISTS (SELECT 1 FROM # DagViolation)
157+ BEGIN
158+ SET @LogMessage = ' Circular relationship detected: adding BatchId ' + CONVERT (NVARCHAR(10 ), @BatchId) +
159+ ' under ParentBatchId ' + CONVERT (NVARCHAR(10 ), @ParentBatchId) +
160+ ' would create a cycle.' ;
161+ SET @MessageLog = [omd].[AddLogMessage](' ERROR' , DEFAULT, DEFAULT, @LogMessage, @MessageLog);
162+ DROP TABLE IF EXISTS # DagViolation;
163+ GOTO EndOfProcedureFailure;
164+ END
165+
166+ DROP TABLE IF EXISTS # DagViolation;
144167 END
168+
169+
145170 END TRY
146171 BEGIN CATCH
147- -- TODO
172+ SET @LogMessage = ' Error testing for circular relationships in the DAG check.' ;
173+ SET @MessageLog = [omd].[AddLogMessage](' ERROR' , DEFAULT, DEFAULT, @LogMessage, @MessageLog);
174+ SET @SuccessIndicator = ' N' ;
175+
176+ THROW 50000 , @LogMessage, 1
148177 END CATCH
149178
150179 -- Find the Parent Batch Id
0 commit comments