Project

General

Profile

1
##### Vars/functions
2

    
3
# Make
4
SHELL := /bin/bash
5

    
6
# 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
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
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
##### Archived imports
42

    
43
#### Backups
44

    
45
# Must come before %.sql with no prerequisites to be matched first
46
%.sql: %
47
	$(restore) $< >$@
48

    
49
# 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
	$(if $(filter %.sql,$@),env plain=1) $(backup) $* >$@
54

    
55
%.backup/restore: %.backup _always
56
	$(restoreBien) $<
57

    
58
%.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
%.backup/test: %.backup.sql _always
64
	rm $<
65

    
66
#### Maintenance
67

    
68
confirmRm = $(call confirm,WARNING: This will delete the archived import $(1)!)
69

    
70
%.backup/remove: %.backup _always
71
	@$(call confirmRm,$*)
72
	echo $(call rmSchema,$*)|$(bin)/psql_script_vegbien
73

    
74
%.backup/rm_indexes: %.backup _always
75
	$(pg_dump) $*|$(bin)/mk_rm_indexes|$(psqlVerbose)
76

    
77
##### Full DB backups
78

    
79
vegbien.backup:
80
	$(backup) all >$@
    (1-1/1)