Project

General

Profile

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

    
5

    
6
##### Vars/functions
7

    
8
# Make
9
SHELL := /bin/bash
10

    
11
##### General targets
12

    
13
all = vegbien.sql vegbien.my.sql functions.sql py_functions.sql
14

    
15
all: _always $(all) ;
16

    
17
clean: _always
18
	$(RM) $(all)
19

    
20
%/reinstall: _always %/uninstall %/install ;
21

    
22
##### Installation
23

    
24
schemas := temp functions py_functions
25
install: $(schemas:%=%/install) ;
26

    
27
schemasReversed := py_functions functions temp
28
uninstall: $(schemasReversed:%=%/uninstall) ;
29

    
30
psqlOpts := --set ON_ERROR_STOP=1 --quiet
31
asAdmin := sudo -E -u postgres
32
psqlAsAdmin := $(asAdmin) psql $(psqlOpts)
33
    # -E preserves env vars so PGOPTIONS is passed to psql
34
psqlAsAdminVegbien := $(psqlAsAdmin) vegbien
35
psqlNoSearchPath := env no_search_path=1 ../bin/psql_script_vegbien
36

    
37
# Must come before `%/install: vegbien.sql` to override it
38
%/install: %.sql _always
39
	-<$< $(psqlNoSearchPath)
40
# ignore errors if schema exists
41

    
42
%/uninstall: _always
43
	@$(if $(filter public% r%,$*),$(call confirmRmPublicSchema,$*))
44
	echo $(call rmSchemaCmd,$*)|$(psqlNoSearchPath)
45

    
46
#### public
47

    
48
confirmRmPublicSchema = $(call confirm,WARNING: This will delete the $(1)\
49
schema of your VegBIEN DB!,$(2))
50

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

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

    
64
rename/%: _always
65
	echo 'ALTER SCHEMA public RENAME TO "$*";'|$(psqlNoSearchPath)
66
	$(MAKE) public/install
67

    
68
rotate: _always rename/$(version) ;
69

    
70
#### py_functions
71

    
72
py_functions/install: py_functions.sql _always
73
	-<$< $(psqlAsAdminVegbien)
74
# ignore errors if schema exists
75

    
76
#### Others
77

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

    
81
##### MySQL schema for ERD
82

    
83
repl = ../bin/repl
84

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

    
88
##### DDL generation
89

    
90
pg_dump = env schema= ../bin/pg_dump_vegbien $(1) >$@
91

    
92
vegbien.sql:
93
	$(call pg_dump,public)
94

    
95
py_functions.sql:
96
	env owners=1 $(call pg_dump,py_functions)
97

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