terrakube icon indicating copy to clipboard operation
terrakube copied to clipboard

Unable to delete VCS client after deleting Workspace

Open ValeriiVozniuk opened this issue 1 year ago • 2 comments

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

  1. Create VCS client.
  2. Create associated Workspace.
  3. Delete that Workspace.
  4. 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.

ValeriiVozniuk avatar Jul 22 '24 11:07 ValeriiVozniuk

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

alfespa17 avatar Jul 22 '24 16:07 alfespa17

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.

vtmocanu avatar Sep 29 '24 15:09 vtmocanu