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