-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 7c819b0
Showing
26 changed files
with
1,367 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
postgres: | ||
docker run --name postgres12 -p 5432:5432 -e POSTGRES_USER=root -e POSTGRES_PASSWORD=secret -d postgres:alpine3.19 | ||
|
||
createdb: | ||
docker exec -it postgres12 createdb --username=root --owner=root simple_bank | ||
|
||
dropdb: | ||
docker exec -it postgres12 dropdb --username=root --owner=root simple_bank | ||
|
||
migrateup: | ||
migrate -path db/migration -database "postgresql://root:secret@localhost:5432/simple_bank?sslmode=disable" -verbose up | ||
|
||
migratedown: | ||
migrate -path db/migration -database "postgresql://root:secret@localhost:5432/simple_bank?sslmode=disable" -verbose down | ||
|
||
sqlc: | ||
sqlc generate | ||
|
||
test: | ||
go test -v -cover ./... | ||
|
||
.PHONY: | ||
postgres createdb dropdb migrateup migratedown |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
CREATE TABLE "accounts" ( | ||
"id" bigserial PRIMARY KEY, | ||
"owner" varchar NOT NULL, | ||
"balance" bigint NOT NULL, | ||
"currency" varchar NOT NULL, | ||
"created_at" timestamptz NOT NULL DEFAULT (now()) | ||
); | ||
|
||
CREATE TABLE "entries" ( | ||
"id" bigserial PRIMARY KEY, | ||
"account_id" bigint, | ||
"amount" bigint NOT NULL, | ||
"created_at" timestamptz NOT NULL DEFAULT (now()) | ||
); | ||
|
||
CREATE TABLE "transfers" ( | ||
"id" bigserial PRIMARY KEY, | ||
"from_account_id" bigint, | ||
"to_account_id" bigint, | ||
"amount" bigint NOT NULL, | ||
"created_at" timestamptz NOT NULL DEFAULT (now()) | ||
); | ||
|
||
CREATE INDEX ON "accounts" ("owner"); | ||
|
||
CREATE INDEX ON "entries" ("account_id"); | ||
|
||
CREATE INDEX ON "transfers" ("from_account_id"); | ||
|
||
CREATE INDEX ON "transfers" ("to_account_id"); | ||
|
||
CREATE INDEX ON "transfers" ("from_account_id", "to_account_id"); | ||
|
||
COMMENT ON COLUMN "entries"."amount" IS 'can be negative or positive'; | ||
|
||
COMMENT ON COLUMN "transfers"."amount" IS 'must be positive'; | ||
|
||
ALTER TABLE "entries" ADD FOREIGN KEY ("account_id") REFERENCES "accounts" ("id"); | ||
|
||
ALTER TABLE "transfers" ADD FOREIGN KEY ("from_account_id") REFERENCES "accounts" ("id"); | ||
|
||
ALTER TABLE "transfers" ADD FOREIGN KEY ("to_account_id") REFERENCES "accounts" ("id"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
DROP TABLE IF EXISTS entries; | ||
DROP TABLE IF EXISTS transfers; | ||
DROP TABLE IF EXISTS accounts; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
CREATE TABLE "accounts" ( | ||
"id" bigserial PRIMARY KEY, | ||
"owner" varchar NOT NULL, | ||
"balance" bigint NOT NULL, | ||
"currency" varchar NOT NULL, | ||
"created_at" timestamptz NOT NULL DEFAULT (now()) | ||
); | ||
|
||
CREATE TABLE "entries" ( | ||
"id" bigserial PRIMARY KEY, | ||
"account_id" bigint NOT NULL, | ||
"amount" bigint NOT NULL, | ||
"created_at" timestamptz NOT NULL DEFAULT (now()) | ||
); | ||
|
||
CREATE TABLE "transfers" ( | ||
"id" bigserial PRIMARY KEY, | ||
"from_account_id" bigint NOT NULL, | ||
"to_account_id" bigint NOT NULL, | ||
"amount" bigint NOT NULL, | ||
"created_at" timestamptz NOT NULL DEFAULT (now()) | ||
); | ||
|
||
CREATE INDEX ON "accounts" ("owner"); | ||
|
||
CREATE INDEX ON "entries" ("account_id"); | ||
|
||
CREATE INDEX ON "transfers" ("from_account_id"); | ||
|
||
CREATE INDEX ON "transfers" ("to_account_id"); | ||
|
||
CREATE INDEX ON "transfers" ("from_account_id", "to_account_id"); | ||
|
||
COMMENT ON COLUMN "entries"."amount" IS 'can be negative or positive'; | ||
|
||
COMMENT ON COLUMN "transfers"."amount" IS 'must be positive'; | ||
|
||
ALTER TABLE "entries" ADD FOREIGN KEY ("account_id") REFERENCES "accounts" ("id"); | ||
|
||
ALTER TABLE "transfers" ADD FOREIGN KEY ("from_account_id") REFERENCES "accounts" ("id"); | ||
|
||
ALTER TABLE "transfers" ADD FOREIGN KEY ("to_account_id") REFERENCES "accounts" ("id"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
-- name: CreateAccount :one | ||
INSERT INTO accounts ( | ||
owner, | ||
balance, | ||
currency | ||
) VALUES ( | ||
$1, $2, $3 | ||
) RETURNING *; | ||
|
||
-- name: GetAccount :one | ||
SELECT * FROM accounts | ||
WHERE id = $1 LIMIT 1; | ||
|
||
-- name: GetAccountForUpdate :one | ||
SELECT * FROM accounts | ||
WHERE id = $1 LIMIT 1 | ||
FOR NO KEY UPDATE; | ||
|
||
-- name: ListAccount :many | ||
SELECT * FROM accounts | ||
ORDER BY id | ||
LIMIT $1 | ||
OFFSET $2; | ||
|
||
-- name: UpdateAccount :one | ||
UPDATE accounts | ||
set balance = $2 | ||
WHERE id = $1 | ||
RETURNING *; | ||
|
||
-- name: AddAccountBalance :one | ||
UPDATE accounts | ||
set balance = balance + sqlc.arg(amount) | ||
WHERE id = sqlc.arg(id) | ||
RETURNING *; | ||
|
||
-- name: DeleteAccount :exec | ||
DELETE FROM accounts | ||
WHERE id = $1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
-- name: CreateEntry :one | ||
INSERT INTO entries ( | ||
account_id, | ||
amount | ||
) VALUES ( | ||
$1, $2 | ||
) RETURNING *; | ||
|
||
-- name: GetEntry :one | ||
SELECT * FROM entries | ||
WHERE id = $1 LIMIT 1; | ||
|
||
-- name: ListEntry :many | ||
SELECT * FROM entries | ||
ORDER BY id | ||
LIMIT $1 | ||
OFFSET $2; | ||
|
||
-- name: UpdateEntry :one | ||
UPDATE entries | ||
set amount = $2 | ||
WHERE id = $1 | ||
RETURNING *; | ||
|
||
-- name: DeleteEntry :exec | ||
DELETE FROM entries | ||
WHERE id = $1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
-- name: CreateTransfer :one | ||
INSERT INTO transfers ( | ||
from_account_id, | ||
to_account_id, | ||
amount | ||
) VALUES ( | ||
$1, $2, $3 | ||
) RETURNING *; | ||
|
||
-- name: GetTransfer :one | ||
SELECT * FROM transfers | ||
WHERE id = $1 LIMIT 1; | ||
|
||
-- name: ListTransfer :many | ||
SELECT * FROM transfers | ||
ORDER BY id | ||
LIMIT $1 | ||
OFFSET $2; | ||
|
||
-- name: UpdateTransfer :one | ||
UPDATE transfers | ||
set amount = $2 | ||
WHERE id = $1 | ||
RETURNING *; | ||
|
||
-- name: DeleteTransfer :exec | ||
DELETE FROM transfers | ||
WHERE id = $1; |
Oops, something went wrong.