From fd6b77f1542aec52346248dbfe3d1e27cb4255de Mon Sep 17 00:00:00 2001 From: Jan Bader Date: Fri, 4 Mar 2022 21:56:12 +0000 Subject: [PATCH] Replace id column with natural PK --- postgres/assignments.sql.go | 22 +++++++++++++++++-- postgres/models.go | 1 - postgres/queries/assignments.sql | 9 +++++++- .../schema/0017_natural-key-assignments.sql | 6 +++++ 4 files changed, 34 insertions(+), 4 deletions(-) create mode 100644 postgres/schema/0017_natural-key-assignments.sql diff --git a/postgres/assignments.sql.go b/postgres/assignments.sql.go index 8602e7a..52bd45d 100644 --- a/postgres/assignments.sql.go +++ b/postgres/assignments.sql.go @@ -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 +} diff --git a/postgres/models.go b/postgres/models.go index 4e33e88..7ff33e8 100644 --- a/postgres/models.go +++ b/postgres/models.go @@ -40,7 +40,6 @@ type Account struct { } type Assignment struct { - ID uuid.UUID CategoryID uuid.UUID Date time.Time Memo sql.NullString diff --git a/postgres/queries/assignments.sql b/postgres/queries/assignments.sql index 56975ef..cb0687d 100644 --- a/postgres/queries/assignments.sql +++ b/postgres/queries/assignments.sql @@ -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; \ No newline at end of file +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; \ No newline at end of file diff --git a/postgres/schema/0017_natural-key-assignments.sql b/postgres/schema/0017_natural-key-assignments.sql new file mode 100644 index 0000000..27c1096 --- /dev/null +++ b/postgres/schema/0017_natural-key-assignments.sql @@ -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; \ No newline at end of file