Project

General

Profile

1
selfDir_AkFFYJ := $(dir $(lastword $(MAKEFILE_LIST)))
2
root := $(selfDir_AkFFYJ)..
3
include $(root)/lib/common.Makefile
4

    
5

    
6
##### General targets
7

    
8
all = vegbien.sql vegbien.my.sql functions.sql py_functions.sql
9

    
10
all: _always $(all) ;
11

    
12
clean: _always
13
	$(RM) $(all)
14

    
15
%/reinstall: _always %/uninstall %/install ;
16

    
17
##### Installation
18

    
19
schemas := temp functions py_functions
20
install: $(schemas:%=%/install) ;
21

    
22
schemasReversed := py_functions functions temp
23
uninstall: $(schemasReversed:%=%/uninstall) ;
24

    
25
psqlOpts := --set ON_ERROR_STOP=1 --quiet
26
asAdmin := sudo -E -u postgres
27
psqlAsAdmin := $(asAdmin) psql $(psqlOpts)
28
    # -E preserves env vars so PGOPTIONS is passed to psql
29
psqlAsAdminVegbien := $(psqlAsAdmin) vegbien
30
psqlNoSearchPath := env no_search_path=1 ../bin/psql_script_vegbien
31

    
32
# Must come before `%/install: vegbien.sql` to override it
33
%/install: %.sql _always
34
	-<$< $(psqlNoSearchPath)
35
# ignore errors if schema exists
36

    
37
%/uninstall: _always
38
	@$(if $(filter public% r%,$*),$(call confirmRmPublicSchema,$*))
39
	echo $(call rmSchemaCmd,$*)|$(psqlNoSearchPath)
40

    
41
#### public
42

    
43
confirmRmPublicSchema = $(call confirm,WARNING: This will delete the $(1)\
44
schema of your VegBIEN DB!,$(2))
45

    
46
# Installs a version of the public schema
47
%/install: vegbien.sql _always
48
	echo $(call mkSchemaCmd,$(@D))\
49
"COMMENT ON SCHEMA $(@D) IS 'Version: $(@D) ($(date))';"|$(psqlNoSearchPath)
50
	<$< $(sed) 's/( )public([,; ])/\1$(@D)\2/g'|$(psqlNoSearchPath)
51

    
52
# Replaces the current public schema with the given version
53
%/publish: _always
54
	@$(call confirmRmPublicSchema,public,To save it: make schemas/rotate)
55
	echo $(call rmSchemaCmd,public)\
56
'ALTER SCHEMA $(@D) RENAME TO public;'|$(psqlNoSearchPath) --single-transaction
57
	@echo $(emph)'In your shell, run:'$(endEmph)'unset version'
58

    
59
rename/%: _always
60
	echo 'ALTER SCHEMA public RENAME TO "$*";'|$(psqlNoSearchPath)
61
	$(MAKE) public/install
62

    
63
rotate: _always rename/$(version) ;
64

    
65
#### py_functions
66

    
67
py_functions/install: py_functions.sql _always
68
	-<$< $(psqlAsAdminVegbien)
69
# ignore errors if schema exists
70

    
71
#### Others
72

    
73
# Needed on Ubuntu 12.04 (also other Linuxes?) because %/reinstall is ignored.
74
temp/reinstall: _always temp/uninstall temp/install ;
75

    
76
##### MySQL schema for ERD
77

    
78
repl = ../bin/repl
79

    
80
%.my.sql: %.sql ../lib/PostgreSQL-MySQL.csv filter_ERD.csv
81
	$(repl) <$(wordlist 1,2,$+)|$(repl) $(word 3,$+) >$@
82

    
83
##### DDL generation
84

    
85
pg_dump = env schema= ../bin/pg_dump_vegbien $(1) >$@
86

    
87
vegbien.sql:
88
	$(call pg_dump,public)
89

    
90
py_functions.sql:
91
	env owners=1 $(call pg_dump,py_functions)
92

    
93
# Must come after %.my.sql so that gets matched first
94
%.sql:
95
	$(call pg_dump,$*)
(2-2/25)