diff --git a/postgres/budgets.sql.go b/postgres/budgets.sql.go index b9f3ab7..e4827e1 100644 --- a/postgres/budgets.sql.go +++ b/postgres/budgets.sql.go @@ -13,30 +13,30 @@ const createBudget = `-- name: CreateBudget :one INSERT INTO budgets (name, last_modification) VALUES ($1, NOW()) -RETURNING name, last_modification, id +RETURNING id, name, last_modification ` func (q *Queries) CreateBudget(ctx context.Context, name string) (Budget, error) { row := q.db.QueryRowContext(ctx, createBudget, name) var i Budget - err := row.Scan(&i.Name, &i.LastModification, &i.ID) + err := row.Scan(&i.ID, &i.Name, &i.LastModification) return i, err } const getBudget = `-- name: GetBudget :one -SELECT name, last_modification, id FROM budgets +SELECT id, name, last_modification FROM budgets WHERE id = $1 ` func (q *Queries) GetBudget(ctx context.Context, id uuid.UUID) (Budget, error) { row := q.db.QueryRowContext(ctx, getBudget, id) var i Budget - err := row.Scan(&i.Name, &i.LastModification, &i.ID) + err := row.Scan(&i.ID, &i.Name, &i.LastModification) return i, err } const getBudgetsForUser = `-- name: GetBudgetsForUser :many -SELECT budgets.name, budgets.last_modification, budgets.id FROM budgets +SELECT budgets.id, budgets.name, budgets.last_modification FROM budgets LEFT JOIN user_budgets ON budgets.id = user_budgets.budget_id WHERE user_budgets.user_id = $1 ` @@ -50,7 +50,7 @@ func (q *Queries) GetBudgetsForUser(ctx context.Context, userID uuid.UUID) ([]Bu var items []Budget for rows.Next() { var i Budget - if err := rows.Scan(&i.Name, &i.LastModification, &i.ID); err != nil { + if err := rows.Scan(&i.ID, &i.Name, &i.LastModification); err != nil { return nil, err } items = append(items, i) diff --git a/postgres/models.go b/postgres/models.go index 4cf15ba..417aa97 100644 --- a/postgres/models.go +++ b/postgres/models.go @@ -16,9 +16,9 @@ type Account struct { } type Budget struct { + ID uuid.UUID Name string LastModification sql.NullTime - ID uuid.UUID } type Payee struct { @@ -29,7 +29,6 @@ type Payee struct { type Transaction struct { ID uuid.UUID - BudgetID uuid.UUID Date time.Time Memo sql.NullString Amount string @@ -38,10 +37,10 @@ type Transaction struct { } type User struct { + ID uuid.UUID Email string Name string Password string - ID uuid.UUID } type UserBudget struct { diff --git a/postgres/queries/transactions.sql b/postgres/queries/transactions.sql index fb6e43e..6534cc1 100644 --- a/postgres/queries/transactions.sql +++ b/postgres/queries/transactions.sql @@ -1,9 +1,14 @@ -- name: CreateTransaction :one INSERT INTO transactions -(budget_id, date, memo, amount) -VALUES ($1, $2, $3, $4) +(date, memo, amount, account_id, payee_id) +VALUES ($1, $2, $3, $4, $5) RETURNING *; -- name: GetTransactionsForBudget :many SELECT transactions.* FROM transactions -WHERE transactions.budget_id = $1; \ No newline at end of file +LEFT JOIN accounts ON accounts.id = transactions.account_id +WHERE accounts.budget_id = $1; + +-- name: GetTransactionsForAccount :many +SELECT transactions.* FROM transactions +WHERE transactions.account_id = $1; \ No newline at end of file diff --git a/postgres/schema/0001_budgets.sql b/postgres/schema/0001_budgets.sql deleted file mode 100644 index 8117997..0000000 --- a/postgres/schema/0001_budgets.sql +++ /dev/null @@ -1,9 +0,0 @@ --- +goose Up -CREATE TABLE budgets ( - id char(26) NOT NULL, - name text NOT NULL, - last_modification timestamp with time zone -); - --- +goose Down -DROP TABLE budgets; \ No newline at end of file diff --git a/postgres/schema/0002_users.sql b/postgres/schema/0002_users.sql deleted file mode 100644 index 31d1050..0000000 --- a/postgres/schema/0002_users.sql +++ /dev/null @@ -1,10 +0,0 @@ --- +goose Up -CREATE TABLE users ( - id char(26) NOT NULL, - email text NOT NULL, - name text NOT NULL, - password text NOT NULL -); - --- +goose Down -DROP TABLE users; \ No newline at end of file diff --git a/postgres/schema/0003_user_budgets.sql b/postgres/schema/0003_user_budgets.sql deleted file mode 100644 index 9d66c8d..0000000 --- a/postgres/schema/0003_user_budgets.sql +++ /dev/null @@ -1,8 +0,0 @@ --- +goose Up -CREATE TABLE user_budgets ( - user_id char(26) NOT NULL, - budget_id char(26) NOT NULL -); - --- +goose Down -DROP TABLE user_budgets; \ No newline at end of file diff --git a/postgres/schema/0004_migrate-to-uuid.sql b/postgres/schema/0004_migrate-to-uuid.sql deleted file mode 100644 index d73bb4c..0000000 --- a/postgres/schema/0004_migrate-to-uuid.sql +++ /dev/null @@ -1,27 +0,0 @@ --- +goose Up -CREATE EXTENSION IF NOT EXISTS "uuid-ossp"; - -ALTER TABLE budgets DROP COLUMN id; -ALTER TABLE budgets ADD COLUMN id uuid DEFAULT uuid_generate_v4() NOT NULL; - -ALTER TABLE users DROP COLUMN id; -ALTER TABLE users ADD COLUMN id uuid DEFAULT uuid_generate_v4() NOT NULL; - -TRUNCATE TABLE user_budgets; -ALTER TABLE user_budgets DROP COLUMN user_id; -ALTER TABLE user_budgets DROP COLUMN budget_id; -ALTER TABLE user_budgets ADD COLUMN user_id uuid NOT NULL; -ALTER TABLE user_budgets ADD COLUMN budget_id uuid NOT NULL; - --- +goose Down -DROP EXTENSION "uuid-ossp"; -ALTER TABLE budgets DROP COLUMN id; -ALTER TABLE budgets ADD COLUMN id char(26) NOT NULL; - -ALTER TABLE users DROP COLUMN id; -ALTER TABLE users ADD COLUMN id char(26) NOT NULL; - -ALTER TABLE user_budgets DROP COLUMN user_id; -ALTER TABLE user_budgets DROP COLUMN budget_id; -ALTER TABLE user_budgets ADD COLUMN user_id char(26) NOT NULL; -ALTER TABLE user_budgets ADD COLUMN budget_id char(26) NOT NULL; \ No newline at end of file diff --git a/postgres/schema/0005_transactions.sql b/postgres/schema/0005_transactions.sql deleted file mode 100644 index 9905522..0000000 --- a/postgres/schema/0005_transactions.sql +++ /dev/null @@ -1,11 +0,0 @@ --- +goose Up -CREATE TABLE transactions ( - id uuid DEFAULT uuid_generate_v4() NOT NULL, - budget_id uuid NOT NULL, - date date NOT NULL, - memo text NULL, - amount decimal(12,2) NOT NULL -); - --- +goose Down -DROP TABLE transactions; \ No newline at end of file diff --git a/postgres/schema/0006_accounts.sql b/postgres/schema/0006_accounts.sql deleted file mode 100644 index af97d51..0000000 --- a/postgres/schema/0006_accounts.sql +++ /dev/null @@ -1,37 +0,0 @@ --- +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/schema/202112021109_initial.sql b/postgres/schema/202112021109_initial.sql new file mode 100644 index 0000000..596d500 --- /dev/null +++ b/postgres/schema/202112021109_initial.sql @@ -0,0 +1,55 @@ +-- +goose Up +CREATE EXTENSION IF NOT EXISTS "uuid-ossp"; + +CREATE TABLE budgets ( + id uuid DEFAULT uuid_generate_v4() PRIMARY KEY, + name text NOT NULL, + last_modification timestamp with time zone +); + +CREATE TABLE users ( + id uuid DEFAULT uuid_generate_v4() PRIMARY KEY, + email text NOT NULL, + name text NOT NULL, + password text NOT NULL +); + +CREATE TABLE user_budgets ( + user_id uuid NOT NULL, + budget_id uuid NOT NULL +); +ALTER TABLE "user_budgets" ADD FOREIGN KEY ("user_id") REFERENCES "users" ("id"); +ALTER TABLE "user_budgets" ADD FOREIGN KEY ("budget_id") REFERENCES "budgets" ("id"); + +CREATE TABLE accounts ( + id uuid DEFAULT uuid_generate_v4() PRIMARY KEY, + budget_id uuid NOT NULL, + name varchar(50) NOT NULL +); +ALTER TABLE "accounts" ADD FOREIGN KEY ("budget_id") REFERENCES "budgets" ("id"); + +CREATE TABLE payees ( + id uuid DEFAULT uuid_generate_v4() PRIMARY KEY, + budget_id uuid NOT NULL, + name varchar(50) NOT NULL +); + +CREATE TABLE transactions ( + id uuid DEFAULT uuid_generate_v4() PRIMARY KEY, + date date NOT NULL, + memo text NULL, + amount decimal(12,2) NOT NULL, + account_id uuid NOT NULL, + payee_id uuid +); +ALTER TABLE "transactions" ADD FOREIGN KEY ("account_id") REFERENCES "accounts" ("id"); +ALTER TABLE "transactions" ADD FOREIGN KEY ("payee_id") REFERENCES "payees" ("id"); + +-- +goose Down +DROP EXTENSION "uuid-ossp"; +DROP TABLE budgets; +DROP TABLE users; +DROP TABLE user_budgets; +DROP TABLE transactions; +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 cd27f07..6963995 100644 --- a/postgres/transactions.sql.go +++ b/postgres/transactions.sql.go @@ -13,29 +13,30 @@ import ( 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, account_id, payee_id +(date, memo, amount, account_id, payee_id) +VALUES ($1, $2, $3, $4, $5) +RETURNING id, date, memo, amount, account_id, payee_id ` type CreateTransactionParams struct { - BudgetID uuid.UUID - Date time.Time - Memo sql.NullString - Amount string + Date time.Time + Memo sql.NullString + Amount string + AccountID uuid.UUID + PayeeID uuid.NullUUID } func (q *Queries) CreateTransaction(ctx context.Context, arg CreateTransactionParams) (Transaction, error) { row := q.db.QueryRowContext(ctx, createTransaction, - arg.BudgetID, arg.Date, arg.Memo, arg.Amount, + arg.AccountID, + arg.PayeeID, ) var i Transaction err := row.Scan( &i.ID, - &i.BudgetID, &i.Date, &i.Memo, &i.Amount, @@ -45,13 +46,49 @@ func (q *Queries) CreateTransaction(ctx context.Context, arg CreateTransactionPa return i, err } -const getTransactionsForBudget = `-- name: GetTransactionsForBudget :many -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 +const getTransactionsForAccount = `-- name: GetTransactionsForAccount :many +SELECT transactions.id, transactions.date, transactions.memo, transactions.amount, transactions.account_id, transactions.payee_id FROM transactions +WHERE transactions.account_id = $1 ` -func (q *Queries) GetTransactionsForBudget(ctx context.Context, budgetID uuid.UUID) ([]Transaction, error) { - rows, err := q.db.QueryContext(ctx, getTransactionsForBudget, budgetID) +func (q *Queries) GetTransactionsForAccount(ctx context.Context, accountID uuid.UUID) ([]Transaction, error) { + rows, err := q.db.QueryContext(ctx, getTransactionsForAccount, accountID) + if err != nil { + return nil, err + } + defer rows.Close() + var items []Transaction + for rows.Next() { + var i Transaction + if err := rows.Scan( + &i.ID, + &i.Date, + &i.Memo, + &i.Amount, + &i.AccountID, + &i.PayeeID, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + +const getTransactionsForBudget = `-- name: GetTransactionsForBudget :many +SELECT transactions.id, transactions.date, transactions.memo, transactions.amount, transactions.account_id, transactions.payee_id FROM transactions +LEFT JOIN accounts ON accounts.id = transactions.account_id +WHERE accounts.budget_id = $1 +` + +func (q *Queries) GetTransactionsForBudget(ctx context.Context, budgetID uuid.UUID) ([]Transaction, error) { + rows, err := q.db.QueryContext(ctx, getTransactionsForBudget, budgetID) if err != nil { return nil, err } @@ -61,7 +98,6 @@ func (q *Queries) GetTransactionsForBudget(ctx context.Context, budgetID uuid.UU var i Transaction if err := rows.Scan( &i.ID, - &i.BudgetID, &i.Date, &i.Memo, &i.Amount, diff --git a/postgres/users.sql.go b/postgres/users.sql.go index ed28220..cdf53b8 100644 --- a/postgres/users.sql.go +++ b/postgres/users.sql.go @@ -13,7 +13,7 @@ const createUser = `-- name: CreateUser :one INSERT INTO users (id, email, name, password) VALUES ($1, $2, $3, $4) -RETURNING email, name, password, id +RETURNING id, email, name, password ` type CreateUserParams struct { @@ -32,16 +32,16 @@ func (q *Queries) CreateUser(ctx context.Context, arg CreateUserParams) (User, e ) var i User err := row.Scan( + &i.ID, &i.Email, &i.Name, &i.Password, - &i.ID, ) return i, err } const getUser = `-- name: GetUser :one -SELECT email, name, password, id FROM users +SELECT id, email, name, password FROM users WHERE id = $1 ` @@ -49,16 +49,16 @@ func (q *Queries) GetUser(ctx context.Context, id uuid.UUID) (User, error) { row := q.db.QueryRowContext(ctx, getUser, id) var i User err := row.Scan( + &i.ID, &i.Email, &i.Name, &i.Password, - &i.ID, ) return i, err } const getUserByUsername = `-- name: GetUserByUsername :one -SELECT email, name, password, id FROM users +SELECT id, email, name, password FROM users WHERE email = $1 ` @@ -66,10 +66,10 @@ func (q *Queries) GetUserByUsername(ctx context.Context, email string) (User, er row := q.db.QueryRowContext(ctx, getUserByUsername, email) var i User err := row.Scan( + &i.ID, &i.Email, &i.Name, &i.Password, - &i.ID, ) return i, err }