Replace id column with natural PK
This commit is contained in:
parent
442e87234c
commit
fd6b77f154
@ -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
|
||||
}
|
||||
|
@ -40,7 +40,6 @@ type Account struct {
|
||||
}
|
||||
|
||||
type Assignment struct {
|
||||
ID uuid.UUID
|
||||
CategoryID uuid.UUID
|
||||
Date time.Time
|
||||
Memo sql.NullString
|
||||
|
@ -22,4 +22,11 @@ SELECT assignments.date, categories.name as category, category_groups.name as gr
|
||||
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;
|
||||
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;
|
6
postgres/schema/0017_natural-key-assignments.sql
Normal file
6
postgres/schema/0017_natural-key-assignments.sql
Normal 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;
|
Loading…
x
Reference in New Issue
Block a user