Replace id column with natural PK

This commit is contained in:
Jan Bader 2022-03-04 21:56:12 +00:00
parent 442e87234c
commit fd6b77f154
4 changed files with 34 additions and 4 deletions

View File

@ -17,7 +17,7 @@ INSERT INTO assignments (
) VALUES (
$1, $2, $3
)
RETURNING id, category_id, date, memo, amount
RETURNING category_id, date, memo, amount
`
type CreateAssignmentParams struct {
@ -30,7 +30,6 @@ func (q *Queries) CreateAssignment(ctx context.Context, arg CreateAssignmentPara
row := q.db.QueryRowContext(ctx, createAssignment, arg.Date, arg.Amount, arg.CategoryID)
var i Assignment
err := row.Scan(
&i.ID,
&i.CategoryID,
&i.Date,
&i.Memo,
@ -130,3 +129,22 @@ func (q *Queries) GetAssignmentsByMonthAndCategory(ctx context.Context, budgetID
}
return items, nil
}
const updateAssignment = `-- name: UpdateAssignment :exec
INSERT INTO assignments (category_id, date, amount)
VALUES($1, $2, $3)
ON CONFLICT (category_id, date)
DO
UPDATE SET amount = $3
`
type UpdateAssignmentParams struct {
CategoryID uuid.UUID
Date time.Time
Amount numeric.Numeric
}
func (q *Queries) UpdateAssignment(ctx context.Context, arg UpdateAssignmentParams) error {
_, err := q.db.ExecContext(ctx, updateAssignment, arg.CategoryID, arg.Date, arg.Amount)
return err
}

View File

@ -40,7 +40,6 @@ type Account struct {
}
type Assignment struct {
ID uuid.UUID
CategoryID uuid.UUID
Date time.Time
Memo sql.NullString

View File

@ -23,3 +23,10 @@ FROM assignments
INNER JOIN categories ON categories.id = assignments.category_id
INNER JOIN category_groups ON categories.category_group_id = category_groups.id
WHERE category_groups.budget_id = @budget_id;
-- name: UpdateAssignment :exec
INSERT INTO assignments (category_id, date, amount)
VALUES($1, $2, $3)
ON CONFLICT (category_id, date)
DO
UPDATE SET amount = $3;

View File

@ -0,0 +1,6 @@
-- +goose Up
ALTER TABLE assignments DROP id;
ALTER TABLE assignments ADD PRIMARY KEY (category_id, date);
-- +goose Down
ALTER TABLE assignments ADD COLUMN id uuid DEFAULT uuid_generate_v4() PRIMARY KEY;