Skip to content
This repository has been archived by the owner on May 11, 2022. It is now read-only.

Commit

Permalink
add db targets to docker compose makefile
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilsk committed Oct 13, 2018
1 parent 9d3353e commit 5078eb3
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 44 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
*.out

# database
/env/backup.db
/env/docker/db/*.dump

# environment
*.env
4 changes: 4 additions & 0 deletions env/cmd.mk
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ cmd-help-run:
cmd-version:
go run $(LDFLAGS) $(BUILD_FILES) version

.PHONY: cmd-migrate
cmd-migrate:
go run $(LDFLAGS) $(BUILD_FILES) migrate $(FLAGS) up

.PHONY: cmd-migrate-up
cmd-migrate-up:
go run $(LDFLAGS) $(BUILD_FILES) migrate $(FLAGS) up 1
Expand Down
92 changes: 50 additions & 42 deletions env/docker-compose.mk
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
COMPOSE ?= docker-compose -f env/docker-compose.base.yml -f env/docker-compose.dev.yml -p forma


.PHONY: env
env:
.PHONY: __env__
__env__:
cp -n env/.env{.example,} || true # for containers
cp -n env/.env .env || true # for docker compose, https://docs.docker.com/compose/env-file/

Expand All @@ -16,113 +16,121 @@ config:
$(COMPOSE) config

.PHONY: up
up: env
up: __env__
$(COMPOSE) up -d
$(COMPOSE) rm -f

.PHONY: fresh-up
fresh-up: env
fresh-up: __env__
$(COMPOSE) up --build --force-recreate -d
$(COMPOSE) rm -f

.PHONY: down
down: env
down: __env__
$(COMPOSE) down

.PHONY: clean-down
clean-down: env
clean-down: __env__
$(COMPOSE) down --volumes --rmi local

.PHONY: clear
clear: env
clear: __env__
$(COMPOSE) rm -f

.PHONY: status
status: env
status: __env__
$(COMPOSE) ps


.PHONY: up-db
up-db: env
up-db: __env__
$(COMPOSE) up -d db

.PHONY: start-db
start-db: env
start-db: __env__
$(COMPOSE) start db

.PHONY: stop-db
stop-db: env
stop-db: __env__
$(COMPOSE) stop db

.PHONY: log-db
log-db: env
log-db: __env__
$(COMPOSE) logs -f db

.PHONY: psql
psql: env
$(COMPOSE) exec db /bin/sh -c 'su - postgres -c psql'

.PHONY: backup-db
backup-db: env
$(COMPOSE) exec db /bin/sh -c 'su - postgres -c "pg_dump --format=custom --file=/tmp/backup.db $${POSTGRES_DB}"'
docker cp $$(make status | tail +3 | awk '{print $$1}' | grep _db_ | head -1):/tmp/backup.db ./env/
$(COMPOSE) exec db rm /tmp/backup.db

.PHONY: restore-db
restore-db: env
docker cp ./env/clean.sql $$(make status | tail +3 | awk '{print $$1}' | grep _db_ | head -1):/tmp/
docker cp ./env/backup.db $$(make status | tail +3 | awk '{print $$1}' | grep _db_ | head -1):/tmp/
$(COMPOSE) exec db /bin/sh -c 'su - postgres -c "psql $${POSTGRES_DB} < /tmp/clean.sql"'
$(COMPOSE) exec db /bin/sh -c 'su - postgres -c "pg_restore -Fc -d $${POSTGRES_DB} /tmp/backup.db"'
$(COMPOSE) exec db rm /tmp/backup.db /tmp/clean.sql


.PHONY: up-migration
up-migration: env
up-migration: __env__
$(COMPOSE) up --build -d migration

.PHONY: start-migration
start-migration: env
start-migration: __env__
$(COMPOSE) start migration

.PHONY: stop-migration
stop-migration: env
stop-migration: __env__
$(COMPOSE) stop migration

.PHONY: log-migration
log-migration: env
log-migration: __env__
$(COMPOSE) logs -f migration


.PHONY: up-service
up-service: env
up-service: __env__
$(COMPOSE) up --build -d service

.PHONY: start-service
start-service: env
start-service: __env__
$(COMPOSE) start service

.PHONY: stop-service
stop-service: env
stop-service: __env__
$(COMPOSE) stop service

.PHONY: log-service
log-service: env
log-service: __env__
$(COMPOSE) logs -f service


.PHONY: up-server
up-server: env
up-server: __env__
$(COMPOSE) up -d server

.PHONY: start-server
start-server: env
start-server: __env__
$(COMPOSE) start server

.PHONY: stop-server
stop-server: env
stop-server: __env__
$(COMPOSE) stop server

.PHONY: log-server
log-server: env
log-server: __env__
$(COMPOSE) logs -f server
#| --- Database-specific commands
#|
.PHONY: psql
psql: __env__ #| Connect to the database.
@($(COMPOSE) exec db /bin/sh -c 'su - postgres -c psql')
#|
.PHONY: backup
backup: __env__ #| Backup the database.
@($(COMPOSE) exec db /bin/sh -c 'su - postgres -c "pg_dump --format=custom --file=/tmp/db.dump $${POSTGRES_DB}"')
@(docker cp $$(make status | tail +3 | awk '{print $$1}' | grep _db_ | head -1):/tmp/db.dump ./env/docker/db/)
@($(COMPOSE) exec db rm /tmp/db.dump)
@(ls -l ./env/docker/db/db.dump)
#|
.PHONY: restore
restore: __env__ #| Restore the database.
@(docker cp ./env/docker/db/reset.sql $$(make status | tail +3 | awk '{print $$1}' | grep _db_ | head -1):/tmp/)
@(docker cp ./env/docker/db/db.dump $$(make status | tail +3 | awk '{print $$1}' | grep _db_ | head -1):/tmp/)
@($(COMPOSE) exec db /bin/sh -c 'su - postgres -c "psql $${POSTGRES_DB} < /tmp/reset.sql"')
@($(COMPOSE) exec db /bin/sh -c 'su - postgres -c "pg_restore -Fc -d $${POSTGRES_DB} /tmp/db.dump"')
@($(COMPOSE) exec db rm /tmp/reset.sql /tmp/db.dump)
#|
.PHONY: truncate
truncate: __env__ #| Truncate the database tables.
@(docker cp ./env/docker/db/truncate.sql $$(make status | tail +3 | awk '{print $$1}' | grep _db_ | head -1):/tmp/)
@($(COMPOSE) exec db /bin/sh -c 'su - postgres -c "psql $${POSTGRES_DB} < /tmp/truncate.sql"')
@($(COMPOSE) exec db rm /tmp/truncate.sql)
1 change: 0 additions & 1 deletion env/docker/.gitkeep

This file was deleted.

File renamed without changes.
5 changes: 5 additions & 0 deletions env/docker/db/truncate.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
TRUNCATE TABLE "link" RESTART IDENTITY RESTRICT;
TRUNCATE TABLE "schema" RESTART IDENTITY RESTRICT;
TRUNCATE TABLE "template" RESTART IDENTITY RESTRICT;
TRUNCATE TABLE "input" RESTART IDENTITY RESTRICT;
TRUNCATE TABLE "event" RESTART IDENTITY RESTRICT;

0 comments on commit 5078eb3

Please sign in to comment.