Permalink
Browse files

add db targets to docker compose makefile

  • Loading branch information...
kamilsk committed Oct 13, 2018
1 parent 9d3353e commit 5078eb3a7b988261654c442fb5a6c9514ad22bfb
Showing with 60 additions and 44 deletions.
  1. +1 −1 .gitignore
  2. +4 −0 env/cmd.mk
  3. +50 −42 env/docker-compose.mk
  4. +0 −1 env/docker/.gitkeep
  5. 0 env/{clean.sql → docker/db/reset.sql}
  6. +5 −0 env/docker/db/truncate.sql
@@ -2,7 +2,7 @@
*.out

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

# environment
*.env
@@ -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
@@ -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/

@@ -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)

This file was deleted.

Oops, something went wrong.
File renamed without changes.
@@ -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.