-
Notifications
You must be signed in to change notification settings - Fork 36
/
db.makefile
39 lines (30 loc) · 1.24 KB
/
db.makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
ifdef USE_NIX
SHELL := nix-shell
.SHELLFLAGS := --packages postgresql --run
endif
export PGDATA ?= $(CURDIR)/.postgres-work
export PGDATABASE ?= snowdrift_development
export PGHOST := $(PGDATA)
pg_isready := pg_isready -q
pg_ctl := $(shell pg_config --bindir)/pg_ctl
# Start postgres process, put all the files in the same place, and wait for
# startup to complete before continuting.
# pg_ctl: -w: wait, -o: postgres options (need quotes), -l: log file path
# postgres: -F: no fsync, -h '': no IP/hostname connections. -k: socket dir
pg_start := $(pg_ctl) \
start -w -o "-F -h '' -k $(PGHOST)" -l $(PGDATA)/logfile
# Example usage: $(db_exists) foo
#
# Result is returned as exit status.
# - https://stackoverflow.com/a/16783253/994643
db_exists := psql -lqt | cut -f1 -d \| | grep -qw
.PHONY: database service cluster isrunning stop clean
database: service ; $(db_exists) $(PGDATABASE) || createdb $(PGDATABASE)
service: cluster ; $(pg_isready) || $(pg_start)
# This is one of the files created when a cluster is initialized.
cluster_sentinel := $(PGDATA)/PG_VERSION
cluster: $(cluster_sentinel)
$(cluster_sentinel): ; $(pg_ctl) initdb -D $(PGDATA)
isrunning: ; $(pg_isready)
stop: ; -$(pg_isready) && $(pg_ctl) stop
clean: stop ; rm -rf $(PGDATA)