Project

General

Profile

1 256 aaronmk
selfDir_3d1bc249 := $(dir $(lastword $(MAKEFILE_LIST)))
2 250 aaronmk
3 368 aaronmk
# Make
4
addBeforeExt = $(basename $(2))$(1)$(suffix $(2))
5
6
# Commands
7 395 aaronmk
CP = cp -p
8
copyTo = $(CP) $(+:_%=)
9
DIFF = diff --unified=2
10 368 aaronmk
11
table = $(shell s="$(*F)"; echo "$${s\#\#*.}")# remove sort order prefix
12
13 264 aaronmk
vegxMaps := $(wildcard map.VegX.*.csv)
14
vegbienMaps := $(subst .VegX.,.VegBIEN.,$(vegxMaps))
15 368 aaronmk
tables := $(vegbienMaps:map.VegBIEN.%.csv=%)
16 247 aaronmk
17 368 aaronmk
root := $(selfDir_3d1bc249)..
18
psqlAsBien := $(root)/bin/psql_vegbien
19 353 aaronmk
20 401 aaronmk
# Config
21
test_n ?= 2
22
23 250 aaronmk
#####
24
25 383 aaronmk
all: _always maps import verify
26 247 aaronmk
27
.SUFFIXES:
28
29 383 aaronmk
_always:
30
.PHONY: _always
31 250 aaronmk
32 383 aaronmk
clean: _always
33 256 aaronmk
	$(RM) $(all)
34
35 383 aaronmk
rm-%: _always
36 368 aaronmk
	$(RM) $*
37
38 383 aaronmk
%.out: % _always
39 368 aaronmk
	./$* >$@
40
.PRECIOUS: %.out
41
42 250 aaronmk
#####
43
44 383 aaronmk
reinstall: _always uninstall install
45 264 aaronmk
46
#####
47
48 383 aaronmk
maps: $(vegbienMaps) _always
49 368 aaronmk
50
all += $(vegbienMaps)
51
52 264 aaronmk
map.VegBIEN.%.csv: map.VegX.%.csv
53 353 aaronmk
	$(root)/bin/join_sort <$< $(root)/mappings/VegX-VegBIEN.$(table).csv >$@
54 264 aaronmk
.PRECIOUS: map.VegBIEN.%.csv
55
56 368 aaronmk
#####
57
58 383 aaronmk
import: _always $(addprefix import-,$(tables))
59 368 aaronmk
60
log = $*$(if $(n),.n=$(n),).log
61
62 339 aaronmk
logs := $(wildcard *.log)
63
64 383 aaronmk
rm_logs: _always
65 339 aaronmk
	$(RM) $(logs)
66
67 264 aaronmk
#####
68
69 383 aaronmk
verify: _always $(addprefix verify-,$(tables))
70 368 aaronmk
71 383 aaronmk
verify-%: verify.%.ref verify.%.out _always
72 368 aaronmk
	$(DIFF)
73
74 383 aaronmk
%.out: %.sql _always
75 371 aaronmk
	$(psqlAsBien) --no-align --field-separator='	' --pset=footer=off <$< >$@
76 370 aaronmk
.PRECIOUS: %.out
77 369 aaronmk
78 370 aaronmk
all += $(wildcard *.out)
79 368 aaronmk
80
#####
81
82 383 aaronmk
test: _always $(addprefix test-,$(tables))
83 368 aaronmk
84 395 aaronmk
test-%: test.%.out _always
85
	$(DIFF) $(<:.out=.ref) $<
86 368 aaronmk
87 369 aaronmk
all += $(filter-out %.ref,$(wildcard test.*.out))
88 368 aaronmk
89 395 aaronmk
accept-%: _always
90
	$(CP) $* $(*:.out=.ref)
91
92 368 aaronmk
#####
93
94 256 aaronmk
ifdef db
95
96 264 aaronmk
mapEnv := in_engine=$(dbEngine) in_database=$(db)
97 256 aaronmk
98 368 aaronmk
####
99 256 aaronmk
100 368 aaronmk
ifeq ($(dbEngine),MySQL)
101 339 aaronmk
102 368 aaronmk
bienPassword := $(shell cat $(root)/config/bien_password)
103
mysqlAs = mysql --user=$(1) --password='$(bienPassword)'
104
mysqlAsRoot := $(call mysqlAs,root)
105
mysqlAsBien := $(call mysqlAs,bien)
106
107 383 aaronmk
import-%: map.VegBIEN.%.csv _always
108 385 aaronmk
	-$(import_cmd)
109 383 aaronmk
import_cmd = (set -x; "time" env commit=1 $(map) $<) \
110 368 aaronmk
$(if $(n),,>>$(log)) 2>&1$(if $(n),|tee -a $(log))
111 256 aaronmk
112 369 aaronmk
%.ref: %.ref.sql
113 371 aaronmk
	$(mysqlAsBien) $(db) <$< >$@
114 369 aaronmk
.PRECIOUS: %.ref
115 368 aaronmk
116
###
117
118 383 aaronmk
install: _always db
119 256 aaronmk
120 383 aaronmk
uninstall: _always rm_db
121 256 aaronmk
122 383 aaronmk
db: src.$(db).sql _always
123 368 aaronmk
	-$(mysqlAsRoot) <$<
124
	echo "GRANT SELECT ON $(db).* TO 'bien'@'localhost';"|$(mysqlAsRoot)
125 255 aaronmk
# ignore errors in db import so that GRANT will still be run
126 250 aaronmk
127 383 aaronmk
rm_db: _always
128 368 aaronmk
	-echo "REVOKE ALL ON $(db).* FROM 'bien'@'localhost';"|$(mysqlAsRoot)
129
	echo "DROP DATABASE IF EXISTS $(db);"|$(mysqlAsRoot)
130 365 aaronmk
# ignore errors if grant not defined
131 250 aaronmk
132 368 aaronmk
###
133
134
else
135
$(error dbEngine variable must be one of the following: MySQL, PostgreSQL)
136
endif
137
138
####
139
140 383 aaronmk
test.%.out: map.VegBIEN.%.csv _always
141 401 aaronmk
	env test=1 n=$(test_n) $(map) $< >$@ 2>&1 || { e=$$?; cat $@; exit $$e;}
142 368 aaronmk
.PRECIOUS: test.%.out
143
144 256 aaronmk
#####
145
146
else
147
$(error db variable must be set)
148
endif
149 368 aaronmk
150
#####
151
152
# Must come after mapEnv is set
153
map := env $(mapEnv) out_database=vegbien $(root)/map