diff --git a/postgres/models.go b/postgres/models.go index 182c982..4cf15ba 100644 --- a/postgres/models.go +++ b/postgres/models.go @@ -9,18 +9,32 @@ import ( "github.com/google/uuid" ) +type Account struct { + ID uuid.UUID + BudgetID uuid.UUID + Name string +} + type Budget struct { Name string LastModification sql.NullTime ID uuid.UUID } -type Transaction struct { +type Payee struct { ID uuid.UUID BudgetID uuid.UUID - Date time.Time - Memo sql.NullString - Amount string + Name string +} + +type Transaction struct { + ID uuid.UUID + BudgetID uuid.UUID + Date time.Time + Memo sql.NullString + Amount string + AccountID uuid.UUID + PayeeID uuid.NullUUID } type User struct { diff --git a/postgres/schema/0003_users.sql b/postgres/schema/0002_users.sql similarity index 100% rename from postgres/schema/0003_users.sql rename to postgres/schema/0002_users.sql diff --git a/postgres/schema/0002_user_budgets.sql b/postgres/schema/0003_user_budgets.sql similarity index 100% rename from postgres/schema/0002_user_budgets.sql rename to postgres/schema/0003_user_budgets.sql diff --git a/postgres/schema/0006_accounts.sql b/postgres/schema/0006_accounts.sql new file mode 100644 index 0000000..af97d51 --- /dev/null +++ b/postgres/schema/0006_accounts.sql @@ -0,0 +1,37 @@ +-- +goose Up +CREATE TABLE accounts ( + id uuid DEFAULT uuid_generate_v4() NOT NULL, + budget_id uuid NOT NULL, + name varchar(50) NOT NULL +); + +CREATE TABLE payees ( + id uuid DEFAULT uuid_generate_v4() NOT NULL, + budget_id uuid NOT NULL, + name varchar(50) NOT NULL +); + +TRUNCATE TABLE transactions; +ALTER TABLE transactions ADD COLUMN account_id uuid NOT NULL; +ALTER TABLE transactions ADD COLUMN payee_id uuid; + + + +ALTER TABLE "user_budgets" ADD FOREIGN KEY ("user_id") REFERENCES "users" ("id"); + +ALTER TABLE "user_budgets" ADD FOREIGN KEY ("budget_id") REFERENCES "budgets" ("id"); + +ALTER TABLE "transactions" ADD FOREIGN KEY ("account_id") REFERENCES "accounts" ("id"); + +ALTER TABLE "accounts" ADD FOREIGN KEY ("budget_id") REFERENCES "budgets" ("id"); + +ALTER TABLE "categories" ADD FOREIGN KEY ("budget_id") REFERENCES "budgets" ("id"); + +ALTER TABLE "assignments" ADD FOREIGN KEY ("category_id") REFERENCES "categories" ("id"); + +-- +goose Down +ALTER TABLE transactions DROP COLUMN account_id; +ALTER TABLE transactions DROP COLUMN payee_id; + +DROP TABLE accounts; +DROP TABLE payees; \ No newline at end of file diff --git a/postgres/transactions.sql.go b/postgres/transactions.sql.go index 6c27502..cd27f07 100644 --- a/postgres/transactions.sql.go +++ b/postgres/transactions.sql.go @@ -15,7 +15,7 @@ const createTransaction = `-- name: CreateTransaction :one INSERT INTO transactions (budget_id, date, memo, amount) VALUES ($1, $2, $3, $4) -RETURNING id, budget_id, date, memo, amount +RETURNING id, budget_id, date, memo, amount, account_id, payee_id ` type CreateTransactionParams struct { @@ -39,12 +39,14 @@ func (q *Queries) CreateTransaction(ctx context.Context, arg CreateTransactionPa &i.Date, &i.Memo, &i.Amount, + &i.AccountID, + &i.PayeeID, ) return i, err } const getTransactionsForBudget = `-- name: GetTransactionsForBudget :many -SELECT transactions.id, transactions.budget_id, transactions.date, transactions.memo, transactions.amount FROM transactions +SELECT transactions.id, transactions.budget_id, transactions.date, transactions.memo, transactions.amount, transactions.account_id, transactions.payee_id FROM transactions WHERE transactions.budget_id = $1 ` @@ -63,6 +65,8 @@ func (q *Queries) GetTransactionsForBudget(ctx context.Context, budgetID uuid.UU &i.Date, &i.Memo, &i.Amount, + &i.AccountID, + &i.PayeeID, ); err != nil { return nil, err }