mirror of https://github.com/harness/drone.git
feat: [CDE-390]:add can delete column in gitspace_config table (#2827)
* fix err msg * update migration query * fix typo * feedback comments * add can delete column in gitspace_config tablepull/3576/head
parent
5cbd33bd5d
commit
21bdad8ece
|
@ -39,16 +39,19 @@ func (c *Controller) Delete(
|
|||
if err != nil {
|
||||
return fmt.Errorf("failed to find space: %w", err)
|
||||
}
|
||||
|
||||
err = apiauth.CheckGitspace(ctx, c.authorizer, session, space.Path, identifier, enum.PermissionGitspaceDelete)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to authorize: %w", err)
|
||||
}
|
||||
|
||||
gitspaceConfig, err := c.gitspaceConfigStore.FindByIdentifier(ctx, space.ID, identifier)
|
||||
gitspaceConfig.SpacePath = space.Path
|
||||
if err != nil || gitspaceConfig == nil {
|
||||
log.Err(err).Msg(gitspaceConfigNotFound + identifier)
|
||||
return err
|
||||
}
|
||||
|
||||
instance, _ := c.gitspaceInstanceStore.FindLatestByGitspaceConfigID(ctx, gitspaceConfig.ID)
|
||||
gitspaceConfig.GitspaceInstance = instance
|
||||
if instance == nil || instance.State == enum.GitspaceInstanceStateUninitialized {
|
||||
|
@ -56,10 +59,18 @@ func (c *Controller) Delete(
|
|||
if err = c.gitspaceSvc.UpdateConfig(ctx, gitspaceConfig); err != nil {
|
||||
return fmt.Errorf("failed to mark gitspace config as deleted: %w", err)
|
||||
}
|
||||
} else {
|
||||
ctxWithoutCancel := context.WithoutCancel(ctx)
|
||||
go c.removeGitspace(ctxWithoutCancel, *gitspaceConfig)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// mark can_delete for gitconfig as true so that if delete operation fails, cron job can clean up resources.
|
||||
gitspaceConfig.IsMarkedForDeletion = true
|
||||
if err = c.gitspaceSvc.UpdateConfig(ctx, gitspaceConfig); err != nil {
|
||||
return fmt.Errorf("failed to mark gitspace config is_marked_for_deletion column: %w", err)
|
||||
}
|
||||
|
||||
ctxWithoutCancel := context.WithoutCancel(ctx)
|
||||
go c.removeGitspace(ctxWithoutCancel, *gitspaceConfig)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
|
@ -51,7 +51,8 @@ const (
|
|||
gconf_is_deleted,
|
||||
gconf_code_repo_ref,
|
||||
gconf_ssh_token_identifier,
|
||||
gconf_created_by
|
||||
gconf_created_by,
|
||||
gconf_is_marked_for_deletion
|
||||
`
|
||||
gitspaceConfigsTable = `gitspace_configs`
|
||||
ReturningClause = "RETURNING "
|
||||
|
@ -73,13 +74,14 @@ type gitspaceConfig struct {
|
|||
DevcontainerPath null.String `db:"gconf_devcontainer_path"`
|
||||
Branch string `db:"gconf_branch"`
|
||||
// TODO: migrate to principal int64 id to use principal cache and consistent with Harness code.
|
||||
UserUID string `db:"gconf_user_uid"`
|
||||
SpaceID int64 `db:"gconf_space_id"`
|
||||
Created int64 `db:"gconf_created"`
|
||||
Updated int64 `db:"gconf_updated"`
|
||||
IsDeleted bool `db:"gconf_is_deleted"`
|
||||
SSHTokenIdentifier string `db:"gconf_ssh_token_identifier"`
|
||||
CreatedBy null.Int `db:"gconf_created_by"`
|
||||
UserUID string `db:"gconf_user_uid"`
|
||||
SpaceID int64 `db:"gconf_space_id"`
|
||||
Created int64 `db:"gconf_created"`
|
||||
Updated int64 `db:"gconf_updated"`
|
||||
IsDeleted bool `db:"gconf_is_deleted"`
|
||||
SSHTokenIdentifier string `db:"gconf_ssh_token_identifier"`
|
||||
CreatedBy null.Int `db:"gconf_created_by"`
|
||||
IsMarkedForDeletion bool `db:"gconf_is_marked_for_deletion"`
|
||||
}
|
||||
|
||||
var _ store.GitspaceConfigStore = (*gitspaceConfigStore)(nil)
|
||||
|
@ -197,6 +199,7 @@ func (s gitspaceConfigStore) Create(ctx context.Context, gitspaceConfig *types.G
|
|||
gitspaceConfig.CodeRepo.Ref,
|
||||
gitspaceConfig.SSHTokenIdentifier,
|
||||
gitspaceConfig.GitspaceUser.ID,
|
||||
gitspaceConfig.IsMarkedForDeletion,
|
||||
).
|
||||
Suffix(ReturningClause + "gconf_id")
|
||||
sql, args, err := stmt.ToSql()
|
||||
|
@ -221,6 +224,7 @@ func (s gitspaceConfigStore) Update(ctx context.Context,
|
|||
Set("gconf_updated", dbGitspaceConfig.Updated).
|
||||
Set("gconf_infra_provider_resource_id", dbGitspaceConfig.InfraProviderResourceID).
|
||||
Set("gconf_is_deleted", dbGitspaceConfig.IsDeleted).
|
||||
Set("gconf_is_marked_for_deletion", dbGitspaceConfig.IsMarkedForDeletion).
|
||||
Where("gconf_id = ?", gitspaceConfig.ID)
|
||||
sql, args, err := stmt.ToSql()
|
||||
if err != nil {
|
||||
|
@ -252,6 +256,7 @@ func mapToInternalGitspaceConfig(config *types.GitspaceConfig) *gitspaceConfig {
|
|||
UserUID: config.GitspaceUser.Identifier,
|
||||
SpaceID: config.SpaceID,
|
||||
IsDeleted: config.IsDeleted,
|
||||
IsMarkedForDeletion: config.IsMarkedForDeletion,
|
||||
Created: config.Created,
|
||||
Updated: config.Updated,
|
||||
SSHTokenIdentifier: config.SSHTokenIdentifier,
|
||||
|
|
|
@ -0,0 +1,2 @@
|
|||
ALTER TABLE gitspace_configs
|
||||
DROP COLUMN gconf_is_marked_for_deletion;
|
|
@ -0,0 +1,5 @@
|
|||
ALTER TABLE gitspace_configs
|
||||
ADD COLUMN gconf_is_marked_for_deletion BOOLEAN NOT NULL DEFAULT FALSE;
|
||||
|
||||
UPDATE gitspace_configs
|
||||
SET gconf_is_marked_for_deletion = gconf_is_deleted;
|
|
@ -0,0 +1,2 @@
|
|||
ALTER TABLE gitspace_configs
|
||||
DROP COLUMN gconf_is_marked_for_deletion;
|
|
@ -0,0 +1,5 @@
|
|||
ALTER TABLE gitspace_configs
|
||||
ADD COLUMN gconf_is_marked_for_deletion BOOLEAN NOT NULL DEFAULT FALSE;
|
||||
|
||||
UPDATE gitspace_configs
|
||||
SET gconf_is_marked_for_deletion = gconf_is_deleted;
|
|
@ -26,6 +26,7 @@ type GitspaceConfig struct {
|
|||
State enum.GitspaceStateType `json:"state"`
|
||||
SpaceID int64 `json:"-"`
|
||||
IsDeleted bool `json:"-"`
|
||||
IsMarkedForDeletion bool `json:"-"`
|
||||
GitspaceInstance *GitspaceInstance `json:"instance"`
|
||||
SpacePath string `json:"space_path"`
|
||||
Created int64 `json:"created"`
|
||||
|
|
Loading…
Reference in New Issue