Skip to content

Commit

Permalink
chore(job): adjust db tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MStreet3 committed Nov 20, 2024
1 parent e768aa8 commit f725669
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 14 deletions.
42 changes: 33 additions & 9 deletions core/services/job/job_orm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import (
"github.com/smartcontractkit/chainlink/v2/core/services/relay"
"github.com/smartcontractkit/chainlink/v2/core/services/vrf/vrfcommon"
"github.com/smartcontractkit/chainlink/v2/core/services/webhook"
"github.com/smartcontractkit/chainlink/v2/core/services/workflows/syncer"
"github.com/smartcontractkit/chainlink/v2/core/testdata/testspecs"
"github.com/smartcontractkit/chainlink/v2/core/utils/testutils/heavyweight"
)
Expand Down Expand Up @@ -1873,6 +1874,7 @@ func Test_ORM_FindJobByWorkflow(t *testing.T) {
c.ID = s.ID
c.Workflow = pkgworkflows.WFYamlSpec(t, "workflow99", addr1) // insert with mismatched name
c.SpecType = job.YamlSpec
c.SecretsID = s.SecretsID
return mustInsertWFJob(t, o, &c)
},
},
Expand All @@ -1892,29 +1894,40 @@ func Test_ORM_FindJobByWorkflow(t *testing.T) {
var c job.WorkflowSpec
c.ID = s.ID
c.Workflow = pkgworkflows.WFYamlSpec(t, "workflow03", addr2) // insert with mismatched owner
c.SecretsID = s.SecretsID
return mustInsertWFJob(t, o, &c)
},
},
wantErr: true,
},
}

for _, tt := range tests {
for i, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := testutils.Context(t)
ks := cltest.NewKeyStore(t, tt.fields.ds)

secretsORM := syncer.NewWorkflowRegistryDS(tt.fields.ds, logger.TestLogger(t))

sid, err := secretsORM.Create(ctx, "some-url.com", fmt.Sprintf("some-hash-%d", i), "some-contentz")
require.NoError(t, err)
tt.args.spec.SecretsID = sql.NullInt64{Int64: int64(sid), Valid: true}

Check failure on line 1914 in core/services/job/job_orm_test.go

View workflow job for this annotation

GitHub Actions / lint

unnecessary conversion (unconvert)

pipelineORM := pipeline.NewORM(tt.fields.ds, logger.TestLogger(t), configtest.NewTestGeneralConfig(t).JobPipeline().MaxSuccessfulRuns())
bridgesORM := bridges.NewORM(tt.fields.ds)
o := NewTestORM(t, tt.fields.ds, pipelineORM, bridgesORM, ks)

var wantJobID int32
if tt.args.before != nil {
wantJobID = tt.args.before(t, o, tt.args.spec)
}
ctx := testutils.Context(t)

gotJ, err := o.FindJobIDByWorkflow(ctx, *tt.args.spec)
if (err != nil) != tt.wantErr {
t.Errorf("orm.FindJobByWorkflow() error = %v, wantErr %v", err, tt.wantErr)
return
}

if err == nil {
assert.Equal(t, wantJobID, gotJ, "mismatch job id")
}
Expand All @@ -1936,25 +1949,36 @@ func Test_ORM_FindJobByWorkflow_Multiple(t *testing.T) {
bridges.NewORM(db),
cltest.NewKeyStore(t, db))
ctx := testutils.Context(t)
secretsORM := syncer.NewWorkflowRegistryDS(db, logger.TestLogger(t))

var sids []int64
for i := 0; i < 3; i++ {
sid, err := secretsORM.Create(ctx, "some-url.com", fmt.Sprintf("some-hash-%d", i), "some-contentz")
require.NoError(t, err)
sids = append(sids, sid)
}

wfYaml1 := pkgworkflows.WFYamlSpec(t, "workflow00", addr1)
s1 := job.WorkflowSpec{
Workflow: wfYaml1,
SpecType: job.YamlSpec,
Workflow: wfYaml1,
SpecType: job.YamlSpec,
SecretsID: sql.NullInt64{Int64: sids[0], Valid: true},
}
wantJobID1 := mustInsertWFJob(t, o, &s1)

wfYaml2 := pkgworkflows.WFYamlSpec(t, "workflow01", addr1)
s2 := job.WorkflowSpec{
Workflow: wfYaml2,
SpecType: job.YamlSpec,
Workflow: wfYaml2,
SpecType: job.YamlSpec,
SecretsID: sql.NullInt64{Int64: sids[1], Valid: true},
}
wantJobID2 := mustInsertWFJob(t, o, &s2)

wfYaml3 := pkgworkflows.WFYamlSpec(t, "workflow00", addr2)
s3 := job.WorkflowSpec{
Workflow: wfYaml3,
SpecType: job.YamlSpec,
Workflow: wfYaml3,
SpecType: job.YamlSpec,
SecretsID: sql.NullInt64{Int64: sids[2], Valid: true},
}
wantJobID3 := mustInsertWFJob(t, o, &s3)

Expand Down Expand Up @@ -1992,7 +2016,7 @@ func mustInsertWFJob(t *testing.T, orm job.ORM, s *job.WorkflowSpec) int32 {
}

err = orm.CreateJob(ctx, &j)
require.NoError(t, err, "failed to insert job with wf spec %v %s", s, s.Workflow)
require.NoError(t, err, "failed to insert job with wf spec %+v %s", s, err)
return j.ID
}

Expand Down
3 changes: 2 additions & 1 deletion core/services/job/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package job

import (
"context"
"database/sql"
"database/sql/driver"
"encoding/json"
"fmt"
Expand Down Expand Up @@ -879,7 +880,7 @@ type WorkflowSpec struct {
WorkflowName string `toml:"-" db:"workflow_name"` // Derived. Do not modify. the name of the workflow.
BinaryURL string `db:"binary_url"`
ConfigURL string `db:"config_url"`
SecretsID string `db:"secrets_id"`
SecretsID sql.NullInt64 `db:"secrets_id"`
CreatedAt time.Time `toml:"-"`
UpdatedAt time.Time `toml:"-"`
SpecType WorkflowSpecType `toml:"spec_type" db:"spec_type"`
Expand Down
4 changes: 2 additions & 2 deletions core/services/job/orm.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,8 +433,8 @@ func (o *orm) CreateJob(ctx context.Context, jb *Job) error {
case Stream:
// 'stream' type has no associated spec, nothing to do here
case Workflow:
sql := `INSERT INTO workflow_specs (workflow, workflow_id, workflow_owner, workflow_name, created_at, updated_at, spec_type, config)
VALUES (:workflow, :workflow_id, :workflow_owner, :workflow_name, NOW(), NOW(), :spec_type, :config)
sql := `INSERT INTO workflow_specs (workflow, workflow_id, workflow_owner, workflow_name, binary_url, config_url, secrets_id, created_at, updated_at, spec_type, config)
VALUES (:workflow, :workflow_id, :workflow_owner, :workflow_name, :binary_url, :config_url, :secrets_id, NOW(), NOW(), :spec_type, :config)
RETURNING id;`
specID, err := tx.prepareQuerySpecID(ctx, sql, jb.WorkflowSpec)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions core/store/migrate/migrations/0259_add_workflow_secrets.sql
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ CREATE INDEX idx_secrets_url ON workflow_secrets(secrets_url);

-- Alter the workflow_specs table
ALTER TABLE workflow_specs
ADD COLUMN binary_url TEXT,
ADD COLUMN config_url TEXT,
ADD COLUMN binary_url TEXT DEFAULT '',
ADD COLUMN config_url TEXT DEFAULT '',
ADD COLUMN secrets_id INT UNIQUE REFERENCES workflow_secrets(id) ON DELETE CASCADE;

-- Alter the config column type
Expand Down

0 comments on commit f725669

Please sign in to comment.