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
rmSchema = 'DROP SCHEMA IF EXISTS "$(1)" CASCADE;'
25

    
26
##### General targets
27

    
28
all:
29

    
30
.SUFFIXES: # turn off built-in suffix rules
31
.SECONDARY: # don't automatically delete intermediate files
32
.DELETE_ON_ERROR: # delete target if recipe fails
33

    
34
_always:
35
.PHONY: _always
36

    
37
##### Backups
38

    
39
restore := "time" $(bin)/postgres_vegbien pg_restore --exit-on-error
40
restoreBien := $(restore) --dbname=vegbien
41
# pg_restore doesn't support PGDATABASE env var
42

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

    
47
# 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
	"time" env data=1 $(if $(filter %.sql,$@),plain=1) $(pg_dump) $* >$@
52

    
53
%.backup/restore: %.backup _always
54
	$(restoreBien) $<
55

    
56
%.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
##### Archived imports
62

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

    
65
%.backup/remove: %.backup _always
66
	@$(call confirmRm,$*)
67
	echo $(call rmSchema,$*)|$(bin)/psql_script_vegbien
68

    
69
%.backup/rm_indexes: %.backup _always
70
	$(pg_dump) $*|$(bin)/mk_rm_indexes|$(psqlVerbose)
    (1-1/1)