forked from techschool/simplebank
-
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.
Co-authored-by: phamlequang <phamlequang@gmail.com>
- Loading branch information
1 parent
9728acf
commit 6d1d65f
Showing
5 changed files
with
180 additions
and
5 deletions.
There are no files selected for viewing
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 @@ | ||
*.log |
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
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
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,64 @@ | ||
Project simple_bank { | ||
database_type: 'PostgreSQL' | ||
Note: ''' | ||
# Simple 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'] | ||
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 uuid [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 boolean [not null, default: false] | ||
expires_at timestamptz [not null] | ||
created_at timestamptz [not null, default: `now()`] | ||
} |
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,72 @@ | ||
-- SQL dump generated using DBML (dbml-lang.org) | ||
-- Database: PostgreSQL | ||
-- Generated at: 2022-03-12T09:34:57.260Z | ||
|
||
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', | ||
"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" 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, | ||
"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"); | ||
|
||
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"); |