Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(api): fix hooks with empty UUID #5098

Merged
merged 1 commit into from
Apr 3, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions engine/api/api.go
Original file line number Diff line number Diff line change
@@ -707,6 +707,10 @@ func (a *API) Serve(ctx context.Context) error {
return migrate.CleanDuplicateHooks(ctx, a.DBConnectionFactory.GetDBMap(), a.Cache, false)
}})

migrate.Add(ctx, sdk.Migration{Name: "FixEmptyUUIDHooks", Release: "0.44.0", Blocker: false, Automatic: false, ExecFunc: func(ctx context.Context) error {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not delete these hooks?

return migrate.FixEmptyUUIDHooks(ctx, a.DBConnectionFactory.GetDBMap(), a.Cache)
}})

isFreshInstall, errF := version.IsFreshInstall(a.mustDB())
if errF != nil {
return sdk.WrapError(errF, "Unable to check if it's a fresh installation of CDS")
77 changes: 77 additions & 0 deletions engine/api/migrate/clean_duplicate_hooks.go
Original file line number Diff line number Diff line change
@@ -135,3 +135,80 @@ func cleanDuplicateHooks(ctx context.Context, db *gorp.DbMap, store cache.Store,

return nil
}

func FixEmptyUUIDHooks(ctx context.Context, db *gorp.DbMap, store cache.Store) error {
q := "select distinct(workflow.id) from w_node_hook join w_node on w_node.id = w_node_hook.node_id join workflow on workflow.id = w_node.workflow_id where uuid = ''"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

request must check workflow_data->node->hooks uuid that is the right data

var ids []int64

if _, err := db.Select(&ids, q); err != nil {
return sdk.WrapError(err, "unable to select workflow")
}

var mError = new(sdk.MultiError)
for _, id := range ids {
if err := fixEmptyUUIDHooks(ctx, db, store, id); err != nil {
mError.Append(err)
log.Error(ctx, "migrate.FixEmptyUUIDHooks> unable to clean workflow %d: %v", id, err)
}
}

if mError.IsEmpty() {
return nil
}
return mError
}

func fixEmptyUUIDHooks(ctx context.Context, db *gorp.DbMap, store cache.Store, workflowID int64) error {
tx, err := db.Begin()
if err != nil {
return sdk.WithStack(err)
}

defer tx.Rollback() // nolint

projectID, err := tx.SelectInt("SELECT project_id FROM workflow WHERE id = $1", workflowID)
if err != nil {
if err == sql.ErrNoRows {
return nil
}
return sdk.WithStack(err)
}

if projectID == 0 {
return nil
}

proj, err := project.LoadByID(tx, store, projectID,
project.LoadOptions.WithApplicationWithDeploymentStrategies,
project.LoadOptions.WithPipelines,
project.LoadOptions.WithEnvironments,
project.LoadOptions.WithIntegrations)
if err != nil {
return sdk.WrapError(err, "unable to load project %d", projectID)
}

w, err := workflow.LoadAndLockByID(ctx, tx, store, *proj, workflowID, workflow.LoadOptions{})
if err != nil {
if sdk.ErrorIs(err, sdk.ErrNotFound) {
return nil
}
return err
}

for i, h := range w.WorkflowData.Node.Hooks {
if h.UUID == "" {
w.WorkflowData.Node.Hooks[i].UUID = sdk.UUID()
}
}

if err := workflow.Update(ctx, tx, store, *proj, w, workflow.UpdateOptions{}); err != nil {
return err
}

if err := tx.Commit(); err != nil {
return err
}
log.Info(ctx, "migrate.fixEmptyUUIDHooks> workflow %s/%s (%d) has been cleaned", proj.Name, w.Name, w.ID)

return nil
}