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
|
psqlAsAdminVegbien := $(psqlAsAdmin) vegbien
|
31
|
psqlNoSearchPath := env no_search_path=1 ../bin/psql_script_vegbien
|
32
|
psql_verbose := env no_search_path=1 ../bin/psql_verbose_vegbien
|
33
|
|
34
|
# Must come before `%/install: vegbien.sql` to override it
|
35
|
%/install: %.sql _always
|
36
|
-<$< $(psqlNoSearchPath)
|
37
|
# ignore errors if schema exists
|
38
|
|
39
|
# must come before `%/uninstall: vegbien.sql` to override it
|
40
|
%/uninstall: %.sql _always
|
41
|
echo $(call rmSchemaCmd,$*)|$(psqlNoSearchPath)
|
42
|
|
43
|
#### public
|
44
|
|
45
|
schema_exists = $(shell $(psql_verbose) <<<'CREATE TEMP TABLE "$(1)".t ();'\
|
46
|
2>&1|grep -F 'cannot create temporary relation in non-temporary schema')
|
47
|
|
48
|
confirmRmPublicSchema = $(if $(schema_exists),$(call\
|
49
|
confirm,WARNING: This will delete the $(1) schema of your VegBIEN DB!,$(2)))
|
50
|
|
51
|
# Installs a version of the public schema
|
52
|
# usage: make schemas/public/reinstall
|
53
|
# make schemas/r#/reinstall
|
54
|
%/install: vegbien.sql _always
|
55
|
echo $(call mkSchemaCmd,$*)\
|
56
|
"COMMENT ON SCHEMA \"$*\" IS 'Version: $* ($(date))';"|$(psqlNoSearchPath)
|
57
|
# create a custom *_validation schema for *each* public schema, rather than
|
58
|
# one for the most recently-created public schema. this allows validations
|
59
|
# to continue to be run against a previous version of the DB while a new
|
60
|
# version is being imported.
|
61
|
<$< $(sed) -e 's/( )public(_[[:alnum:]_]+)?([,;]| [^[:lower:]])/\1"$*\2"\3/g'\
|
62
|
|$(psqlNoSearchPath) # [[:alnum:]_]+, not ...*, because public_ is a username
|
63
|
|
64
|
%/uninstall: vegbien.sql _always
|
65
|
$(call confirmRmPublicSchema,$*)
|
66
|
# don't use public.rm(), because when the public schema is incompletely
|
67
|
# imported, this function will not yet exist
|
68
|
$(psqlNoSearchPath) <<<"SELECT util.schema_bundle_rm('$*');"
|
69
|
|
70
|
# Replaces the current public schema with the given version
|
71
|
# usage: make schemas/r#/publish
|
72
|
%/publish: _always
|
73
|
@$(call confirmRmPublicSchema,public,To save it: make schemas/rotate)
|
74
|
$(psqlNoSearchPath) <<<'SELECT "$*".publish();'
|
75
|
@echo $(emph)'In your shell, run:'$(endEmph)'unset version'
|
76
|
|
77
|
#### py_util
|
78
|
|
79
|
py_util/install: py_util.sql _always
|
80
|
-<$< $(psqlAsAdminVegbien)
|
81
|
# ignore errors if schema exists
|
82
|
|
83
|
#### Others
|
84
|
|
85
|
# Needed on Ubuntu 12.04 (also other Linuxes?) because %/reinstall is ignored.
|
86
|
temp/reinstall: _always temp/uninstall temp/install ;
|
87
|
|
88
|
##### MySQL schema for ERD
|
89
|
|
90
|
repl = ../bin/repl
|
91
|
|
92
|
%.my.sql: %.sql ../lib/PostgreSQL-MySQL.csv filter_ERD.csv
|
93
|
$(repl) <$(wordlist 1,2,$+)|$(repl) $(word 3,$+) >$@
|
94
|
|
95
|
##### DDL generation
|
96
|
|
97
|
pg_dump = env schema= ../bin/pg_dump_vegbien $(1) >$@
|
98
|
|
99
|
vegbien.sql:
|
100
|
# include schemas that depend on `public` so they are restored along with it
|
101
|
unset version dump_opts; $(call pg_dump,public --schema=public_validation)
|
102
|
|
103
|
py_util.sql:
|
104
|
env owners=1 $(call pg_dump,py_util)
|
105
|
|
106
|
# Must come after %.my.sql so that gets matched first
|
107
|
%.sql:
|
108
|
$(call pg_dump,$*)
|