Skip to content

Commit

Permalink
Merge pull request #3 from hafizxd/ft/dbdocs
Browse files Browse the repository at this point in the history
add dbdocs
  • Loading branch information
hafizxd committed Feb 19, 2024
2 parents 26361e9 + c0078e2 commit d248577
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 48 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.log
1 change: 0 additions & 1 deletion .idea/sqldialects.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 13 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
DB_URL=postgresql://root:secret@localhost:5432/simple_bank?sslmode=disable

postgres:
docker run --name postgres12 --network bank-network -p 5432:5432 -e POSTGRES_USER=root -e POSTGRES_PASSWORD=secret -d postgres:alpine3.19

Expand All @@ -11,16 +13,16 @@ 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
migrate -path db/migration -database "$(DB_URL)" -verbose up

migrateup1:
migrate -path db/migration -database "postgresql://root:secret@localhost:5432/simple_bank?sslmode=disable" -verbose up 1
migrate -path db/migration -database "$(DB_URL)" -verbose up 1

migratedown:
migrate -path db/migration -database "postgresql://root:secret@localhost:5432/simple_bank?sslmode=disable" -verbose down
migrate -path db/migration -database "$(DB_URL)" -verbose down

migratedown1:
migrate -path db/migration -database "postgresql://root:secret@localhost:5432/simple_bank?sslmode=disable" -verbose down 1
migrate -path db/migration -database "$(DB_URL)" -verbose down 1

sqlc:
sqlc generate
Expand All @@ -34,5 +36,11 @@ server:
mock:
mockgen -package mockdb -destination db/mock/store.go github.com/hafizxd/simple_bank/db/sqlc Store

db_docs:
dbdocs build doc/db.dbml

db_schema:
dbml2sql --postgres -o doc/schema.sql doc/db.dbml

.PHONY:
postgres postgresstart createdb dropdb migrateup migratedown sqlc test server mock
postgres postgresstart createdb dropdb migrateup migratedown sqlc test server mock db_docs db_schema
42 changes: 0 additions & 42 deletions db.sql

This file was deleted.

68 changes: 68 additions & 0 deletions doc/db.dbml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
Project microbank {
database_type: 'PostgreSQL'
Note: '''
# Micro Bank Database
'''
}

Table users as U {
username varchar [pk]
hashed_password varchar [not null]
full_name varchar [not null]
email varchar [unique, not null]
password_changed_at timestamptz [not null, default: '0001-01-01 00:00:00Z']
created_at timestamptz [not null, default: 'now()']
}

Table accounts as A {
id bigserial [pk]
owner varchar [ref: > U.username, not null]
balance bigint [not null]
currency varchar [not null]
created_at timestamptz [not null, default: 'now()']

Indexes {
owner
(owner, currency) [unique]
}
}

Table entries {
id bigserial [pk]
account_id bigint [ref: > A.id, not null]
amount bigint [not null, note: 'can be negative or positive']
created_at timestamptz [not null, default: 'now()']

Indexes {
account_id
}
}

Table transfers {
id bigserial [pk]
from_account_id bigint [ref: > A.id, not null]
to_account_id bigint [ref: > A.id, not null]
amount bigint [not null, note: 'must be positive']
created_at timestamptz [not null, default: 'now()']

Indexes {
from_account_id
to_account_id
(from_account_id, to_account_id)
}
}

Table sessions {
id varchar [pk]
username varchar [ref: > U.username, not null]
refresh_token varchar [not null]
user_agent varchar [not null]
client_ip varchar [not null]
is_blocked bool [not null, default: 'false']
expires_at timestamptz [not null]
created_at timestamptz [not null, default: 'now()']

Indexes {
username
}
}
74 changes: 74 additions & 0 deletions doc/schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
-- SQL dump generated using DBML (dbml-lang.org)
-- Database: PostgreSQL
-- Generated at: 2024-02-19T06:21:04.061Z

CREATE TABLE "users" (
"username" varchar PRIMARY KEY,
"hashed_password" varchar NOT NULL,
"full_name" varchar NOT NULL,
"email" varchar UNIQUE NOT NULL,
"password_changed_at" timestamptz NOT NULL DEFAULT '0001-01-01 00:00:00Z',
"created_at" timestamptz NOT NULL DEFAULT 'now()'
);

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 TABLE "sessions" (
"id" varchar PRIMARY KEY,
"username" varchar NOT NULL,
"refresh_token" varchar NOT NULL,
"user_agent" varchar NOT NULL,
"client_ip" varchar NOT NULL,
"is_blocked" bool NOT NULL DEFAULT 'false',
"expires_at" timestamptz NOT NULL,
"created_at" timestamptz NOT NULL DEFAULT 'now()'
);

CREATE INDEX ON "accounts" ("owner");

CREATE UNIQUE INDEX ON "accounts" ("owner", "currency");

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");

CREATE INDEX ON "sessions" ("username");

COMMENT ON COLUMN "entries"."amount" IS 'can be negative or positive';

COMMENT ON COLUMN "transfers"."amount" IS 'must be positive';

ALTER TABLE "accounts" ADD FOREIGN KEY ("owner") REFERENCES "users" ("username");

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");

ALTER TABLE "sessions" ADD FOREIGN KEY ("username") REFERENCES "users" ("username");

0 comments on commit d248577

Please sign in to comment.