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 = public_.sql 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: public_.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
*q = $(if $(filter public,$*),,")
52

    
53
# Installs a version of the public schema
54
# usage: make schemas/public/reinstall
55
#        make schemas/r#/reinstall
56
%/install: public_.sql _always
57
	echo $(call mkSchemaCmd,$*)\
58
"COMMENT ON SCHEMA \"$*\" IS 'Version: $* ($(date))';"|$(psqlNoSearchPath)
59
	# create a custom *_validation schema for *each* public schema, rather than
60
	# one for the most recently-created public schema. this allows validations
61
	# to continue to be run against a previous version of the DB while a new
62
	# version is being imported.
63
	<$< $(sed) -e \
64
's/([ (]|::)public(_[[:alnum:]_]+)?([.,;]| [^[:lower:]])/\1$(*q)$*\2$(*q)\3/g'\
65
|$(psqlNoSearchPath) # [[:alnum:]_]+, not ...*, because public_ is a username
66

    
67
%/uninstall: public_.sql _always
68
	$(call confirmRmPublicSchema,$*)
69
	# don't use public.rm(), because when the public schema is incompletely
70
	# imported, this function will not yet exist
71
	$(psqlNoSearchPath) <<<"SELECT util.schema_bundle_rm('$*');"
72

    
73
# Replaces the current public schema with the given version
74
# usage: make schemas/r#/publish
75
# **IMPORTANT**: you should not publish a schema until you are satisfied that it
76
# can replace the previous public schema, so that the previous public schema
77
# doesn't need to be saved.
78
%/publish: _always
79
	@$(call confirmRmPublicSchema,public)
80
	$(psqlNoSearchPath) <<<'SELECT "$*".publish();'
81
	@echo $(emph)'In your shell, run:'$(endEmph)'unset version'
82

    
83
rename/%: _always
84
	$(psqlNoSearchPath) <<<"SELECT util.schema_bundle_rename('public', '$*');"
85
	$(MAKE) public/install
86

    
87
vegbien/install: _always
88
	../inputs/.TNRS/run install
89
	$(MAKE) public/install
90

    
91
vegbien/uninstall: _always
92
	$(MAKE) public/uninstall
93
	(cd ..; make inputs/.TNRS/uninstall)
94

    
95
#### py_util
96

    
97
py_util/install: py_util.sql _always
98
	-<$< $(psqlAsAdminVegbien)
99
# ignore errors if schema exists
100

    
101
#### Others
102

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

    
106
##### MySQL schema for ERD
107

    
108
repl = ../bin/repl
109

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

    
113
##### DDL generation
114

    
115
pg_dump = env schema= ../bin/pg_dump_vegbien $(1) >$@
116

    
117
public_.sql:
118
	# include schemas that depend on `public` so they are restored along with it
119
	unset version dump_opts; $(call pg_dump,public --schema='public_*')
120

    
121
vegbien.sql: public_.sql _always
122
	rm=1 ../inputs/.TNRS/schema.sql.run # its contents change along with this
123
	rm=1 ../inputs/.TNRS/data.sql.run   # its contents change along with this
124
	(cat public_.sql; schema=1 ../bin/pg_dump_vegbien all --schema='geoscrub' \
125
--schema='"TNRS"' --exclude-table='*."~"*' --exclude-table='*.*\y"Source"\y*') \
126
>$@
127
# `all`: prevents auto-adding a --schema entry
128
# exclude the same set of Source tables excluded by inputs/.TNRS/schema.sql, so
129
# that reinstalling TNRS doesn't change the contents of this file
130

    
131
py_util.sql:
132
	env owners=1 $(call pg_dump,py_util)
133

    
134
# Must come after %.my.sql so that gets matched first
135
%.sql:
136
	$(call pg_dump,$*)
(5-5/31)