Skip to content

Commit f97273f

Browse files
committed
feat: added foreign keys
1 parent 49b527f commit f97273f

File tree

2 files changed

+7
-11
lines changed

2 files changed

+7
-11
lines changed

src/DIRAC/WorkloadManagementSystem/DB/PilotAgentsDB.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -193,19 +193,13 @@ def deletePilots(self, pilotIDs, conn=False):
193193
if not isinstance(pilotIDs, list):
194194
return S_ERROR("Input argument is not a List")
195195

196-
failed = []
197-
198196
result = self._escapeValues(pilotIDs)
199197
if not result["OK"]:
200198
return S_ERROR(f"Failed to remove pilot: {result['Value']}")
201199
stringIDs = ",".join(result["Value"])
202-
for table in ["PilotOutput", "JobToPilotMapping", "PilotAgents"]:
203-
result = self._update(f"DELETE FROM {table} WHERE PilotID in ({stringIDs})", conn=conn)
204-
if not result["OK"]:
205-
failed.append(table)
206-
207-
if failed:
208-
return S_ERROR(f"Failed to remove pilot from {', '.join(failed)} tables")
200+
result = self._update(f"DELETE FROM PilotAgents WHERE PilotID in ({stringIDs})", conn=conn)
201+
if not result["OK"]:
202+
return S_ERROR("Failed to remove pilots: ", result["Message"])
209203
return S_OK(pilotIDs)
210204

211205
##########################################################################################

src/DIRAC/WorkloadManagementSystem/DB/PilotAgentsDB.sql

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,9 @@ CREATE TABLE `JobToPilotMapping` (
5454
`PilotID` INT(11) UNSIGNED NOT NULL,
5555
`JobID` INT(11) UNSIGNED NOT NULL,
5656
`StartTime` DATETIME NOT NULL,
57-
KEY `JobID` (`JobID`),
58-
KEY `PilotID` (`PilotID`)
57+
PRIMARY KEY (`PilotID`, `JobID`),
58+
FOREIGN KEY (`PilotID`) REFERENCES `PilotAgents`(`PilotID`) ON DELETE CASCADE,
59+
FOREIGN KEY (`JobID`) REFERENCES `Jobs`(`JobID`) ON DELETE CASCADE
5960
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
6061

6162
DROP TABLE IF EXISTS `PilotOutput`;
@@ -64,4 +65,5 @@ CREATE TABLE `PilotOutput` (
6465
`StdOutput` MEDIUMTEXT,
6566
`StdError` MEDIUMTEXT,
6667
PRIMARY KEY (`PilotID`)
68+
FOREIGN KEY (`PilotID`) REFERENCES `PilotAgents`(`PilotID`) ON DELETE CASCADE
6769
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

0 commit comments

Comments
 (0)