mirror of
https://github.com/acaloiaro/frm
synced 2026-07-21 18:29:12 +00:00
fix: calling SaveShortCode with the same params twice fails
This commit is contained in:
parent
2898364885
commit
aff261b4e5
3 changed files with 21 additions and 3 deletions
|
|
@ -102,4 +102,6 @@ WHERE workspace_id = @workspace_id
|
|||
-- name: SaveShortCode :one
|
||||
|
||||
INSERT INTO short_codes (workspace_id, form_id, subject_id, short_code)
|
||||
VALUES (@workspace_id, @form_id, @subject_id, @short_code) RETURNING *;
|
||||
VALUES (@workspace_id, @form_id, @subject_id, @short_code) ON CONFLICT (subject_id, form_id) DO
|
||||
UPDATE
|
||||
SET updated_at = timezone('utc', now()) RETURNING *;
|
||||
|
|
|
|||
12
frm_test.go
12
frm_test.go
|
|
@ -48,4 +48,16 @@ func TestCreateShortCode(t *testing.T) {
|
|||
if len(sc.ShortCode) != internal.DefaultShortcodeLen {
|
||||
t.Fatal("shortcode length is incorrect")
|
||||
}
|
||||
|
||||
sc2, err := f.CreateShortCode(ctx, frm.CreateShortCodeArgs{
|
||||
FormID: draft.ID,
|
||||
SubjectID: "foobar_idx",
|
||||
})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if sc2.ShortCode != sc.ShortCode {
|
||||
t.Error("successive SaveShortCode calls should create same short code")
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -406,7 +406,9 @@ func (q *Queries) SaveDraft(ctx context.Context, arg SaveDraftParams) (Form, err
|
|||
const saveShortCode = `-- name: SaveShortCode :one
|
||||
|
||||
INSERT INTO short_codes (workspace_id, form_id, subject_id, short_code)
|
||||
VALUES ($1, $2, $3, $4) RETURNING id, workspace_id, form_id, short_code, subject_id, created_at, updated_at
|
||||
VALUES ($1, $2, $3, $4) ON CONFLICT (subject_id, form_id) DO
|
||||
UPDATE
|
||||
SET updated_at = timezone('utc', now()) RETURNING id, workspace_id, form_id, short_code, subject_id, created_at, updated_at
|
||||
`
|
||||
|
||||
type SaveShortCodeParams struct {
|
||||
|
|
@ -419,7 +421,9 @@ type SaveShortCodeParams struct {
|
|||
// SaveShortCode
|
||||
//
|
||||
// INSERT INTO short_codes (workspace_id, form_id, subject_id, short_code)
|
||||
// VALUES ($1, $2, $3, $4) RETURNING id, workspace_id, form_id, short_code, subject_id, created_at, updated_at
|
||||
// VALUES ($1, $2, $3, $4) ON CONFLICT (subject_id, form_id) DO
|
||||
// UPDATE
|
||||
// SET updated_at = timezone('utc', now()) RETURNING id, workspace_id, form_id, short_code, subject_id, created_at, updated_at
|
||||
func (q *Queries) SaveShortCode(ctx context.Context, arg SaveShortCodeParams) (ShortCode, error) {
|
||||
row := q.db.QueryRow(ctx, saveShortCode,
|
||||
arg.WorkspaceID,
|
||||
|
|
|
|||
Loading…
Reference in a new issue