Project

General

Profile

1 6912 aaronmk
selfDir_AkFFYJ := $(dir $(lastword $(MAKEFILE_LIST)))
2
root := $(selfDir_AkFFYJ)..
3
include $(root)/lib/common.Makefile
4
5
6 6979 aaronmk
##### Vars/functions
7
8
# Make
9
SHELL := /bin/bash
10
11 785 aaronmk
##### General targets
12 387 aaronmk
13 13154 aaronmk
all = public_.sql vegbien.sql vegbien.my.sql util.sql py_util.sql
14 387 aaronmk
15 418 aaronmk
all: _always $(all) ;
16 387 aaronmk
17
clean: _always
18
	$(RM) $(all)
19
20 6916 aaronmk
%/reinstall: _always %/uninstall %/install ;
21
22
##### Installation
23
24 8183 aaronmk
schemas := temp util py_util
25 6916 aaronmk
install: $(schemas:%=%/install) ;
26
27 8183 aaronmk
schemasReversed := py_util util temp
28 6916 aaronmk
uninstall: $(schemasReversed:%=%/uninstall) ;
29
30
psqlAsAdminVegbien := $(psqlAsAdmin) vegbien
31
psqlNoSearchPath := env no_search_path=1 ../bin/psql_script_vegbien
32 11793 aaronmk
psql_verbose := env no_search_path=1 ../bin/psql_verbose_vegbien
33 6916 aaronmk
34 13154 aaronmk
# Must come before `%/install: public_.sql` to override it
35 6935 aaronmk
%/install: %.sql _always
36
	-<$< $(psqlNoSearchPath)
37
# ignore errors if schema exists
38
39 12073 aaronmk
# must come before `%/uninstall: vegbien.sql` to override it
40
%/uninstall: %.sql _always
41 6933 aaronmk
	echo $(call rmSchemaCmd,$*)|$(psqlNoSearchPath)
42
43 6916 aaronmk
#### public
44
45 11793 aaronmk
schema_exists = $(shell $(psql_verbose) <<<'CREATE TEMP TABLE "$(1)".t ();'\
46
2>&1|grep -F 'cannot create temporary relation in non-temporary schema')
47 6916 aaronmk
48 11793 aaronmk
confirmRmPublicSchema = $(if $(schema_exists),$(call\
49
confirm,WARNING: This will delete the $(1) schema of your VegBIEN DB!,$(2)))
50
51 12543 aaronmk
*q = $(if $(filter public,$*),,")
52
53 6919 aaronmk
# Installs a version of the public schema
54 12129 aaronmk
# usage: make schemas/public/reinstall
55
#        make schemas/r#/reinstall
56 13154 aaronmk
%/install: public_.sql _always
57 6982 aaronmk
	echo $(call mkSchemaCmd,$*)\
58 10882 aaronmk
"COMMENT ON SCHEMA \"$*\" IS 'Version: $* ($(date))';"|$(psqlNoSearchPath)
59 12070 aaronmk
	# 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 12543 aaronmk
	<$< $(sed) -e \
64 13420 aaronmk
's/([ (]|::)public(_[[:alnum:]_]+)?([.,;]| [^[:lower:]])/\1$(*q)$*\2$(*q)\3/g'\
65 12140 aaronmk
|$(psqlNoSearchPath) # [[:alnum:]_]+, not ...*, because public_ is a username
66 6919 aaronmk
67 13154 aaronmk
%/uninstall: public_.sql _always
68 12073 aaronmk
	$(call confirmRmPublicSchema,$*)
69 12141 aaronmk
	# 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 12073 aaronmk
73 6922 aaronmk
# Replaces the current public schema with the given version
74 12129 aaronmk
# usage: make schemas/r#/publish
75 12541 aaronmk
# **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 6932 aaronmk
%/publish: _always
79 12145 aaronmk
	@$(call confirmRmPublicSchema,public)
80 12142 aaronmk
	$(psqlNoSearchPath) <<<'SELECT "$*".publish();'
81 6945 aaronmk
	@echo $(emph)'In your shell, run:'$(endEmph)'unset version'
82 6922 aaronmk
83 13571 aaronmk
rename/%: _always
84
	$(psqlNoSearchPath) <<<"SELECT util.schema_bundle_rename('public', '$*');"
85
	$(MAKE) public/install
86
87 13476 aaronmk
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 8183 aaronmk
#### py_util
96 6916 aaronmk
97 8183 aaronmk
py_util/install: py_util.sql _always
98 6955 aaronmk
	-<$< $(psqlAsAdminVegbien)
99 6916 aaronmk
# 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 785 aaronmk
##### MySQL schema for ERD
107 533 aaronmk
108 809 aaronmk
repl = ../bin/repl
109 541 aaronmk
110 809 aaronmk
%.my.sql: %.sql ../lib/PostgreSQL-MySQL.csv filter_ERD.csv
111
	$(repl) <$(wordlist 1,2,$+)|$(repl) $(word 3,$+) >$@
112 2979 aaronmk
113 6916 aaronmk
##### DDL generation
114 2979 aaronmk
115 6029 aaronmk
pg_dump = env schema= ../bin/pg_dump_vegbien $(1) >$@
116 2979 aaronmk
117 13154 aaronmk
public_.sql:
118 12042 aaronmk
	# include schemas that depend on `public` so they are restored along with it
119 12146 aaronmk
	unset version dump_opts; $(call pg_dump,public --schema='public_*')
120 2979 aaronmk
121 13475 aaronmk
vegbien.sql: public_.sql _always
122
	rm=1 ../inputs/.TNRS/schema.sql.run # its contents change along with this
123 13154 aaronmk
	(cat public_.sql; schema=1 ../bin/pg_dump_vegbien all --schema='geoscrub' \
124 13438 aaronmk
--schema='"TNRS"' --exclude-table='*."~"*' --exclude-table='*.*\y"Source"\y*') \
125
>$@
126 13154 aaronmk
# `all`: prevents auto-adding a --schema entry
127 13438 aaronmk
# exclude the same set of Source tables excluded by inputs/.TNRS/schema.sql, so
128
# that reinstalling TNRS doesn't change the contents of this file
129 13154 aaronmk
130 8183 aaronmk
py_util.sql:
131
	env owners=1 $(call pg_dump,py_util)
132 2979 aaronmk
133
# Must come after %.my.sql so that gets matched first
134
%.sql:
135
	$(call pg_dump,$*)