Skip to content

Commit

Permalink
[CAPPL-394] Some database changes
Browse files Browse the repository at this point in the history
- Allow multiple workflows to reference the same secrets file
- Change workflow specs uniqueness constraint so it includes the
  workflow id
  • Loading branch information
cedric-cordenier committed Dec 13, 2024
1 parent 8f1b956 commit 35c685a
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 4 deletions.
4 changes: 2 additions & 2 deletions core/services/workflows/syncer/orm.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ func (orm *orm) UpsertWorkflowSpec(ctx context.Context, spec *job.WorkflowSpec)
:created_at,
:updated_at,
:spec_type
) ON CONFLICT (workflow_owner, workflow_name) DO UPDATE
) ON CONFLICT (workflow_owner, workflow_name, workflow_id) DO UPDATE
SET
workflow = EXCLUDED.workflow,
config = EXCLUDED.config,
Expand Down Expand Up @@ -322,7 +322,7 @@ func (orm *orm) UpsertWorkflowSpecWithSecrets(
:created_at,
:updated_at,
:spec_type
) ON CONFLICT (workflow_owner, workflow_name) DO UPDATE
) ON CONFLICT (workflow_owner, workflow_name, workflow_id) DO UPDATE
SET
workflow = EXCLUDED.workflow,
config = EXCLUDED.config,
Expand Down
42 changes: 42 additions & 0 deletions core/services/workflows/syncer/orm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"testing"
"time"

"github.com/google/uuid"

"github.com/smartcontractkit/chainlink/v2/core/internal/testutils"
"github.com/smartcontractkit/chainlink/v2/core/internal/testutils/pgtest"
"github.com/smartcontractkit/chainlink/v2/core/logger"
Expand Down Expand Up @@ -372,4 +374,44 @@ func Test_UpsertWorkflowSpecWithSecrets(t *testing.T) {
require.NoError(t, err)
require.Equal(t, "new contents", contents)
})

t.Run("updates existing spec and secrets if spec has executions", func(t *testing.T) {
giveURL := "https://example.com"
giveBytes, err := crypto.Keccak256([]byte(giveURL))
require.NoError(t, err)
giveHash := hex.EncodeToString(giveBytes)
giveContent := "some contents"

spec := &job.WorkflowSpec{
Workflow: "test_workflow",
Config: "test_config",
WorkflowID: "cid-123",
WorkflowOwner: "owner-123",
WorkflowName: "Test Workflow",
Status: job.WorkflowSpecStatusActive,
BinaryURL: "http://example.com/binary",
ConfigURL: "http://example.com/config",
CreatedAt: time.Now(),
SpecType: job.WASMFile,
}

_, err = orm.UpsertWorkflowSpecWithSecrets(ctx, spec, giveURL, giveHash, giveContent)
require.NoError(t, err)

_, err = db.ExecContext(
ctx,
`INSERT INTO workflow_executions (id, workflow_id, status, created_at) VALUES ($1, $2, $3, $4)`,
uuid.New().String(),
"cid-123",
"started",
time.Now(),
)
require.NoError(t, err)

// Update the status
spec.WorkflowID = "cid-456"

_, err = orm.UpsertWorkflowSpecWithSecrets(ctx, spec, giveURL, giveHash, "new contents")
require.NoError(t, err)
})
}
2 changes: 1 addition & 1 deletion core/services/workflows/syncer/workflow_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ func (w *workflowRegistry) readRegistryEvents(ctx context.Context, reader Contra
for _, event := range events {
err := w.handler.Handle(ctx, event.Event)
if err != nil {
w.lggr.Errorw("failed to handle event", "err", err)
w.lggr.Errorw("failed to handle event", "err", err, "type", event.Event.EventType)
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion core/store/migrate/migrations/0259_add_workflow_secrets.sql
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,5 @@ DROP INDEX IF EXISTS idx_secrets_url_hash;

-- Drop the workflow_artifacts table
DROP TABLE IF EXISTS workflow_secrets;
-- +goose StatementEnd
-- +goose StatementEnd

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
-- +goose Up
-- +goose StatementBegin
-- unique constraint on workflow_owner and workflow_name
ALTER TABLE workflow_specs DROP CONSTRAINT unique_workflow_owner_name;
ALTER TABLE workflow_specs ADD CONSTRAINT unique_workflow_owner_name_id unique (workflow_owner, workflow_name, workflow_id);
ALTER TABLE workflow_specs DROP CONSTRAINT workflow_specs_secrets_id_key;
-- +goose StatementEnd

-- +goose Down
-- +goose StatementBegin
ALTER TABLE workflow_specs DROP CONSTRAINT unique_workflow_owner_name_id;
ALTER TABLE workflow_specs ADD CONSTRAINT unique_workflow_owner_name unique (workflow_owner, workflow_name);
ALTER TABLE workflow_specs ADD CONSTRAINT workflow_specs_secrets_id_key unique (secrets_id);
-- +goose StatementEnd

0 comments on commit 35c685a

Please sign in to comment.