|
1 | 1 | """ SandboxMetadataDB class is a front-end to the metadata for sandboxes |
2 | 2 | """ |
3 | | -from DIRAC import S_ERROR, S_OK, gLogger |
| 3 | +from DIRAC import S_ERROR, S_OK |
4 | 4 | from DIRAC.ConfigurationSystem.Client.Helpers import Registry |
5 | 5 | from DIRAC.Core.Base.DB import DB |
6 | 6 | from DIRAC.Core.Security import Properties |
@@ -246,7 +246,7 @@ def unassignEntities(self, entities, requesterName, requesterGroup): |
246 | 246 | return None |
247 | 247 | conds = self.__entitiesByRequesterCond(requesterName, requesterGroup) |
248 | 248 |
|
249 | | - sqlCmd = "CREATE TEMPORARY TABLE to_delete_EntityId (EntityId VARCHAR(128) NOT NULL, PRIMARY KEY (EntityId)) ENGINE=MEMORY;" |
| 249 | + sqlCmd = "CREATE TEMPORARY TABLE to_delete_EntityId (EntityId VARCHAR(128) NOT NULL, PRIMARY KEY (EntityId)) ENGINE=MEMORY;" |
250 | 250 | returnValueOrRaise(self._update(sqlCmd)) |
251 | 251 | try: |
252 | 252 | sqlCmd = "INSERT INTO to_delete_EntityId (EntityId) VALUES ( %s )" |
@@ -313,16 +313,30 @@ def getUnusedSandboxes(self): |
313 | 313 | sqlCmd = f"SELECT SBId, SEName, SEPFN FROM `sb_SandBoxes` WHERE SEPFN not like '/S3/%' AND (( {' ) OR ( '.join(sqlCond)} ))" |
314 | 314 | return self._query(sqlCmd) |
315 | 315 |
|
| 316 | + @convertToReturnValue |
316 | 317 | def deleteSandboxes(self, SBIdList): |
317 | 318 | """ |
318 | | - Delete sandboxes |
| 319 | + Delete sandboxes using a temporary table for efficiency and consistency. |
319 | 320 | """ |
320 | | - sqlSBList = ", ".join([str(sbid) for sbid in SBIdList]) |
321 | | - for table in ("sb_SandBoxes", "sb_EntityMapping"): |
322 | | - sqlCmd = f"DELETE FROM `{table}` WHERE SBId IN ( {sqlSBList} )" |
323 | | - result = self._update(sqlCmd) |
324 | | - if not result["OK"]: |
325 | | - return result |
| 321 | + if not SBIdList: |
| 322 | + return S_OK() |
| 323 | + # Create temporary table |
| 324 | + sqlCmd = "CREATE TEMPORARY TABLE to_delete_SBId (SBId INTEGER(10) UNSIGNED NOT NULL, PRIMARY KEY (SBId)) ENGINE=MEMORY;" |
| 325 | + returnValueOrRaise(self._update(sqlCmd)) |
| 326 | + try: |
| 327 | + # Insert SBIds into temporary table |
| 328 | + sqlCmd = "INSERT INTO to_delete_SBId (SBId) VALUES (%s)" |
| 329 | + returnValueOrRaise(self._updatemany(sqlCmd, [(sbid,) for sbid in SBIdList])) |
| 330 | + # Delete from sb_EntityMapping first (to respect FK constraints if any) |
| 331 | + sqlCmd = "DELETE FROM `sb_EntityMapping` WHERE SBId IN (SELECT SBId FROM to_delete_SBId)" |
| 332 | + returnValueOrRaise(self._update(sqlCmd)) |
| 333 | + # Delete from sb_SandBoxes |
| 334 | + sqlCmd = "DELETE FROM `sb_SandBoxes` WHERE SBId IN (SELECT SBId FROM to_delete_SBId)" |
| 335 | + returnValueOrRaise(self._update(sqlCmd)) |
| 336 | + finally: |
| 337 | + # Drop temporary table |
| 338 | + sqlCmd = "DROP TEMPORARY TABLE to_delete_SBId" |
| 339 | + returnValueOrRaise(self._update(sqlCmd)) |
326 | 340 | return S_OK() |
327 | 341 |
|
328 | 342 | def getSandboxId(self, SEName, SEPFN, requesterName, requesterGroup, field="SBId"): |
|
0 commit comments