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 3437 aaronmk
##### Backups
42 3393 aaronmk
43 3395 aaronmk
# Must come before %.sql with no prerequisites to be matched first
44
%.sql: %
45 3399 aaronmk
	$(restore) $< >$@
46 3395 aaronmk
47 3393 aaronmk
# Note: This can't be used for the current (unrotated) public schema because
48
# pg_dump doesn't back up the CREATE SCHEMA statement for it, assuming falsely
49
# that public already exists because it's in template1.
50
%.backup %.sql:
51 3404 aaronmk
	$(if $(filter %.sql,$@),env plain=1) $(backup) $* >$@
52 3393 aaronmk
53 3402 aaronmk
%.backup/restore: %.backup _always
54 3399 aaronmk
	$(restoreBien) $<
55 3393 aaronmk
56 3402 aaronmk
%.sql/restore: %.sql _always
57
	$(bin)/psql_script_vegbien <$<
58
# pg_restore only supports "non-plain-text formats"
59
# (http://www.postgresql.org/docs/9.1/static/app-pgrestore.html)
60
61 3409 aaronmk
%.backup/test: %.backup.sql _always
62
	rm $<
63
64 3437 aaronmk
#### Archived imports
65 3393 aaronmk
66
confirmRm = $(call confirm,WARNING: This will delete the archived import $(1)!)
67
68
%.backup/remove: %.backup _always
69
	@$(call confirmRm,$*)
70
	echo $(call rmSchema,$*)|$(bin)/psql_script_vegbien
71
72
%.backup/rm_indexes: %.backup _always
73
	$(pg_dump) $*|$(bin)/mk_rm_indexes|$(psqlVerbose)
74 3407 aaronmk
75 3437 aaronmk
#### Full DB
76 3407 aaronmk
77
vegbien.backup:
78
	$(backup) all >$@