Skip to content

Commit

Permalink
added stream job delete capability (#15690)
Browse files Browse the repository at this point in the history
* added stream job delete capability

* fixed typo in the query

* changeset

* Add changeset tag

* removed unnecessary named import

* removed unnecessary job generator for stream specs

---------

Co-authored-by: denis.chernov <[email protected]>
Co-authored-by: Ivaylo Novakov <[email protected]>
  • Loading branch information
3 people authored Dec 16, 2024
1 parent 74d6b5a commit ed6f486
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/five-gifts-end.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"chainlink": patch
---

#added stream job delete capability
13 changes: 13 additions & 0 deletions core/services/job/job_orm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import (
"github.com/smartcontractkit/chainlink/v2/core/services/ocrbootstrap"
"github.com/smartcontractkit/chainlink/v2/core/services/pipeline"
"github.com/smartcontractkit/chainlink/v2/core/services/relay"
"github.com/smartcontractkit/chainlink/v2/core/services/streams"
"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"
Expand Down Expand Up @@ -444,6 +445,18 @@ func TestORM_DeleteJob_DeletesAssociatedRecords(t *testing.T) {
cltest.AssertCount(t, db, "jobs", 0)
})

t.Run("it creates and deletes records for stream jobs", func(t *testing.T) {
ctx := testutils.Context(t)
jb, err := streams.ValidatedStreamSpec(testspecs.GenerateStreamSpec(testspecs.StreamSpecParams{Name: "Test-stream", StreamID: 1}).Toml())
require.NoError(t, err)
err = jobORM.CreateJob(ctx, &jb)
require.NoError(t, err)
cltest.AssertCount(t, db, "jobs", 1)
err = jobORM.DeleteJob(ctx, jb.ID, jb.Type)
require.NoError(t, err)
cltest.AssertCount(t, db, "jobs", 0)
})

t.Run("does not allow to delete external initiators if they have referencing external_initiator_webhook_specs", func(t *testing.T) {
// create new db because this will rollback transaction and poison it
db := pgtest.NewSqlxDB(t)
Expand Down
19 changes: 12 additions & 7 deletions core/services/job/orm.go
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,7 @@ func (o *orm) DeleteJob(ctx context.Context, id int32, jobType Type) error {
Workflow: `DELETE FROM workflow_specs WHERE id in (SELECT workflow_spec_id FROM deleted_jobs)`,
StandardCapabilities: `DELETE FROM standardcapabilities_specs WHERE id in (SELECT standard_capabilities_spec_id FROM deleted_jobs)`,
CCIP: `DELETE FROM ccip_specs WHERE id in (SELECT ccip_spec_id FROM deleted_jobs)`,
Stream: ``,
}
q, ok := queries[jobType]
if !ok {
Expand All @@ -757,7 +758,7 @@ func (o *orm) DeleteJob(ctx context.Context, id int32, jobType Type) error {
// and this query was taking ~40secs.
ctx, cancel := context.WithTimeout(sqlutil.WithoutDefaultTimeout(ctx), time.Minute)
defer cancel()
query := fmt.Sprintf(`
query := `
WITH deleted_jobs AS (
DELETE FROM jobs WHERE id = $1 RETURNING
id,
Expand All @@ -775,15 +776,19 @@ func (o *orm) DeleteJob(ctx context.Context, id int32, jobType Type) error {
gateway_spec_id,
workflow_spec_id,
standard_capabilities_spec_id,
ccip_spec_id
),
deleted_specific_specs AS (
%s
),
ccip_spec_id,
stream_id
),`
if len(q) > 0 {
query += fmt.Sprintf(`deleted_specific_specs AS (
%s
),`, q)
}
query += `
deleted_job_pipeline_specs AS (
DELETE FROM job_pipeline_specs WHERE job_id IN (SELECT id FROM deleted_jobs) RETURNING pipeline_spec_id
)
DELETE FROM pipeline_specs WHERE id IN (SELECT pipeline_spec_id FROM deleted_job_pipeline_specs)`, q)
DELETE FROM pipeline_specs WHERE id IN (SELECT pipeline_spec_id FROM deleted_job_pipeline_specs)`
res, err := o.ds.ExecContext(ctx, query, id)
if err != nil {
return errors.Wrap(err, "DeleteJob failed to delete job")
Expand Down

0 comments on commit ed6f486

Please sign in to comment.