Skip to content

Commit 1ba957c

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

File tree

3 files changed

+11
-19
lines changed

3 files changed

+11
-19
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 & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,15 @@ 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
5959
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
6060

6161
DROP TABLE IF EXISTS `PilotOutput`;
6262
CREATE TABLE `PilotOutput` (
6363
`PilotID` INT(11) UNSIGNED NOT NULL,
6464
`StdOutput` MEDIUMTEXT,
6565
`StdError` MEDIUMTEXT,
66-
PRIMARY KEY (`PilotID`)
66+
PRIMARY KEY (`PilotID`),
67+
FOREIGN KEY (`PilotID`) REFERENCES `PilotAgents`(`PilotID`) ON DELETE CASCADE
6768
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

src/DIRAC/WorkloadManagementSystem/DB/SandboxMetadataDB.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,11 @@ def __initializeDB(self):
6060
"Fields": {
6161
"SBId": "INTEGER(10) UNSIGNED NOT NULL",
6262
"EntityId": "VARCHAR(128) NOT NULL",
63-
"Type": "VARCHAR(64) NOT NULL",
63+
"Type": "ENUM('Input', 'Output') NOT NULL",
6464
},
6565
"Indexes": {"Entity": ["EntityId"], "SBIndex": ["SBId"]},
6666
"UniqueIndexes": {"Mapping": ["SBId", "EntityId", "Type"]},
67+
"ForeignKeys": {"SBId": "sb_SandBoxes.SBId"},
6768
}
6869

6970
for tableName in self.__tablesDesc:
@@ -323,12 +324,8 @@ def deleteSandboxes(self, SBIdList):
323324
Delete sandboxes
324325
"""
325326
sqlSBList = ", ".join([str(sbid) for sbid in SBIdList])
326-
for table in ("sb_SandBoxes", "sb_EntityMapping"):
327-
sqlCmd = f"DELETE FROM `{table}` WHERE SBId IN ( {sqlSBList} )"
328-
result = self._update(sqlCmd)
329-
if not result["OK"]:
330-
return result
331-
return S_OK()
327+
sqlCmd = f"DELETE FROM sb_SandBoxes WHERE SBId IN ( {sqlSBList} )"
328+
return self._update(sqlCmd)
332329

333330
def getSandboxId(self, SEName, SEPFN, requesterName, requesterGroup, field="SBId", requesterDN=None):
334331
"""

0 commit comments

Comments
 (0)