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 785 aaronmk
##### General targets
7 387 aaronmk
8 2978 aaronmk
all = vegbien.sql vegbien.my.sql functions.sql py_functions.sql
9 387 aaronmk
10 418 aaronmk
all: _always $(all) ;
11 387 aaronmk
12
clean: _always
13
	$(RM) $(all)
14
15 6916 aaronmk
%/reinstall: _always %/uninstall %/install ;
16
17
##### Installation
18
19
schemas := temp functions py_functions public
20
install: $(schemas:%=%/install) ;
21
22
schemasReversed := public py_functions functions temp
23
uninstall: $(schemasReversed:%=%/uninstall) ;
24
25
psqlOpts := --set ON_ERROR_STOP=1 --quiet
26
asAdmin := sudo -E -u postgres
27
psqlAsAdmin := $(asAdmin) psql $(psqlOpts)
28
    # -E preserves env vars so PGOPTIONS is passed to psql
29
psqlAsAdminVegbien := $(psqlAsAdmin) vegbien
30
psqlNoSearchPath := env no_search_path=1 ../bin/psql_script_vegbien
31
32 6933 aaronmk
%/uninstall: _always
33
	@$(if $(filter public% r%,$*),$(call confirmRmPublicSchema,$*))
34
	echo $(call rmSchemaCmd,$*)|$(psqlNoSearchPath)
35
36 6916 aaronmk
#### public
37
38 6928 aaronmk
confirmRmPublicSchema = $(call confirm,WARNING: This will delete the $(1)\
39
schema of your VegBIEN DB!,$(2))
40 6916 aaronmk
41 6919 aaronmk
# Installs a version of the public schema
42 6934 aaronmk
public/install public%/install: vegbien.sql _always
43 6920 aaronmk
	echo $(call mkSchemaCmd,$(@D))\
44 6926 aaronmk
"COMMENT ON SCHEMA $(@D) IS 'Version: $(@D) ($(date))';"|$(psqlNoSearchPath)
45 6919 aaronmk
	<$< $(sed) 's/( )public([,; ])/\1$(@D)\2/g'|$(psqlNoSearchPath)
46
47 6922 aaronmk
# Replaces the current public schema with the given version
48 6932 aaronmk
%/publish: _always
49 6928 aaronmk
	@$(call confirmRmPublicSchema,public,To save it: make schemas/rotate)
50
	echo $(call rmSchemaCmd,public)\
51 6922 aaronmk
'ALTER SCHEMA $(@D) RENAME TO public;'|$(psqlNoSearchPath) --single-transaction
52
53 6916 aaronmk
rename/%: _always
54
	echo 'ALTER SCHEMA public RENAME TO "$*";'|$(psqlNoSearchPath)
55
	$(MAKE) public/install
56
57
rotate: _always rename/public.$(version) ;
58
59
#### py_functions
60
61
py_functions/install: py_functions.sql _always
62
	-<$< env public= $(psqlAsAdminVegbien)
63
# ignore errors if schema exists
64
65
#### Others
66
67
%/install: %.sql _always
68
	-<$< $(psqlNoSearchPath)
69
# ignore errors if schema exists
70
71
# Needed on Ubuntu 12.04 (also other Linuxes?) because %/reinstall is ignored.
72
temp/reinstall: _always temp/uninstall temp/install ;
73
74 785 aaronmk
##### MySQL schema for ERD
75 533 aaronmk
76 809 aaronmk
repl = ../bin/repl
77 541 aaronmk
78 809 aaronmk
%.my.sql: %.sql ../lib/PostgreSQL-MySQL.csv filter_ERD.csv
79
	$(repl) <$(wordlist 1,2,$+)|$(repl) $(word 3,$+) >$@
80 2979 aaronmk
81 6916 aaronmk
##### DDL generation
82 2979 aaronmk
83 6029 aaronmk
pg_dump = env schema= ../bin/pg_dump_vegbien $(1) >$@
84 2979 aaronmk
85
vegbien.sql:
86
	$(call pg_dump,public)
87
88
py_functions.sql:
89
	env owners=1 $(call pg_dump,py_functions)
90
91
# Must come after %.my.sql so that gets matched first
92
%.sql:
93
	$(call pg_dump,$*)