Add on_budget column to accounts

This commit is contained in:
Jan Bader 2021-12-07 20:52:53 +00:00
parent 915379f5cb
commit e3f3dc6748
3 changed files with 27 additions and 6 deletions

View File

@ -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)

View File

@ -13,6 +13,7 @@ type Account struct {
ID uuid.UUID
BudgetID uuid.UUID
Name string
OnBudget bool
}
type Assignment struct {

View File

@ -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;