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