Project

General

Profile

1
##### Vars/functions
2

    
3
# Paths
4
bin := ../bin
5

    
6
# Terminal
7
esc := '['
8
reset := $(esc)'0m'
9
emph := $(esc)'7m '
10
endEmph := ' '$(reset)
11

    
12
# User interaction
13

    
14
confirm = $(if $(shell read -p $(emph)"$(1)"$(endEmph)$$'$(if\
15
$(2),\n$(2))\nContinue? (y/n) ' REPLY; test "$$REPLY" = y && echo t),,\
16
$(error Aborting))
17

    
18
# DB
19
psqlVerbose := $(bin)/psql_script_vegbien --echo-all
20
pg_dump := $(bin)/pg_dump_vegbien
21
rmSchema = 'DROP SCHEMA IF EXISTS "$(1)" CASCADE;'
22

    
23
##### General targets
24

    
25
all:
26

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

    
31
_always:
32
.PHONY: _always
33

    
34
##### Backups
35

    
36
# Note: This can't be used for the current (unrotated) public schema because
37
# pg_dump doesn't back up the CREATE SCHEMA statement for it, assuming falsely
38
# that public already exists because it's in template1.
39
%.backup %.sql:
40
	"time" env data=1 $(if $(filter %.sql,$@),plain=1) $(pg_dump) $* >$@
41

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

    
46
%/restore: % _always
47
	$(restoreBien) $< &>$<.log
48

    
49
%.sql: %
50
	$(restore) $< >$@ 2>$<.extract.log
51

    
52
##### Archived imports
53

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

    
56
%.backup/remove: %.backup _always
57
	@$(call confirmRm,$*)
58
	echo $(call rmSchema,$*)|$(bin)/psql_script_vegbien
59

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