db.makefile 1.2 KB
Newer Older
Bryan Richter's avatar
Bryan Richter committed
1 2
ifdef USE_NIX
    SHELL := nix-shell
3 4 5
    .SHELLFLAGS := --packages postgresql --run
endif

Bryan Richter's avatar
Bryan Richter committed
6 7 8
export PGDATA ?= $(CURDIR)/.postgres-work
export PGDATABASE ?= snowdrift_development
export PGHOST := $(PGDATA)
9

10 11
pg_isready := pg_isready -q
pg_ctl := $(shell pg_config --bindir)/pg_ctl
12

13 14 15 16
# 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
17
pg_start := $(pg_ctl) \
18
    start -w -o "-F -h '' -k $(PGHOST)" -l $(PGDATA)/log
19

20
# Example usage:  $(db_exists) foo
Bryan Richter's avatar
Bryan Richter committed
21 22
#
# Result is returned as exit status.
23
#   - https://stackoverflow.com/a/16783253/994643
Bryan Richter's avatar
Bryan Richter committed
24
db_exists := psql -lqt | cut -f1 -d \| | grep -qw
25

26
.PHONY: database service cluster stop clean
27

Bryan Richter's avatar
Bryan Richter committed
28
database: service ; $(db_exists) $(PGDATABASE) || createdb $(PGDATABASE)
29

Bryan Richter's avatar
Bryan Richter committed
30 31
service: cluster ; $(pg_isready) || $(pg_start)

32 33
# This is one of the files created when a cluster is initialized.
cluster_sentinel := $(PGDATA)/PG_VERSION
Bryan Richter's avatar
Bryan Richter committed
34
cluster: $(cluster_sentinel)
35
$(cluster_sentinel): ; $(pg_ctl) initdb -D $(PGDATA)
Bryan Richter's avatar
Bryan Richter committed
36

37
stop: ; -$(pg_isready) && $(pg_ctl) stop
38
clean: stop ; rm -rf $(PGDATA)