From e3f3dc6748e0c5f7ad0fee9bf0c7ba1b3db71868 Mon Sep 17 00:00:00 2001 From: Jan Bader Date: Tue, 7 Dec 2021 20:52:53 +0000 Subject: [PATCH] Add on_budget column to accounts --- postgres/accounts.sql.go | 27 ++++++++++++++----- postgres/models.go | 1 + .../202112072148_off-budget-accounts.sql | 5 ++++ 3 files changed, 27 insertions(+), 6 deletions(-) create mode 100644 postgres/schema/202112072148_off-budget-accounts.sql diff --git a/postgres/accounts.sql.go b/postgres/accounts.sql.go index 0eb66be..67c9100 100644 --- a/postgres/accounts.sql.go +++ b/postgres/accounts.sql.go @@ -13,7 +13,7 @@ const createAccount = `-- name: CreateAccount :one INSERT INTO accounts (name, budget_id) VALUES ($1, $2) -RETURNING id, budget_id, name +RETURNING id, budget_id, name, on_budget ` type CreateAccountParams struct { @@ -24,24 +24,34 @@ type CreateAccountParams struct { func (q *Queries) CreateAccount(ctx context.Context, arg CreateAccountParams) (Account, error) { row := q.db.QueryRowContext(ctx, createAccount, arg.Name, arg.BudgetID) var i Account - err := row.Scan(&i.ID, &i.BudgetID, &i.Name) + err := row.Scan( + &i.ID, + &i.BudgetID, + &i.Name, + &i.OnBudget, + ) return i, err } const getAccount = `-- name: GetAccount :one -SELECT accounts.id, accounts.budget_id, accounts.name FROM accounts +SELECT accounts.id, accounts.budget_id, accounts.name, accounts.on_budget FROM accounts WHERE accounts.id = $1 ` func (q *Queries) GetAccount(ctx context.Context, id uuid.UUID) (Account, error) { row := q.db.QueryRowContext(ctx, getAccount, id) var i Account - err := row.Scan(&i.ID, &i.BudgetID, &i.Name) + err := row.Scan( + &i.ID, + &i.BudgetID, + &i.Name, + &i.OnBudget, + ) return i, err } const getAccounts = `-- name: GetAccounts :many -SELECT accounts.id, accounts.budget_id, accounts.name FROM accounts +SELECT accounts.id, accounts.budget_id, accounts.name, accounts.on_budget FROM accounts WHERE accounts.budget_id = $1 ORDER BY accounts.name ` @@ -55,7 +65,12 @@ func (q *Queries) GetAccounts(ctx context.Context, budgetID uuid.UUID) ([]Accoun var items []Account for rows.Next() { var i Account - if err := rows.Scan(&i.ID, &i.BudgetID, &i.Name); err != nil { + if err := rows.Scan( + &i.ID, + &i.BudgetID, + &i.Name, + &i.OnBudget, + ); err != nil { return nil, err } items = append(items, i) diff --git a/postgres/models.go b/postgres/models.go index 2405128..411c9ff 100644 --- a/postgres/models.go +++ b/postgres/models.go @@ -13,6 +13,7 @@ type Account struct { ID uuid.UUID BudgetID uuid.UUID Name string + OnBudget bool } type Assignment struct { diff --git a/postgres/schema/202112072148_off-budget-accounts.sql b/postgres/schema/202112072148_off-budget-accounts.sql new file mode 100644 index 0000000..148d709 --- /dev/null +++ b/postgres/schema/202112072148_off-budget-accounts.sql @@ -0,0 +1,5 @@ +-- +goose Up +ALTER TABLE accounts ADD COLUMN on_budget boolean DEFAULT 'true' NOT NULL; + +-- +goose Down +ALTER TABLE accounts DROP COLUMN on_budget; \ No newline at end of file