Skip to content

Commit

Permalink
fix: avoid insertion key dupls in module_secrets table by upserting…
Browse files Browse the repository at this point in the history
… on conflict (#2105)

Addresses part of #2084

Postgresql way to handle upserts:
https://www.postgresqltutorial.com/postgresql-tutorial/postgresql-upsert/

This PR fixes the issue for secrets since that's causing issues for the
client team, but not for config, which has the same issue. I confirmed
the test fails before this change but passes with it.
  • Loading branch information
deniseli authored Jul 17, 2024
1 parent 15f04ef commit 3bee890
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
13 changes: 13 additions & 0 deletions common/configuration/dal/dal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,16 @@ func TestModuleConfiguration(t *testing.T) {
}
})
}

func TestModuleSecrets(t *testing.T) {
ctx := log.ContextWithNewDefaultLogger(context.Background())
conn := sqltest.OpenForTesting(ctx, t)
dal, err := New(ctx, conn)
assert.NoError(t, err)
assert.NotZero(t, dal)

err = dal.SetModuleSecretURL(ctx, optional.Some("echo"), "my_secret", "asm://echo.my_secret")
assert.NoError(t, err)
err = dal.SetModuleSecretURL(ctx, optional.Some("echo"), "my_secret", "asm://echo.my_secret")
assert.NoError(t, err)
}
5 changes: 3 additions & 2 deletions common/configuration/sql/queries.sql
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ ORDER BY module, name;

-- name: SetModuleSecretURL :exec
INSERT INTO module_secrets (module, name, url)
VALUES ($1, $2, $3);
VALUES ($1, $2, $3)
ON CONFLICT (module, name) DO UPDATE SET url = $3;

-- name: UnsetModuleSecret :exec
DELETE FROM module_secrets
WHERE module = @module AND name = @name;
WHERE module = @module AND name = @name;
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 3bee890

Please sign in to comment.