Permalink
Browse files

add useful database commands

  • Loading branch information...
kamilsk committed Oct 10, 2018
1 parent 4a417f1 commit 34450224f8b3df1866193e1580b0ea97050d3ed8
Showing with 31 additions and 5 deletions.
  1. +2 βˆ’2 .gitattributes
  2. +1 βˆ’1 .gitignore
  3. 0 env/docker/db/{clean.sql β†’ truncate.sql}
  4. +28 βˆ’2 env/make/docker-compose.mk
@@ -23,7 +23,7 @@
*.pb.gw.go linguist-generated=true
*_easyjson.go -diff -merge
*_easyjson.go linguist-generated=true
bindata.go -diff -merge
bindata.go linguist-generated=true
mock_*_test.go -diff -merge
mock_*_test.go linguist-generated=true
statik.go -diff -merge
statik.go linguist-generated=true
@@ -2,7 +2,7 @@
*.out

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

# environment
/.env
File renamed without changes.
@@ -6,7 +6,7 @@ COMPOSE = $(_base) -f env/docker/compose/docker-compose.dev.yml
__env__:
@(cp -nrf env/docker/compose/.env.example .env)
#|
#| ---
#| --- Docker Compose' generic commands
#|
.PHONY: ci
ci: #| Switch docker compose to CI/CD configuration.
@@ -48,7 +48,7 @@ destroy: __env__ #| Stops containers and removes them with networks, volumes,
status: __env__ #| List containers and their status.
@($(COMPOSE) ps)
#|
#| ---
#| --- Service-specific commands
#|
SERVICES = db \
legacy \
@@ -103,3 +103,29 @@ endef

render_service_tpl = $(eval $(call service_tpl,$(service)))
$(foreach service,$(SERVICES),$(render_service_tpl))
#| --- 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)

0 comments on commit 3445022

Please sign in to comment.