Skip to content

Commit

Permalink
Default tags to an empty slice, make not null
Browse files Browse the repository at this point in the history
This adds a new migration v3 to adjust the tags column to ensure it's
never null, which helps simplify client implementations by not making
them deal with both null and empty array values.

Fixes #46.
  • Loading branch information
bgentry committed Nov 22, 2023
1 parent 98e8f60 commit 58d108e
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Changed

- Database schema v3: alter river_job tags column to set a default of `[]` and add not null constraint.

## [0.0.7] - 2023-11-20

### Changed
Expand Down
6 changes: 3 additions & 3 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,7 @@ func Test_Client_Insert(t *testing.T) {
require.Equal(t, (&noOpArgs{}).Kind(), jobRow.Kind)
require.Equal(t, PriorityDefault, jobRow.Priority)
require.Equal(t, QueueDefault, jobRow.Queue)
require.Equal(t, []string(nil), jobRow.Tags)
require.Equal(t, []string{}, jobRow.Tags)
})

t.Run("WithInsertOpts", func(t *testing.T) {
Expand Down Expand Up @@ -755,7 +755,7 @@ func Test_Client_InsertTx(t *testing.T) {
require.Equal(t, (&noOpArgs{}).Kind(), jobRow.Kind)
require.Equal(t, PriorityDefault, jobRow.Priority)
require.Equal(t, QueueDefault, jobRow.Queue)
require.Equal(t, []string(nil), jobRow.Tags)
require.Equal(t, []string{}, jobRow.Tags)

// Job is not visible outside of the transaction.
_, err = bundle.queries.JobGetByID(ctx, client.driver.GetDBPool(), jobRow.ID)
Expand Down Expand Up @@ -2790,7 +2790,7 @@ func TestInsert(t *testing.T) {
require.Equal(1, insertedJob.Priority)
// Default comes from database now(), and we can't know the exact value:
require.WithinDuration(time.Now(), insertedJob.ScheduledAt, 2*time.Second)
require.Equal([]string(nil), insertedJob.Tags)
require.Equal([]string{}, insertedJob.Tags)
// require.Equal([]byte("{}"), insertedJob.metadata)
},
},
Expand Down
6 changes: 5 additions & 1 deletion internal/dbadapter/db_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,10 @@ func (a *StandardAdapter) JobInsertManyTx(ctx context.Context, tx pgx.Tx, params
metadata = []byte("{}")
}

tags := params.Tags
if tags == nil {
tags = []string{}
}
insertJobsParams[i] = dbsqlc.JobInsertManyParams{
Args: params.EncodedArgs,
Kind: params.Kind,
Expand All @@ -306,7 +310,7 @@ func (a *StandardAdapter) JobInsertManyTx(ctx context.Context, tx pgx.Tx, params
Priority: int16(min(params.Priority, math.MaxInt16)),
Queue: params.Queue,
State: params.State,
Tags: params.Tags,
Tags: tags,
}
}

Expand Down
2 changes: 1 addition & 1 deletion internal/dbmigrate/002_initial_schema.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,4 @@ CREATE UNLOGGED TABLE river_leader(

CONSTRAINT name_length CHECK (char_length(name) > 0 AND char_length(name) < 128),
CONSTRAINT leader_id_length CHECK (char_length(leader_id) > 0 AND char_length(leader_id) < 128)
);
);
2 changes: 2 additions & 0 deletions internal/dbmigrate/003_river_job_tags_non_null.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE river_job ALTER COLUMN tags DROP NOT NULL,
ALTER COLUMN tags DROP DEFAULT;
2 changes: 2 additions & 0 deletions internal/dbmigrate/003_river_job_tags_non_null.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE river_job ALTER COLUMN tags SET NOT NULL,
ALTER COLUMN tags SET DEFAULT '{}';
4 changes: 2 additions & 2 deletions internal/dbsqlc/river_job.sql
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ CREATE TABLE river_job(
queue text NOT NULL DEFAULT 'default' ::text,
state river_job_state NOT NULL DEFAULT 'available' ::river_job_state,
scheduled_at timestamptz NOT NULL DEFAULT NOW(),
tags varchar(255)[],
tags varchar(255)[] NOT NULL DEFAULT '{}' ::varchar(255)[],
CONSTRAINT finalized_or_finalized_at_null CHECK ((state IN ('cancelled', 'completed', 'discarded') AND finalized_at IS NOT NULL) OR finalized_at IS NULL),
CONSTRAINT priority_in_range CHECK (priority >= 1 AND priority <= 4),
CONSTRAINT queue_length CHECK (char_length(queue) > 0 AND char_length(queue) < 128),
Expand Down Expand Up @@ -184,7 +184,7 @@ INSERT INTO river_job(
@queue::text,
coalesce(sqlc.narg('scheduled_at')::timestamptz, now()),
@state::river_job_state,
@tags::varchar(255)[]
coalesce(@tags::varchar(255)[], '{}')
) RETURNING *;

-- name: JobInsertMany :copyfrom
Expand Down
2 changes: 1 addition & 1 deletion internal/dbsqlc/river_job.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 58d108e

Please sign in to comment.