Skip to content

Commit

Permalink
Merge pull request #2 from bagashiz/feat/setup-database-migrations
Browse files Browse the repository at this point in the history
feat: setup database migrations
  • Loading branch information
bagashiz committed Aug 21, 2023
2 parents f82fb46 + a2e12a6 commit f731c05
Show file tree
Hide file tree
Showing 16 changed files with 193 additions and 7 deletions.
51 changes: 48 additions & 3 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ version: "3"

vars:
DBML_FILE: "./schema.dbml"
DSN: "{{.DB_CONNECTION}}://{{.DB_USERNAME}}:{{.DB_PASSWORD}}@{{.DB_HOST}}:{{.DB_PORT}}/{{.DB_DATABASE}}?sslmode=disable"

dotenv:
- ".env"
Expand All @@ -13,7 +14,9 @@ tasks:
desc: "Gettings started"
cmds:
- task: install
- task: db:start
- task: db:up
- task: db:create
- task: migrate:up
- task: dev

db:docs:
Expand All @@ -23,25 +26,65 @@ tasks:
vars:
- DBML_FILE

db:start:
db:up:
desc: "Start database container"
cmd: podman-compose up -d

db:stop:
db:create:
desc: "Create database schema"
cmd: podman exec -it postgres_gopos sh -c "psql -U {{.DB_USERNAME}} -c 'CREATE DATABASE {{.DB_DATABASE}};'"
requires:
vars:
- DB_USERNAME
- DB_DATABASE

migrate:up:
desc: "Run database migrations"
cmd: migrate -path ./db/migrations -database {{.DSN}} -verbose up {{.CLI_ARGS}}
requires:
vars:
- DSN

migrate:down:
desc: "Rollback database migrations"
cmd: migrate -path ./db/migrations -database {{.DSN}} -verbose down {{.CLI_ARGS}}
requires:
vars:
- DSN

db:drop:
desc: "Drop database schema"
cmd: podman exec -it postgres_gopos sh -c "psql -U {{.DB_USERNAME}} -c 'DROP DATABASE {{.DB_DATABASE}};'"
requires:
vars:
- DB_USERNAME
- DB_DATABASE

db:down:
desc: "Stop database container"
cmd: podman-compose down

sqlc:
desc: "Generate SQL code"
cmd: sqlc generate

dev:
desc: "Start development server"
cmd: air

build:
desc: "Build binary"
cmd: go build -o ./bin/{{.APP_NAME}} ./cmd/http/main.go
requires:
vars:
- APP_NAME

start:
desc: "Start binary"
cmd: ./bin/{{.APP_NAME}}
requires:
vars:
- APP_NAME

# docs:
# desc: "Generate swagger documentation"
Expand All @@ -58,3 +101,5 @@ tasks:
cmds:
- go install github.com/go-task/task/v3/cmd/task@latest
- go install github.com/cosmtrek/air@latest
- go install github.com/sqlc-dev/sqlc/cmd/sqlc@latest
- go install -tags 'postgres' github.com/golang-migrate/migrate/v4/cmd/migrate@latest
3 changes: 3 additions & 0 deletions db/migrations/000001_create_users_table.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
DROP TABLE IF EXISTS "users";

DROP TYPE IF EXISTS "users_role_enum";
13 changes: 13 additions & 0 deletions db/migrations/000001_create_users_table.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
CREATE TYPE "users_role_enum" AS ENUM ('admin', 'cashier');

CREATE TABLE "users" (
"id" BIGSERIAL PRIMARY KEY,
"name" varchar NOT NULL,
"email" varchar NOT NULL,
"password" varchar NOT NULL,
"role" users_role_enum DEFAULT 'cashier',
"created_at" timestamptz NOT NULL DEFAULT (now()),
"updated_at" timestamptz NOT NULL DEFAULT (now())
);

CREATE UNIQUE INDEX "email" ON "users" ("email");
1 change: 1 addition & 0 deletions db/migrations/000002_create_payments_table.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE IF EXISTS "payments";
10 changes: 10 additions & 0 deletions db/migrations/000002_create_payments_table.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
CREATE TABLE "payments" (
"id" BIGSERIAL PRIMARY KEY,
"name" varchar NOT NULL,
"type" varchar NOT NULL,
"logo" varchar,
"created_at" timestamptz NOT NULL DEFAULT (now()),
"updated_at" timestamptz NOT NULL DEFAULT (now())
);

CREATE UNIQUE INDEX "payment_name" ON "payments" ("name");
7 changes: 7 additions & 0 deletions db/migrations/000003_create_orders_table.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
ALTER TABLE
IF EXISTS "orders" DROP CONSTRAINT "fk_users_orders";

ALTER TABLE
IF EXISTS "orders" DROP CONSTRAINT "fk_payments_orders";

DROP TABLE IF EXISTS "orders";
30 changes: 30 additions & 0 deletions db/migrations/000003_create_orders_table.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
CREATE TABLE "orders" (
"id" BIGSERIAL PRIMARY KEY,
"user_id" bigint NOT NULL,
"payment_id" bigint NOT NULL,
"customer_name" varchar NOT NULL,
"total_price" decimal(18, 2) NOT NULL,
"total_paid" decimal(18, 2) NOT NULL,
"total_return" decimal(18, 2) NOT NULL,
"receipt_code" uuid NOT NULL DEFAULT (gen_random_uuid()),
"created_at" timestamptz NOT NULL DEFAULT (now()),
"updated_at" timestamptz NOT NULL DEFAULT (now())
);

CREATE INDEX "orders_customer_name" ON "orders" ("customer_name");

CREATE INDEX "orders_payment_id" ON "orders" ("payment_id");

CREATE INDEX "orders_user_id" ON "orders" ("user_id");

CREATE UNIQUE INDEX "receipt_code" ON "orders" ("receipt_code");

ALTER TABLE
"orders"
ADD
CONSTRAINT "fk_payments_orders" FOREIGN KEY ("payment_id") REFERENCES "payments" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;

ALTER TABLE
"orders"
ADD
CONSTRAINT "fk_users_orders" FOREIGN KEY ("user_id") REFERENCES "users" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;
1 change: 1 addition & 0 deletions db/migrations/000004_create_categories_table.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE IF EXISTS "categories";
8 changes: 8 additions & 0 deletions db/migrations/000004_create_categories_table.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CREATE TABLE "categories" (
"id" BIGSERIAL PRIMARY KEY,
"name" varchar NOT NULL,
"created_at" timestamptz NOT NULL DEFAULT (now()),
"updated_at" timestamptz NOT NULL DEFAULT (now())
);

CREATE UNIQUE INDEX "category_name" ON "categories" ("name");
4 changes: 4 additions & 0 deletions db/migrations/000005_create_products_table.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ALTER TABLE
"products" DROP CONSTRAINT "fk_categories_products";

DROP TABLE IF EXISTS "products";
22 changes: 22 additions & 0 deletions db/migrations/000005_create_products_table.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
CREATE TABLE "products" (
"id" BIGSERIAL PRIMARY KEY,
"category_id" bigint NOT NULL,
"sku" varchar NOT NULL,
"name" varchar NOT NULL,
"stock" bigint NOT NULL,
"price" decimal(18, 2) NOT NULL,
"image" varchar,
"created_at" timestamptz NOT NULL DEFAULT (now()),
"updated_at" timestamptz NOT NULL DEFAULT (now())
);

CREATE INDEX "products_category_id" ON "products" ("category_id");

CREATE INDEX "products_name" ON "products" ("name");

CREATE UNIQUE INDEX "sku" ON "products" ("sku");

ALTER TABLE
"products"
ADD
CONSTRAINT "fk_categories_products" FOREIGN KEY ("category_id") REFERENCES "categories" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;
7 changes: 7 additions & 0 deletions db/migrations/000006_create_order_products_table.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
ALTER TABLE
IF EXISTS "order_products" DROP CONSTRAINT "fk_products_order_products";

ALTER TABLE
IF EXISTS "order_products" DROP CONSTRAINT "fk_orders_order_products";

DROP TABLE IF EXISTS "order_products";
23 changes: 23 additions & 0 deletions db/migrations/000006_create_order_products_table.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
CREATE TABLE "order_products" (
"id" BIGSERIAL PRIMARY KEY,
"order_id" bigint NOT NULL,
"product_id" bigint NOT NULL,
"quantity" bigint NOT NULL,
"total_price" decimal(18, 2) NOT NULL,
"created_at" timestamptz NOT NULL DEFAULT (now()),
"updated_at" timestamptz NOT NULL DEFAULT (now())
);

CREATE INDEX "order_product_order_id" ON "order_products" ("order_id");

CREATE INDEX "order_product_product_id" ON "order_products" ("product_id");

ALTER TABLE
"order_products"
ADD
CONSTRAINT "fk_orders_order_products" FOREIGN KEY ("order_id") REFERENCES "orders" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;

ALTER TABLE
"order_products"
ADD
CONSTRAINT "fk_products_order_products" FOREIGN KEY ("product_id") REFERENCES "products" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;
3 changes: 1 addition & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: '3.8'
version: "3.8"

services:
postgres_gopos:
Expand All @@ -13,7 +13,6 @@ services:
environment:
POSTGRES_USER: ${DB_USERNAME}
POSTGRES_PASSWORD: ${DB_PASSWORD}
POSTGRES_DB: ${DB_DATABASE}

volumes:
data:
4 changes: 2 additions & 2 deletions schema.dbml
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ Indexes {

Table "categories" {
"id" bigserial [pk, increment]
"updated_at" timestamptz [not null, default: `now()`]
"created_at" timestamptz [not null, default: `now()`]
"name" varchar [not null]
"created_at" timestamptz [not null, default: `now()`]
"updated_at" timestamptz [not null, default: `now()`]

Indexes {
name [unique, name: "category_name"]
Expand Down
13 changes: 13 additions & 0 deletions sqlc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
version: "2"
sql:
- engine: "postgresql"
schema: "./db/migrations"
queries: "./db/queries"
gen:
go:
package: "repository"
out: "./internal/adapter/repository"
sql_package: "pgx/v5"
emit_json_tags: true
emit_interface: true
emit_empty_slices: true

0 comments on commit f731c05

Please sign in to comment.