diff --git a/src/DIRAC/Core/Utilities/MySQL.py b/src/DIRAC/Core/Utilities/MySQL.py index 25f282ad985..712b8fdf5de 100755 --- a/src/DIRAC/Core/Utilities/MySQL.py +++ b/src/DIRAC/Core/Utilities/MySQL.py @@ -1042,7 +1042,7 @@ def _createTables(self, tableDict, force=False): # cmdList.append( '`%s` %s' % ( forTable, tableDict[forTable]['Fields'][forKey] ) cmdList.append( "FOREIGN KEY ( `%s` ) REFERENCES `%s` ( `%s` )" - " ON DELETE RESTRICT" % (key, forTable, forKey) + " ON DELETE CASCADE" % (key, forTable, forKey) ) engine = thisTable.get("Engine", "InnoDB") diff --git a/src/DIRAC/WorkloadManagementSystem/DB/PilotAgentsDB.py b/src/DIRAC/WorkloadManagementSystem/DB/PilotAgentsDB.py index 5e66cd54a0b..d3962300456 100755 --- a/src/DIRAC/WorkloadManagementSystem/DB/PilotAgentsDB.py +++ b/src/DIRAC/WorkloadManagementSystem/DB/PilotAgentsDB.py @@ -193,19 +193,13 @@ def deletePilots(self, pilotIDs, conn=False): if not isinstance(pilotIDs, list): return S_ERROR("Input argument is not a List") - failed = [] - result = self._escapeValues(pilotIDs) if not result["OK"]: return S_ERROR(f"Failed to remove pilot: {result['Value']}") stringIDs = ",".join(result["Value"]) - for table in ["PilotOutput", "JobToPilotMapping", "PilotAgents"]: - result = self._update(f"DELETE FROM {table} WHERE PilotID in ({stringIDs})", conn=conn) - if not result["OK"]: - failed.append(table) - - if failed: - return S_ERROR(f"Failed to remove pilot from {', '.join(failed)} tables") + result = self._update(f"DELETE FROM PilotAgents WHERE PilotID in ({stringIDs})", conn=conn) + if not result["OK"]: + return S_ERROR("Failed to remove pilots: ", result["Message"]) return S_OK(pilotIDs) ########################################################################################## diff --git a/src/DIRAC/WorkloadManagementSystem/DB/PilotAgentsDB.sql b/src/DIRAC/WorkloadManagementSystem/DB/PilotAgentsDB.sql index 7168e529f74..3756c498a32 100755 --- a/src/DIRAC/WorkloadManagementSystem/DB/PilotAgentsDB.sql +++ b/src/DIRAC/WorkloadManagementSystem/DB/PilotAgentsDB.sql @@ -54,8 +54,8 @@ CREATE TABLE `JobToPilotMapping` ( `PilotID` INT(11) UNSIGNED NOT NULL, `JobID` INT(11) UNSIGNED NOT NULL, `StartTime` DATETIME NOT NULL, - KEY `JobID` (`JobID`), - KEY `PilotID` (`PilotID`) + PRIMARY KEY (`PilotID`, `JobID`), + FOREIGN KEY (`PilotID`) REFERENCES `PilotAgents`(`PilotID`) ON DELETE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; DROP TABLE IF EXISTS `PilotOutput`; @@ -63,5 +63,6 @@ CREATE TABLE `PilotOutput` ( `PilotID` INT(11) UNSIGNED NOT NULL, `StdOutput` MEDIUMTEXT, `StdError` MEDIUMTEXT, - PRIMARY KEY (`PilotID`) + PRIMARY KEY (`PilotID`), + FOREIGN KEY (`PilotID`) REFERENCES `PilotAgents`(`PilotID`) ON DELETE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; diff --git a/src/DIRAC/WorkloadManagementSystem/DB/SandboxMetadataDB.py b/src/DIRAC/WorkloadManagementSystem/DB/SandboxMetadataDB.py index 639beb8afeb..cbedcad1bad 100644 --- a/src/DIRAC/WorkloadManagementSystem/DB/SandboxMetadataDB.py +++ b/src/DIRAC/WorkloadManagementSystem/DB/SandboxMetadataDB.py @@ -60,10 +60,11 @@ def __initializeDB(self): "Fields": { "SBId": "INTEGER(10) UNSIGNED NOT NULL", "EntityId": "VARCHAR(128) NOT NULL", - "Type": "VARCHAR(64) NOT NULL", + "Type": "ENUM('Input', 'Output') NOT NULL", }, "Indexes": {"Entity": ["EntityId"], "SBIndex": ["SBId"]}, "UniqueIndexes": {"Mapping": ["SBId", "EntityId", "Type"]}, + "ForeignKeys": {"SBId": "sb_SandBoxes.SBId"}, } for tableName in self.__tablesDesc: @@ -323,12 +324,8 @@ def deleteSandboxes(self, SBIdList): Delete sandboxes """ sqlSBList = ", ".join([str(sbid) for sbid in SBIdList]) - for table in ("sb_SandBoxes", "sb_EntityMapping"): - sqlCmd = f"DELETE FROM `{table}` WHERE SBId IN ( {sqlSBList} )" - result = self._update(sqlCmd) - if not result["OK"]: - return result - return S_OK() + sqlCmd = f"DELETE FROM sb_SandBoxes WHERE SBId IN ( {sqlSBList} )" + return self._update(sqlCmd) def getSandboxId(self, SEName, SEPFN, requesterName, requesterGroup, field="SBId", requesterDN=None): """ diff --git a/src/DIRAC/WorkloadManagementSystem/DB/TaskQueueDB.py b/src/DIRAC/WorkloadManagementSystem/DB/TaskQueueDB.py index f235c50ac2a..5a023aa70b1 100755 --- a/src/DIRAC/WorkloadManagementSystem/DB/TaskQueueDB.py +++ b/src/DIRAC/WorkloadManagementSystem/DB/TaskQueueDB.py @@ -319,18 +319,7 @@ def cleanOrphanedTaskQueues(self, connObj=False): if not orphanedTQs: return S_OK() orphanedTQs = [str(otq[0]) for otq in orphanedTQs] - - for mvField in multiValueDefFields: - result = self._update( - f"DELETE FROM `tq_TQTo{mvField}` WHERE TQId in ( {','.join(orphanedTQs)} )", conn=connObj - ) - if not result["OK"]: - return result - - result = self._update(f"DELETE FROM `tq_TaskQueues` WHERE TQId in ( {','.join(orphanedTQs)} )", conn=connObj) - if not result["OK"]: - return result - return S_OK() + return self._update(f"DELETE FROM `tq_TaskQueues` WHERE TQId in ( {','.join(orphanedTQs)} )", conn=connObj) def __setTaskQueueEnabled(self, tqId, enabled=True, connObj=False): if enabled: @@ -990,10 +979,6 @@ def deleteTaskQueueIfEmpty(self, tqId, tqOwner=False, tqOwnerGroup=False, connOb tqToDel = retVal["Value"] if tqToDel: - for mvField in multiValueDefFields: - retVal = self._update(f"DELETE FROM `tq_TQTo{mvField}` WHERE TQId = {tqId}", conn=connObj) - if not retVal["OK"]: - return retVal retVal = self._update(f"DELETE FROM `tq_TaskQueues` WHERE TQId = {tqId}", conn=connObj) if not retVal["OK"]: return retVal diff --git a/tests/Integration/WorkloadManagementSystem/Test_SandboxMetadataDB.py b/tests/Integration/WorkloadManagementSystem/Test_SandboxMetadataDB.py index 6da5c5303f7..1b8f0a5872a 100644 --- a/tests/Integration/WorkloadManagementSystem/Test_SandboxMetadataDB.py +++ b/tests/Integration/WorkloadManagementSystem/Test_SandboxMetadataDB.py @@ -28,7 +28,7 @@ def test_SandboxMetadataDB(): print(f"sbId:{sbId}") print(f"newSandbox:{newSandbox}") - assignTo = {owner: [(f"SB:{sbSE}|{sbPFN}", ownerGroup)]} + assignTo = {123: [(f"SB:{sbSE}|{sbPFN}", "Input")]} res = smDB.assignSandboxesToEntities(assignTo, owner, ownerGroup) assert res["OK"], res["Message"] assert res["Value"] == 1