Skip to content

Commit

Permalink
fix: avoid insertion key dupls in module_configuration table by ups…
Browse files Browse the repository at this point in the history
…erting on conflict (#2107)

Fixes #2084

This PR addresses the config side, whereas
#2105 fixed it for secrets.

I confirmed the test fails before this change but passes with it:

```
$ go test ./common/configuration/dal/... -run TestModuleConfiguration
debug:migrate: Applying: 20231103205514_init.sql
debug:migrate: Applied: 20231103205514_init.sql in 134.088875ms
debug:migrate: Applying: 20240704103403_create_module_secrets.sql
debug:migrate: Applied: 20240704103403_create_module_secrets.sql in 5.793958ms
--- FAIL: TestModuleConfiguration (0.30s)
    --- FAIL: TestModuleConfiguration/HandlesConflicts (0.00s)
        dal_test.go:108: Did not expect an error but got:
            duplicate key value violates unique constraint "module_configuration_module_name_key": conflict
FAIL
FAIL	github.com/TBD54566975/ftl/common/configuration/dal	0.760s
FAIL
$ go test ./common/configuration/dal/... -run TestModuleConfiguration
ok  	github.com/TBD54566975/ftl/common/configuration/dal	0.775s
```
  • Loading branch information
deniseli authored Jul 17, 2024
1 parent 3bee890 commit b64f7cf
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
7 changes: 7 additions & 0 deletions common/configuration/dal/dal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@ func TestModuleConfiguration(t *testing.T) {
assert.Equal(t, sortedList[i].Name, gotList[i].Name)
}
})

t.Run("HandlesConflicts", func(t *testing.T) {
err := dal.SetModuleConfiguration(ctx, optional.Some("echo"), "my_config", []byte(`""`))
assert.NoError(t, err)
err = dal.SetModuleConfiguration(ctx, optional.Some("echo"), "my_config", []byte(`""`))
assert.NoError(t, err)
})
}

func TestModuleSecrets(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion common/configuration/sql/queries.sql
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ ORDER BY module, name;

-- name: SetModuleConfiguration :exec
INSERT INTO module_configuration (module, name, value)
VALUES ($1, $2, $3);
VALUES ($1, $2, $3)
ON CONFLICT (module, name) DO UPDATE SET value = $3;

-- name: UnsetModuleConfiguration :exec
DELETE FROM module_configuration
Expand Down
1 change: 1 addition & 0 deletions common/configuration/sql/queries.sql.go

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

0 comments on commit b64f7cf

Please sign in to comment.