Skip to content

Commit

Permalink
Merge pull request #33 from aryyawijaya/feat/dbdocs
Browse files Browse the repository at this point in the history
feat: add db documentation with dbdocs
  • Loading branch information
aryyawijaya authored Feb 7, 2024
2 parents ce30b44 + 7cd6aad commit 9465c5a
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@ tmp

# Go workspace file
go.work

# Log files
*.log
18 changes: 14 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
DB_URL = postgresql://root:secretpassword@localhost:5433/simple_bank?sslmode=disable

pull-postgres:
docker pull postgres:16.0-alpine3.18

Expand Down Expand Up @@ -28,28 +30,28 @@ dropdb:
migrateup-all:
migrate \
-path db/migration \
-database "postgresql://root:secretpassword@localhost:5433/simple_bank?sslmode=disable" \
-database "${DB_URL}" \
-verbose \
up

migrateup-1:
migrate \
-path db/migration \
-database "postgresql://root:secretpassword@localhost:5433/simple_bank?sslmode=disable" \
-database "${DB_URL}" \
-verbose \
up 1

migratedown-all:
migrate \
-path db/migration \
-database "postgresql://root:secretpassword@localhost:5433/simple_bank?sslmode=disable" \
-database "${DB_URL}" \
-verbose \
down

migratedown-1:
migrate \
-path db/migration \
-database "postgresql://root:secretpassword@localhost:5433/simple_bank?sslmode=disable" \
-database "${DB_URL}" \
-verbose \
down 1

Expand Down Expand Up @@ -120,6 +122,12 @@ format:
query-update:
make sqlc mock

db-docs:
dbdocs build doc/db.dbml

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

.PHONY:
pull-postgres \
start-postgres \
Expand All @@ -138,3 +146,5 @@ query-update:
mock \
format \
query-update \
db-docs \
db-schema \
75 changes: 75 additions & 0 deletions doc/db.dbml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
Project simple_bank {
database_type: 'PostgreSQL'
Note: '''
# Simple Bank Database
**markdown content here**
'''
}

Table "accounts" {
"id" bigserial [pk, increment]
"owner" varchar [not null]
"balance" bigint [not null]
"currency" varchar [not null]
"createdAt" timestamptz [not null, default: `now()`]

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

Table "entries" {
"id" bigserial [pk, increment]
"account_id" bigint [not null]
"amount" bigint [not null, note: 'can be negative or positive']
"createdAt" timestamptz [not null, default: `now()`]

Indexes {
account_id
}
}

Table "transfers" {
"id" bigserial [pk, increment]
"from_account_id" bigint [not null]
"to_account_id" bigint [not null]
"amount" bigint [not null, note: 'must be positive']
"createdAt" timestamptz [not null, default: `now()`]

Indexes {
from_account_id
to_account_id
(from_account_id, to_account_id)
}
}

Table "users" {
"username" varchar [pk]
"hashedPassword" varchar [not null]
"fullName" varchar [not null]
"email" varchar [unique, not null]
"passwordChangedAt" timestamptz [not null, default: "0001-01-01 00:00:00Z"]
"createdAt" timestamptz [not null, default: `now()`]
}

Table "sessions" {
"id" uuid [pk]
"username" varchar [not null]
"refresh_token" varchar [not null]
"user_agent" varchar [not null]
"client_ip" varchar [not null]
"is_blocked" boolean [not null, default: false]
"expires_at" timestamptz [not null]
"createdAt" timestamptz [not null, default: `now()`]
}

Ref:"accounts"."id" < "entries"."account_id"

Ref:"accounts"."id" < "transfers"."from_account_id"

Ref:"accounts"."id" < "transfers"."to_account_id"

Ref:"users"."username" < "accounts"."owner"

Ref:"users"."username" < "sessions"."username"
72 changes: 72 additions & 0 deletions doc/schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
-- SQL dump generated using DBML (dbml-lang.org)
-- Database: PostgreSQL
-- Generated at: 2024-02-07T15:17:03.046Z

CREATE TABLE "accounts" (
"id" BIGSERIAL PRIMARY KEY,
"owner" varchar NOT NULL,
"balance" bigint NOT NULL,
"currency" varchar NOT NULL,
"createdAt" timestamptz NOT NULL DEFAULT (now())
);

CREATE TABLE "entries" (
"id" BIGSERIAL PRIMARY KEY,
"account_id" bigint NOT NULL,
"amount" bigint NOT NULL,
"createdAt" 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,
"createdAt" timestamptz NOT NULL DEFAULT (now())
);

CREATE TABLE "users" (
"username" varchar PRIMARY KEY,
"hashedPassword" varchar NOT NULL,
"fullName" varchar NOT NULL,
"email" varchar UNIQUE NOT NULL,
"passwordChangedAt" timestamptz NOT NULL DEFAULT '0001-01-01 00:00:00Z',
"createdAt" timestamptz NOT NULL DEFAULT (now())
);

CREATE TABLE "sessions" (
"id" uuid PRIMARY KEY,
"username" varchar NOT NULL,
"refresh_token" varchar NOT NULL,
"user_agent" varchar NOT NULL,
"client_ip" varchar NOT NULL,
"is_blocked" boolean NOT NULL DEFAULT false,
"expires_at" timestamptz NOT NULL,
"createdAt" 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");

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

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

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

0 comments on commit 9465c5a

Please sign in to comment.