Unable to delete VCS client after deleting Workspace
Bug description 🐞
When I'm trying to delete a VCS client which have deleted Workspace associated with it, I'm getting an error:
ERROR: update or delete on table "vcs" violates foreign key constraint "fk_workspace_vcs" on table "workspace"\n Detail: Key (id)=(5c325b88-dbc2-412d-9fcc-b609eae1b033) is still referenced from table "workspace".
Per what I see in database, deleted Workspaces are staying in the workspace table just having _DEL_xxxx suffix added. I assume they both should be dropped at this point.
Steps to reproduce
- Create VCS client.
- Create associated Workspace.
- Delete that Workspace.
- Try to delete VCS client
Expected behavior
VCS client is deleted
Example repository
No response
Anything else?
I'm also don't see any mentions in documentation about that soft delete process in database, and how those deleted workspaces should be properly cleaned.
That is correct we only do a "soft delete" updating one flag called "deleted = true", we don't delete the record from the database
https://github.com/AzBuilder/terrakube/blob/bfe3413e91a04ef827306a02ee77778ee9e32eec/api/src/main/java/org/terrakube/api/rs/workspace/Workspace.java#L40
What we are currently deleting is the "backend storage" where we put all the logs, state, json, terraform plan, etc in this part of the code:
https://github.com/AzBuilder/terrakube/blob/bfe3413e91a04ef827306a02ee77778ee9e32eec/api/src/main/java/org/terrakube/api/plugin/softdelete/SoftDeleteService.java#L47
https://github.com/AzBuilder/terrakube/blob/bfe3413e91a04ef827306a02ee77778ee9e32eec/api/src/main/java/org/terrakube/api/plugin/softdelete/SoftDeleteService.java#L50
https://github.com/AzBuilder/terrakube/blob/bfe3413e91a04ef827306a02ee77778ee9e32eec/api/src/main/java/org/terrakube/api/plugin/scheduler/workspace/DeleteStorageBackendJob.java#L26
For now I think the alternative will be to add an additional update in the UI that calls the API and remove the relationship from the workspace that will be "soft deleted" in that way you could delete the VCS connection once all the relationships from the workspaces are removed.
It should be something similar to this code that we use to remove or add the relationship to a self hosted agent
https://github.com/AzBuilder/terrakube/blob/bfe3413e91a04ef827306a02ee77778ee9e32eec/ui/src/domain/Workspaces/Details.jsx#L501
https://github.com/AzBuilder/terrakube/blob/bfe3413e91a04ef827306a02ee77778ee9e32eec/ui/src/domain/Workspaces/Details.jsx#L513
I encountered the same issue, jut by trying to delete a VCS client, without deleting the workspace. I added a gitlab VCS as admin app then added gitlab VCS as user app and now I am unable to remove the initial gitlab VCS admin app client.