Project

General

Profile

1 3393 aaronmk
##### Vars/functions
2
3 3403 aaronmk
# Make
4
SHELL := /bin/bash
5
6 3393 aaronmk
# Paths
7
bin := ../bin
8
9
# Terminal
10
esc := '['
11
reset := $(esc)'0m'
12
emph := $(esc)'7m '
13
endEmph := ' '$(reset)
14
15
# User interaction
16
17
confirm = $(if $(shell read -p $(emph)"$(1)"$(endEmph)$$'$(if\
18
$(2),\n$(2))\nContinue? (y/n) ' REPLY; test "$$REPLY" = y && echo t),,\
19
$(error Aborting))
20
21
# DB
22
psqlVerbose := $(bin)/psql_script_vegbien --echo-all
23
pg_dump := $(bin)/pg_dump_vegbien
24 3404 aaronmk
backup := "time" env data=1 $(pg_dump)
25
restore := "time" $(bin)/postgres_vegbien pg_restore --exit-on-error
26
restoreBien := $(restore) --dbname=vegbien
27
# pg_restore doesn't support PGDATABASE env var
28 3393 aaronmk
rmSchema = 'DROP SCHEMA IF EXISTS "$(1)" CASCADE;'
29
30
##### General targets
31
32
all:
33
34
.SUFFIXES: # turn off built-in suffix rules
35
.SECONDARY: # don't automatically delete intermediate files
36
.DELETE_ON_ERROR: # delete target if recipe fails
37
38
_always:
39
.PHONY: _always
40
41 3404 aaronmk
##### Archived imports
42 3393 aaronmk
43 3404 aaronmk
#### Backups
44 3398 aaronmk
45 3395 aaronmk
# Must come before %.sql with no prerequisites to be matched first
46
%.sql: %
47 3399 aaronmk
	$(restore) $< >$@
48 3395 aaronmk
49 3393 aaronmk
# Note: This can't be used for the current (unrotated) public schema because
50
# pg_dump doesn't back up the CREATE SCHEMA statement for it, assuming falsely
51
# that public already exists because it's in template1.
52
%.backup %.sql:
53 3404 aaronmk
	$(if $(filter %.sql,$@),env plain=1) $(backup) $* >$@
54 3393 aaronmk
55 3402 aaronmk
%.backup/restore: %.backup _always
56 3399 aaronmk
	$(restoreBien) $<
57 3393 aaronmk
58 3402 aaronmk
%.sql/restore: %.sql _always
59
	$(bin)/psql_script_vegbien <$<
60
# pg_restore only supports "non-plain-text formats"
61
# (http://www.postgresql.org/docs/9.1/static/app-pgrestore.html)
62
63 3404 aaronmk
#### Maintenance
64 3393 aaronmk
65
confirmRm = $(call confirm,WARNING: This will delete the archived import $(1)!)
66
67
%.backup/remove: %.backup _always
68
	@$(call confirmRm,$*)
69
	echo $(call rmSchema,$*)|$(bin)/psql_script_vegbien
70
71
%.backup/rm_indexes: %.backup _always
72
	$(pg_dump) $*|$(bin)/mk_rm_indexes|$(psqlVerbose)