Merge schemas into one file
This commit is contained in:
parent
37d19733df
commit
4d1b883974
@ -13,30 +13,30 @@ const createBudget = `-- name: CreateBudget :one
|
|||||||
INSERT INTO budgets
|
INSERT INTO budgets
|
||||||
(name, last_modification)
|
(name, last_modification)
|
||||||
VALUES ($1, NOW())
|
VALUES ($1, NOW())
|
||||||
RETURNING name, last_modification, id
|
RETURNING id, name, last_modification
|
||||||
`
|
`
|
||||||
|
|
||||||
func (q *Queries) CreateBudget(ctx context.Context, name string) (Budget, error) {
|
func (q *Queries) CreateBudget(ctx context.Context, name string) (Budget, error) {
|
||||||
row := q.db.QueryRowContext(ctx, createBudget, name)
|
row := q.db.QueryRowContext(ctx, createBudget, name)
|
||||||
var i Budget
|
var i Budget
|
||||||
err := row.Scan(&i.Name, &i.LastModification, &i.ID)
|
err := row.Scan(&i.ID, &i.Name, &i.LastModification)
|
||||||
return i, err
|
return i, err
|
||||||
}
|
}
|
||||||
|
|
||||||
const getBudget = `-- name: GetBudget :one
|
const getBudget = `-- name: GetBudget :one
|
||||||
SELECT name, last_modification, id FROM budgets
|
SELECT id, name, last_modification FROM budgets
|
||||||
WHERE id = $1
|
WHERE id = $1
|
||||||
`
|
`
|
||||||
|
|
||||||
func (q *Queries) GetBudget(ctx context.Context, id uuid.UUID) (Budget, error) {
|
func (q *Queries) GetBudget(ctx context.Context, id uuid.UUID) (Budget, error) {
|
||||||
row := q.db.QueryRowContext(ctx, getBudget, id)
|
row := q.db.QueryRowContext(ctx, getBudget, id)
|
||||||
var i Budget
|
var i Budget
|
||||||
err := row.Scan(&i.Name, &i.LastModification, &i.ID)
|
err := row.Scan(&i.ID, &i.Name, &i.LastModification)
|
||||||
return i, err
|
return i, err
|
||||||
}
|
}
|
||||||
|
|
||||||
const getBudgetsForUser = `-- name: GetBudgetsForUser :many
|
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
|
LEFT JOIN user_budgets ON budgets.id = user_budgets.budget_id
|
||||||
WHERE user_budgets.user_id = $1
|
WHERE user_budgets.user_id = $1
|
||||||
`
|
`
|
||||||
@ -50,7 +50,7 @@ func (q *Queries) GetBudgetsForUser(ctx context.Context, userID uuid.UUID) ([]Bu
|
|||||||
var items []Budget
|
var items []Budget
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var i Budget
|
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
|
return nil, err
|
||||||
}
|
}
|
||||||
items = append(items, i)
|
items = append(items, i)
|
||||||
|
@ -16,9 +16,9 @@ type Account struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Budget struct {
|
type Budget struct {
|
||||||
|
ID uuid.UUID
|
||||||
Name string
|
Name string
|
||||||
LastModification sql.NullTime
|
LastModification sql.NullTime
|
||||||
ID uuid.UUID
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type Payee struct {
|
type Payee struct {
|
||||||
@ -29,7 +29,6 @@ type Payee struct {
|
|||||||
|
|
||||||
type Transaction struct {
|
type Transaction struct {
|
||||||
ID uuid.UUID
|
ID uuid.UUID
|
||||||
BudgetID uuid.UUID
|
|
||||||
Date time.Time
|
Date time.Time
|
||||||
Memo sql.NullString
|
Memo sql.NullString
|
||||||
Amount string
|
Amount string
|
||||||
@ -38,10 +37,10 @@ type Transaction struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type User struct {
|
type User struct {
|
||||||
|
ID uuid.UUID
|
||||||
Email string
|
Email string
|
||||||
Name string
|
Name string
|
||||||
Password string
|
Password string
|
||||||
ID uuid.UUID
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type UserBudget struct {
|
type UserBudget struct {
|
||||||
|
@ -1,9 +1,14 @@
|
|||||||
-- name: CreateTransaction :one
|
-- name: CreateTransaction :one
|
||||||
INSERT INTO transactions
|
INSERT INTO transactions
|
||||||
(budget_id, date, memo, amount)
|
(date, memo, amount, account_id, payee_id)
|
||||||
VALUES ($1, $2, $3, $4)
|
VALUES ($1, $2, $3, $4, $5)
|
||||||
RETURNING *;
|
RETURNING *;
|
||||||
|
|
||||||
-- name: GetTransactionsForBudget :many
|
-- name: GetTransactionsForBudget :many
|
||||||
SELECT transactions.* FROM transactions
|
SELECT transactions.* FROM transactions
|
||||||
WHERE transactions.budget_id = $1;
|
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;
|
@ -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;
|
|
@ -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;
|
|
@ -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;
|
|
@ -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;
|
|
@ -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;
|
|
@ -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;
|
|
55
postgres/schema/202112021109_initial.sql
Normal file
55
postgres/schema/202112021109_initial.sql
Normal file
@ -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;
|
@ -13,29 +13,30 @@ import (
|
|||||||
|
|
||||||
const createTransaction = `-- name: CreateTransaction :one
|
const createTransaction = `-- name: CreateTransaction :one
|
||||||
INSERT INTO transactions
|
INSERT INTO transactions
|
||||||
(budget_id, date, memo, amount)
|
(date, memo, amount, account_id, payee_id)
|
||||||
VALUES ($1, $2, $3, $4)
|
VALUES ($1, $2, $3, $4, $5)
|
||||||
RETURNING id, budget_id, date, memo, amount, account_id, payee_id
|
RETURNING id, date, memo, amount, account_id, payee_id
|
||||||
`
|
`
|
||||||
|
|
||||||
type CreateTransactionParams struct {
|
type CreateTransactionParams struct {
|
||||||
BudgetID uuid.UUID
|
Date time.Time
|
||||||
Date time.Time
|
Memo sql.NullString
|
||||||
Memo sql.NullString
|
Amount string
|
||||||
Amount string
|
AccountID uuid.UUID
|
||||||
|
PayeeID uuid.NullUUID
|
||||||
}
|
}
|
||||||
|
|
||||||
func (q *Queries) CreateTransaction(ctx context.Context, arg CreateTransactionParams) (Transaction, error) {
|
func (q *Queries) CreateTransaction(ctx context.Context, arg CreateTransactionParams) (Transaction, error) {
|
||||||
row := q.db.QueryRowContext(ctx, createTransaction,
|
row := q.db.QueryRowContext(ctx, createTransaction,
|
||||||
arg.BudgetID,
|
|
||||||
arg.Date,
|
arg.Date,
|
||||||
arg.Memo,
|
arg.Memo,
|
||||||
arg.Amount,
|
arg.Amount,
|
||||||
|
arg.AccountID,
|
||||||
|
arg.PayeeID,
|
||||||
)
|
)
|
||||||
var i Transaction
|
var i Transaction
|
||||||
err := row.Scan(
|
err := row.Scan(
|
||||||
&i.ID,
|
&i.ID,
|
||||||
&i.BudgetID,
|
|
||||||
&i.Date,
|
&i.Date,
|
||||||
&i.Memo,
|
&i.Memo,
|
||||||
&i.Amount,
|
&i.Amount,
|
||||||
@ -45,13 +46,49 @@ func (q *Queries) CreateTransaction(ctx context.Context, arg CreateTransactionPa
|
|||||||
return i, err
|
return i, err
|
||||||
}
|
}
|
||||||
|
|
||||||
const getTransactionsForBudget = `-- name: GetTransactionsForBudget :many
|
const getTransactionsForAccount = `-- name: GetTransactionsForAccount :many
|
||||||
SELECT transactions.id, transactions.budget_id, transactions.date, transactions.memo, transactions.amount, transactions.account_id, transactions.payee_id FROM transactions
|
SELECT transactions.id, transactions.date, transactions.memo, transactions.amount, transactions.account_id, transactions.payee_id FROM transactions
|
||||||
WHERE transactions.budget_id = $1
|
WHERE transactions.account_id = $1
|
||||||
`
|
`
|
||||||
|
|
||||||
func (q *Queries) GetTransactionsForBudget(ctx context.Context, budgetID uuid.UUID) ([]Transaction, error) {
|
func (q *Queries) GetTransactionsForAccount(ctx context.Context, accountID uuid.UUID) ([]Transaction, error) {
|
||||||
rows, err := q.db.QueryContext(ctx, getTransactionsForBudget, budgetID)
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -61,7 +98,6 @@ func (q *Queries) GetTransactionsForBudget(ctx context.Context, budgetID uuid.UU
|
|||||||
var i Transaction
|
var i Transaction
|
||||||
if err := rows.Scan(
|
if err := rows.Scan(
|
||||||
&i.ID,
|
&i.ID,
|
||||||
&i.BudgetID,
|
|
||||||
&i.Date,
|
&i.Date,
|
||||||
&i.Memo,
|
&i.Memo,
|
||||||
&i.Amount,
|
&i.Amount,
|
||||||
|
@ -13,7 +13,7 @@ const createUser = `-- name: CreateUser :one
|
|||||||
INSERT INTO users
|
INSERT INTO users
|
||||||
(id, email, name, password)
|
(id, email, name, password)
|
||||||
VALUES ($1, $2, $3, $4)
|
VALUES ($1, $2, $3, $4)
|
||||||
RETURNING email, name, password, id
|
RETURNING id, email, name, password
|
||||||
`
|
`
|
||||||
|
|
||||||
type CreateUserParams struct {
|
type CreateUserParams struct {
|
||||||
@ -32,16 +32,16 @@ func (q *Queries) CreateUser(ctx context.Context, arg CreateUserParams) (User, e
|
|||||||
)
|
)
|
||||||
var i User
|
var i User
|
||||||
err := row.Scan(
|
err := row.Scan(
|
||||||
|
&i.ID,
|
||||||
&i.Email,
|
&i.Email,
|
||||||
&i.Name,
|
&i.Name,
|
||||||
&i.Password,
|
&i.Password,
|
||||||
&i.ID,
|
|
||||||
)
|
)
|
||||||
return i, err
|
return i, err
|
||||||
}
|
}
|
||||||
|
|
||||||
const getUser = `-- name: GetUser :one
|
const getUser = `-- name: GetUser :one
|
||||||
SELECT email, name, password, id FROM users
|
SELECT id, email, name, password FROM users
|
||||||
WHERE id = $1
|
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)
|
row := q.db.QueryRowContext(ctx, getUser, id)
|
||||||
var i User
|
var i User
|
||||||
err := row.Scan(
|
err := row.Scan(
|
||||||
|
&i.ID,
|
||||||
&i.Email,
|
&i.Email,
|
||||||
&i.Name,
|
&i.Name,
|
||||||
&i.Password,
|
&i.Password,
|
||||||
&i.ID,
|
|
||||||
)
|
)
|
||||||
return i, err
|
return i, err
|
||||||
}
|
}
|
||||||
|
|
||||||
const getUserByUsername = `-- name: GetUserByUsername :one
|
const getUserByUsername = `-- name: GetUserByUsername :one
|
||||||
SELECT email, name, password, id FROM users
|
SELECT id, email, name, password FROM users
|
||||||
WHERE email = $1
|
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)
|
row := q.db.QueryRowContext(ctx, getUserByUsername, email)
|
||||||
var i User
|
var i User
|
||||||
err := row.Scan(
|
err := row.Scan(
|
||||||
|
&i.ID,
|
||||||
&i.Email,
|
&i.Email,
|
||||||
&i.Name,
|
&i.Name,
|
||||||
&i.Password,
|
&i.Password,
|
||||||
&i.ID,
|
|
||||||
)
|
)
|
||||||
return i, err
|
return i, err
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user