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 8183 aaronmk
all = 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 6935 aaronmk
# Must come before `%/install: vegbien.sql` to override it
35
%/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 6919 aaronmk
# Installs a version of the public schema
52 12129 aaronmk
# usage: make schemas/public/reinstall
53
#        make schemas/r#/reinstall
54 6935 aaronmk
%/install: vegbien.sql _always
55 6982 aaronmk
	echo $(call mkSchemaCmd,$*)\
56 10882 aaronmk
"COMMENT ON SCHEMA \"$*\" IS 'Version: $* ($(date))';"|$(psqlNoSearchPath)
57 12070 aaronmk
	# 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 12130 aaronmk
	<$< $(sed) -e 's/( )public(_validation)([,; ])/\1"$*\2"\3/g'$(if\
62 12071 aaronmk
$(filter public,$*),, -e 's/( )public([,; ])/\1"$*"\2/g')|$(psqlNoSearchPath)
63 6919 aaronmk
64 12073 aaronmk
%/uninstall: vegbien.sql _always
65
	$(call confirmRmPublicSchema,$*)
66 12074 aaronmk
	echo $(call rmSchemaCmd,$*_validation)$(call rmSchemaCmd,$*)\
67
|$(psqlNoSearchPath)
68 12073 aaronmk
69 6922 aaronmk
# Replaces the current public schema with the given version
70 12129 aaronmk
# usage: make schemas/r#/publish
71 6932 aaronmk
%/publish: _always
72 6928 aaronmk
	@$(call confirmRmPublicSchema,public,To save it: make schemas/rotate)
73 12072 aaronmk
	echo $(call rmSchemaCmd,public)'ALTER SCHEMA "$*" RENAME TO public; \
74
ALTER SCHEMA "$*_validation" RENAME TO public_validation;'\
75 6986 aaronmk
|$(psqlNoSearchPath) --file - --single-transaction
76 6945 aaronmk
	@echo $(emph)'In your shell, run:'$(endEmph)'unset version'
77 6922 aaronmk
78 8183 aaronmk
#### py_util
79 6916 aaronmk
80 8183 aaronmk
py_util/install: py_util.sql _always
81 6955 aaronmk
	-<$< $(psqlAsAdminVegbien)
82 6916 aaronmk
# ignore errors if schema exists
83
84
#### Others
85
86
# Needed on Ubuntu 12.04 (also other Linuxes?) because %/reinstall is ignored.
87
temp/reinstall: _always temp/uninstall temp/install ;
88
89 785 aaronmk
##### MySQL schema for ERD
90 533 aaronmk
91 809 aaronmk
repl = ../bin/repl
92 541 aaronmk
93 809 aaronmk
%.my.sql: %.sql ../lib/PostgreSQL-MySQL.csv filter_ERD.csv
94
	$(repl) <$(wordlist 1,2,$+)|$(repl) $(word 3,$+) >$@
95 2979 aaronmk
96 6916 aaronmk
##### DDL generation
97 2979 aaronmk
98 6029 aaronmk
pg_dump = env schema= ../bin/pg_dump_vegbien $(1) >$@
99 2979 aaronmk
100
vegbien.sql:
101 12042 aaronmk
	# include schemas that depend on `public` so they are restored along with it
102 12130 aaronmk
	unset version dump_opts; $(call pg_dump,public --schema=public_validation)
103 2979 aaronmk
104 8183 aaronmk
py_util.sql:
105
	env owners=1 $(call pg_dump,py_util)
106 2979 aaronmk
107
# Must come after %.my.sql so that gets matched first
108
%.sql:
109
	$(call pg_dump,$*)