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 util.sql py_util.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 util py_util
25
install: $(schemas:%=%/install) ;
26

    
27
schemasReversed := py_util util temp
28
uninstall: $(schemasReversed:%=%/uninstall) ;
29

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

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

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

    
45
#### public
46

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

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

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

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

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

    
69
#### py_util
70

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

    
75
#### Others
76

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

    
80
##### MySQL schema for ERD
81

    
82
repl = ../bin/repl
83

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

    
87
##### DDL generation
88

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

    
91
vegbien.sql:
92
	unset version dump_opts; $(call pg_dump,public)
93

    
94
py_util.sql:
95
	env owners=1 $(call pg_dump,py_util)
96

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