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: turns out ON CONFLICT DO NOTHING doesn't work with RETURNING #2382

Merged
merged 1 commit into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
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
42 changes: 42 additions & 0 deletions backend/controller/dal/dal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"io"
"reflect"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -369,6 +370,47 @@ func TestDAL(t *testing.T) {
})
}

func TestCreateArtefactConflict(t *testing.T) {
ctx := log.ContextWithNewDefaultLogger(context.Background())
conn := sqltest.OpenForTesting(ctx, t)
dal, err := New(ctx, conn, optional.None[string]())
assert.NoError(t, err)

idch := make(chan sha256.SHA256, 2)

wg := sync.WaitGroup{}
wg.Add(2)
createContent := func() {
defer wg.Done()
tx1, err := dal.Begin(ctx)
assert.NoError(t, err)
digest, err := tx1.CreateArtefact(ctx, []byte("content"))
assert.NoError(t, err)
time.Sleep(time.Second * 2)
err = tx1.Commit(ctx)
assert.NoError(t, err)
idch <- digest
}

go createContent()
go createContent()

wg.Wait()

ids := []sha256.SHA256{}

for range 2 {
select {
case id := <-idch:
ids = append(ids, id)
case <-time.After(time.Second * 3):
t.Fatal("Timed out waiting for artefact creation")
}
}
assert.Equal(t, 2, len(ids))
assert.Equal(t, ids[0], ids[1])
}

func artefactContent(t testing.TB, artefacts []*model.Artefact) [][]byte {
t.Helper()
var result [][]byte
Expand Down
3 changes: 2 additions & 1 deletion backend/controller/sql/queries.sql
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ WHERE deployment_id = $1;
-- Create a new artefact and return the artefact ID.
INSERT INTO artefacts (digest, content)
VALUES ($1, $2)
ON CONFLICT (digest) DO NOTHING
ON CONFLICT (digest)
DO UPDATE SET digest = EXCLUDED.digest
RETURNING id;

-- name: AssociateArtefactWithDeployment :exec
Expand Down
3 changes: 2 additions & 1 deletion backend/controller/sql/queries.sql.go

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

2 changes: 1 addition & 1 deletion cmd/ftl/cmd_serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type serveCmd struct {
Stop bool `help:"Stop the running FTL instance. Can be used with --background to restart the server" default:"false"`
StartupTimeout time.Duration `help:"Timeout for the server to start up." default:"1m"`
ObservabilityConfig observability.Config `embed:"" prefix:"o11y-"`
DatabaseImage string `help:"The container image to start for the database" default:"postgres:15.4" env:"FTL_DATABASE_IMAGE" hidden:""`
DatabaseImage string `help:"The container image to start for the database" default:"postgres:15.8" env:"FTL_DATABASE_IMAGE" hidden:""`
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This now matches the version in the Docker compose file.

controller.CommonConfig
}

Expand Down