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 public
20
install: $(schemas:%=%/install) ;
21

    
22
schemasReversed := public 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
#### public
33

    
34
confirmRmPublicSchema = $(call confirm,WARNING: This will delete the $(1)\
35
schema of your VegBIEN DB!,$(2))
36

    
37
public/install: vegbien.sql _always
38
	-echo $(call mkSchemaCmd,public)|$(psqlNoSearchPath)
39
	<$< $(psqlNoSearchPath)
40
# ignore errors if schema exists
41
# public schema will be owned by bien
42
# don't include public in the search_path
43

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

    
50
public/uninstall public%/uninstall: _always
51
	@$(call confirmRmPublicSchema,$(@D))
52
	echo $(call rmSchemaCmd,$(@D))|$(psqlNoSearchPath)
53

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

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

    
64
rotate: _always rename/public.$(version) ;
65

    
66
#### py_functions
67

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

    
72
#### Others
73

    
74
%/install: %.sql _always
75
	-<$< $(psqlNoSearchPath)
76
# ignore errors if schema exists
77

    
78
%/uninstall: _always
79
	echo $(call rmSchemaCmd,$*)|$(psqlNoSearchPath)
80

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

    
84
##### MySQL schema for ERD
85

    
86
repl = ../bin/repl
87

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

    
91
##### DDL generation
92

    
93
pg_dump = env schema= ../bin/pg_dump_vegbien $(1) >$@
94

    
95
vegbien.sql:
96
	$(call pg_dump,public)
97

    
98
py_functions.sql:
99
	env owners=1 $(call pg_dump,py_functions)
100

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