Project

General

Profile

1 3761 aaronmk
selfDir_uZPPqC := $(dir $(lastword $(MAKEFILE_LIST)))
2
root := $(selfDir_uZPPqC)..
3
include $(root)/lib/common.Makefile
4
5
6 783 aaronmk
##### Configuration
7
8 1921 aaronmk
# Command line
9 7245 aaronmk
full_import ?=
10 6592 aaronmk
import_source ?= 1
11 1190 aaronmk
log ?= $(if $(test),,1)
12 1257 aaronmk
profile ?=
13 1983 aaronmk
quiet ?=
14 1620 aaronmk
reverify ?= 1
15 5810 aaronmk
schema_only ?=
16 1990 aaronmk
use_staged ?= $(by_col)
17 1921 aaronmk
18
# Makefile
19 7650 aaronmk
exts ?= csv tsv tab txt dat dmp xml
20 783 aaronmk
test_n ?= 2
21
22
##### Vars/functions
23
24 1821 aaronmk
# Paths
25 5008 aaronmk
datasrc := $(patsubst .%,%,$(notdir $(realpath .)))
26 1821 aaronmk
bin := $(root)/bin
27
mappings := $(root)/mappings
28
29 368 aaronmk
# Make
30 640 aaronmk
SHELL := /bin/bash
31 1509 aaronmk
selfMake = $(MAKE) --makefile=../input.Makefile
32 1821 aaronmk
subMake = $(MAKE) $(@:$(root)/%=%) --directory=$(root)
33 404 aaronmk
+_ = $(+:_%=)
34 368 aaronmk
addBeforeExt = $(basename $(2))$(1)$(suffix $(2))
35
36 640 aaronmk
# Terminal
37 1184 aaronmk
termCols := $(shell tput cols)
38 640 aaronmk
39 368 aaronmk
# Commands
40 1245 aaronmk
MKDIR = mkdir -p
41
mkdir = $(MKDIR) $(@D)
42 1184 aaronmk
diff = diff --unified=2
43 3721 aaronmk
diffIgnoreSpace = $(diff) --ignore-space-change
44 1184 aaronmk
diffVerbose = $(if $(verbose),diff --side-by-side --left-column\
45
--width=$(termCols),$(diff))
46 368 aaronmk
47 1918 aaronmk
# BIEN commands
48 4137 aaronmk
sortFilenames = $(shell $(bin)/sort_filenames $(1))
49 1524 aaronmk
selfMap = $(bin)/cols 0 0
50 8108 aaronmk
psqlAsBien := $(bin)/psql_verbose_vegbien
51 6186 aaronmk
psqlNoSearchPath := env no_search_path=1 $(psqlAsBien)
52 4408 aaronmk
# Usage: ($(inDatasrc); cat $(file))|$(psqlCmd)
53 6186 aaronmk
inDatasrc := echo 'SET search_path TO "$(datasrc)";'
54 353 aaronmk
55 1594 aaronmk
# SVN
56 1771 aaronmk
setSvnIgnore = svn propset svn:ignore $(2) $(1)
57 1594 aaronmk
define addDirWithIgnore
58
$(addDir)
59 1771 aaronmk
$(setSvnIgnore)
60 1594 aaronmk
endef
61
62 4414 aaronmk
##### Environment
63
64
export PATH := $(bin):$(PATH)
65
66 783 aaronmk
##### General targets
67 250 aaronmk
68 418 aaronmk
all: _always maps ;
69 247 aaronmk
70 383 aaronmk
clean: _always
71 256 aaronmk
	$(RM) $(all)
72
73 1742 aaronmk
remake: _always clean
74
	+$(selfMake)
75
# re-run make so that cache of existing files is reset
76
77 5876 aaronmk
# Only remake if doesn't exist. This prevents unintentional remaking when the
78
# make script is newly checked out from svn (which sets the mod time to now) but
79
# the output is synced externally.
80
# Can't remove prereq to do this, because it determines when the rule applies.
81
make_script = $(if $(wildcard $@),,"time" ./$< >$@)
82 368 aaronmk
83 6743 aaronmk
%/: %/map.csv _always ;
84
85 1604 aaronmk
%/: % _always ;
86
87 4379 aaronmk
%: %.make
88 5876 aaronmk
	$(make_script)
89 4182 aaronmk
.PRECIOUS: % # save partial outputs of aborted src make scripts
90 1627 aaronmk
91 6379 aaronmk
##### Tables discovery
92 250 aaronmk
93 6379 aaronmk
sortFile := import_order.txt
94 6156 aaronmk
noImportFile := _no_import
95
96 6592 aaronmk
dontImport = $(wildcard $(1)/$(noImportFile))$(if\
97
$(import_source),,$(filter Source,$(1)))
98 6379 aaronmk
99
tables := $(if $(wildcard $(sortFile)),$(shell cat $(sortFile)))
100
    # $(shell) replaces "\n" with " "
101
allSubdirs := $(call wildcard/,*/)
102
allTables := $(call sortFilenames,$(filter-out _% verify logs,$(allSubdirs:%/=%)))
103
joinedTables := $(filter-out $(tables),$(allTables))
104
allTables := $(strip $(joinedTables) $(tables))# move joined tables to beginning
105
ifeq ($(tables),)# none specified in sort file
106
tables := $(allTables)
107
endif
108
importTables := $(foreach table,$(tables),$(if\
109
$(call dontImport,$(table)),,$(table)))
110
111
##### SVN
112
113 6990 aaronmk
svnFilesGlob:= */{,$(noImportFile),{,.}{map,VegBIEN}.csv{,.*},*header.*,*.sql,test.xml.ref}
114
svnFilesGlob := {map.csv,*{schema,~}*.sql,{,*/}{*.make,*terms.csv},$(svnFilesGlob)}
115 7568 aaronmk
_svnFilesGlob := {_MySQL/{,*schema*.sql,*.make},{,*/}{*.{md5,url,pdf},*README.TXT}}
116 5923 aaronmk
svnFiles = $(filter-out _% logs/%,$(call wildcard/,$(svnFilesGlob)))\
117
$(call wildcard/,$(_svnFilesGlob))
118 5878 aaronmk
119 6806 aaronmk
# To update all inputs with these settings: make inputs/add
120 6591 aaronmk
add: _always Source/add Source/map.csv $(allTables:%=%/add)
121 4182 aaronmk
	$(call setSvnIgnore,.,'*')
122 4459 aaronmk
	$(call addDirWithIgnore,logs,$$'*.log.sql\n*.trace')
123 6805 aaronmk
	$(call addDirWithIgnore,verify,$$'*.out\n*.csv\n*.xls\n*.xlsx')
124 4223 aaronmk
	$(call addFile,import_order.txt)
125 6014 aaronmk
	$(if $(wildcard _MySQL/),$(call addDirWithIgnore,_MySQL,'*'))
126 6064 aaronmk
	$(if $(wildcard _src/),$(call addDirWithIgnore,_src,'*'))
127 6733 aaronmk
	$(if $(wildcard _archive/),$(call addDirWithIgnore,_archive,'*'))
128 5921 aaronmk
	$(call add*,$(svnFiles))
129 766 aaronmk
130 4217 aaronmk
# Adds a new table subdir
131
%/add: _always
132
	$(call addDirWithIgnore,$*,'*')
133
	$(call addDirWithIgnore,$*/logs,$$'*.log.sql\n*.trace')
134
135 1969 aaronmk
##### Existing maps discovery
136 1955 aaronmk
137 5035 aaronmk
anyMap := %/map.csv %/VegBIEN.csv %/unmapped_terms.csv %/new_terms.csv
138 3574 aaronmk
139 7386 aaronmk
exts := $(call ci,$(exts))
140 4214 aaronmk
extsFilter := $(addprefix %.,$(exts))
141
dataOnly = $(filter $(extsFilter),$(1))
142
143 4182 aaronmk
anyTest = $*/test.%
144 4214 aaronmk
srcsOnly = $(filter-out $(anyMap) $(anyTest) %/logs,$(call dataOnly,$(1)))
145 4120 aaronmk
146 6071 aaronmk
srcDict := map.csv
147
148 7485 aaronmk
vocab := $(mappings)/VegCore.vocab.csv
149
thesaurus := $(mappings)/VegCore.thesaurus.csv
150 4658 aaronmk
coreMap := $(mappings)/VegCore-VegBIEN.csv
151 783 aaronmk
152 6157 aaronmk
viaMaps := $(importTables:%=%/map.csv)
153 783 aaronmk
154 4117 aaronmk
autogenMaps := $(subst map.,VegBIEN.,$(viaMaps))
155 728 aaronmk
directMaps := $(autogenMaps) $(filter-out $(autogenMaps),\
156 4182 aaronmk
$(wildcard */VegBIEN.csv))
157 728 aaronmk
158 1969 aaronmk
##### Sources
159
160 4182 aaronmk
srcs = $(call sortFilenames,$(call srcsOnly,$(wildcard $*/*)))
161 4450 aaronmk
nonHeaderSrcs = $(filter-out %/header.csv,$(srcs))
162
isRef = $(if $(nonHeaderSrcs),,1)
163
    # empty subdir, so references an already-installed staging table
164 4452 aaronmk
isXml = $(filter %.xml,$(nonHeaderSrcs))
165
nonXml = $(if $(isXml),,1)
166
isCsv = $(if $(nonHeaderSrcs),$(if $(isXml),,1))
167 1969 aaronmk
    # true if $(srcs) non-empty and contains no *.xml
168 8159 aaronmk
catSrcs = $(bin)/cat$(if $(nonXml),_csv) $(nonHeaderSrcs)
169 1969 aaronmk
withCatSrcs = $(catSrcs:$(bin)/%=$(bin)/with_%) --
170
171 7076 aaronmk
# Run with `make -s` to avoid echoing make commands
172 6157 aaronmk
cat: $(importTables:%=%/cat) _always ;
173 1969 aaronmk
174 4252 aaronmk
%/cat: _always
175 1969 aaronmk
	$(catSrcs)
176
177 6363 aaronmk
##### Input data retrieval
178
179
# Must come before `%.sql: _MySQL/%.sql` to override it
180
%.sql: %.sql.make
181
	$(make_script)
182
183
# The export must be created with:
184
# `--compatible=postgresql --add-locks=false --set-charset --no-create-info`
185
# Must come before `%.sql: _MySQL/%.sql` to override it
186
%.data.sql: _MySQL/%.data.sql
187
	$(if $(wildcard $@),,$(bin)/my2pg.data <$< >$@)
188
189
# The export must be created with:
190
# `--compatible=postgresql --add-locks=false --set-charset`
191
# Add `--no-data` to create a schema-only export.
192
%.sql: _MySQL/%.sql
193
	$(if $(wildcard $@),,$(bin)/my2pg <$< >$@)
194
195 4121 aaronmk
##### Staging tables installation
196 1921 aaronmk
197 5693 aaronmk
srcTable := %.src
198
199 6364 aaronmk
dbExportsWildcard = $(sort $(patsubst _MySQL/%.make,%,$(wildcard\
200
$(1) _MySQL/$(1).make)))
201
202
dbExports := $(call dbExportsWildcard,*schema*.sql)# schemas first
203
ifeq ($(schema_only),) # add rest of .sql files
204
dbExports += $(filter-out $(dbExports),$(call dbExportsWildcard,*.sql))
205 5810 aaronmk
endif
206 4438 aaronmk
dbExports := $(strip $(dbExports))# += adds extra whitespace
207 6565 aaronmk
allInstalls := $(if $(dbExports),sql) $(filter-out Source,$(allTables))
208 1921 aaronmk
209 4413 aaronmk
install: _always schema $(allInstalls:%=%/install) ;
210
211 5209 aaronmk
uninstall: _always confirm_rm_schema rm_schema ;
212 4121 aaronmk
# rm_schema will also drop all staging tables
213 1921 aaronmk
214
reinstall: _always uninstall install ;
215
216 5209 aaronmk
confirm_rm_schema: _always
217
	$(if $(filter TNRS,$(datasrc)),$(call confirm,WARNING: This will delete the\
218
TNRS cache!,To save it: make backups/TNRS.backup-remake))
219
220 4121 aaronmk
schema: _always
221 6186 aaronmk
	-echo 'CREATE SCHEMA "$(datasrc)";'|$(psqlNoSearchPath)
222 1921 aaronmk
# ignore errors if schema exists
223
224 4121 aaronmk
rm_schema: _always
225 6186 aaronmk
	echo 'DROP SCHEMA IF EXISTS "$(datasrc)" CASCADE;'|$(psqlNoSearchPath)
226 1921 aaronmk
227 5162 aaronmk
installLog := logs/install.log.sql
228 4453 aaronmk
229 7204 aaronmk
logInstall = $(if $(quiet),$(2)$(1)$(installLog) 2>&1,2>&1|tee $(3)\
230
$(1)$(installLog))
231 5692 aaronmk
logInstallRoot = $(call logInstall,,>)
232 5162 aaronmk
logInstall* = $(call logInstall,$*/,>)
233
logInstall*Add = $(call logInstall,$*/,>>,-a)# append to log
234
235 4413 aaronmk
# Must come before %/install to override it
236 4434 aaronmk
sql/install: $(dbExports)
237 6186 aaronmk
	($(inDatasrc); cat $+|pg_dump_limit)|"time" $(psqlNoSearchPath) \
238 5796 aaronmk
--set=schema='"$(datasrc)"' $(logInstallRoot)
239 4413 aaronmk
240 5795 aaronmk
cleanup = $(if $(wildcard $*/cleanup.sql),($(inDatasrc); cat $*/cleanup.sql)\
241 6186 aaronmk
|"time" $(psqlNoSearchPath) --echo-all --set=table='"$*"' $(logInstall*Add),\
242 7065 aaronmk
(export schema=$(datasrc) table=$*; . $(bin)/vegbien_dest; unset schemas; \
243 6186 aaronmk
$(bin)/csv2db) $(logInstall*Add))
244 4449 aaronmk
245 8160 aaronmk
%/header.csv:
246 8201 aaronmk
	echo 'COPY (SELECT * FROM "$(datasrc)"."$*" LIMIT 0) TO STDOUT CSV HEADER;'|\
247
env no_search_path=1 $(bin)/psql_script_vegbien >$*/header.csv
248 4449 aaronmk
249 8160 aaronmk
exportHeader = $(selfMake) $*/header.csv
250
251 7067 aaronmk
# Don't try to edit a view. Must come before %/install to override it.
252
%_view/install: _always ;
253
254 8238 aaronmk
%.sql/run: _always
255
	$(if $(wildcard $(@D)),($(inDatasrc); cat $(@D))|(cd '$(*D)';\
256
"time" env no_search_path=1 ../$(bin)/psql_verbose_vegbien \
257
--set=table='"$(*D)"' --set=table_str=\''"$(*D)"'\'))
258
259 8239 aaronmk
%/postprocess: _always %/postprocess.sql/run
260
	$(if $(wildcard $*/import),$*/import)
261 8238 aaronmk
262 4260 aaronmk
# For staging tables which are derived by joining together other staging tables.
263 4263 aaronmk
%/install %/header.csv: %/create.sql _always
264 4527 aaronmk
	($(inDatasrc); echo 'CREATE TABLE "$*" AS'; cat $<; echo ';')|"time" \
265 6186 aaronmk
$(psqlNoSearchPath) --echo-all --set=schema='"$(datasrc)"' --set=table='"$*"' \
266 5912 aaronmk
$(logInstall*)
267 8238 aaronmk
	$(selfMake) $*/postprocess
268 4449 aaronmk
	$(exportHeader)
269 8157 aaronmk
	$(cleanup)
270 5441 aaronmk
.PRECIOUS: %/header.csv
271 4260 aaronmk
272 4252 aaronmk
%/install: _always
273 8154 aaronmk
	$(if $(nonXml),$(import_install_))
274 8238 aaronmk
	$(selfMake) $*/postprocess
275 8154 aaronmk
	$(exportHeader)
276 8157 aaronmk
	$(cleanup)
277 5693 aaronmk
define import_install_
278 6950 aaronmk
(. $(bin)/vegbien_dest; unset schemas; "time" nice -n +5\
279 5028 aaronmk
env schema=$(datasrc) table=$* $(bin)/csv2db $(catSrcs) $(logInstall*))
280 5693 aaronmk
$(if $(filter $(srcTable),$*),($(inDatasrc);\
281
echo 'ALTER TABLE "$(datasrc)"."$*" RENAME row_num TO "$*.row_num";')|"time"\
282 6186 aaronmk
$(psqlNoSearchPath) --echo-all --set=table='"$*"' $(logInstall*Add))
283 5693 aaronmk
endef
284 5697 aaronmk
# table-scope src table's row_num col to allow joining it with other tables
285 1921 aaronmk
286 5819 aaronmk
%/uninstall: _always
287 6186 aaronmk
	echo 'DROP TABLE IF EXISTS "$(datasrc)"."$*" CASCADE;'|$(psqlNoSearchPath)
288 5819 aaronmk
289
%/reinstall: _always %/uninstall %/install ;
290
291 8241 aaronmk
postprocess: _always $(allTables:%=%/postprocess) ;
292 8113 aaronmk
293 5155 aaronmk
cleanup: _always $(tables:%=%/cleanup) ;
294
295 5184 aaronmk
# WARNING: This removes any index comments, due to a PostgreSQL bug.
296
# This occurs because ALTER TABLE recreates the index but not its comment.
297 5155 aaronmk
%/cleanup: _always
298
	$(cleanup)
299
300 1777 aaronmk
##### Maps building
301
302 4646 aaronmk
# Maps to (try to) build are added to this
303 1779 aaronmk
maps :=
304
305 4208 aaronmk
srcRoot = $(mappings)/root.sh
306 8156 aaronmk
mkSrcMap = (. $(srcRoot); env datasrc=$(datasrc) $(bin)/src_map <$*/header.csv\
307
>$@)
308 1779 aaronmk
309 7437 aaronmk
translate = $(if $(wildcard $(1)),$(bin)/in_place $< $(bin)/translate_ci 1 $(1))
310 6074 aaronmk
311 6076 aaronmk
$(srcDict):# empty target in case it doesn't exist
312
313 3572 aaronmk
# Via maps cleanup
314 4182 aaronmk
ifneq ($(filter %/.map.csv.last_cleanup,$(MAKECMDGOALS)),)
315 7484 aaronmk
%/.map.csv.last_cleanup: %/map.csv $(vocab) $(thesaurus) $(coreMap) $(srcDict)
316 7437 aaronmk
	$(call translate,$(srcDict))
317 4844 aaronmk
	$(bin)/in_place $< $(bin)/canon 1 $(vocab)
318 7484 aaronmk
	$(call translate,$(thesaurus))
319 8176 aaronmk
	$(bin)/in_place $< $(bin)/fix_line_endings
320 3572 aaronmk
	touch $@
321 4601 aaronmk
	+$(selfMake) $(<:%/map.csv=%/unmapped_terms.csv)
322
	+$(selfMake) $(<:%/map.csv=%/new_terms.csv)
323 5253 aaronmk
.PRECIOUS: %/.map.csv.last_cleanup
324 3572 aaronmk
else
325 4644 aaronmk
%/map.csv: _always
326
	$(if $(wildcard $@),,$(if $(nonXml),$(mkSrcMap)))
327 4182 aaronmk
	+$(selfMake) $(@:%/map.csv=%/.map.csv.last_cleanup)
328 5254 aaronmk
.PRECIOUS: %/map.csv
329 3572 aaronmk
endif
330
331 4641 aaronmk
%/VegBIEN.csv: %/map.csv $(coreMap)
332 4656 aaronmk
	<$< $(bin)/cat_cols 1 2|$(bin)/join $(coreMap)|$(bin)/sort_map >$@
333 1530 aaronmk
maps += $(autogenMaps)
334
335 4646 aaronmk
maps: $(maps) _always ;
336 1139 aaronmk
337
all += $(maps)
338
339 1742 aaronmk
##### Maps validation
340
341 4663 aaronmk
# `tail -n +2`: Remove header before running filter_out_ci because filter_out_ci
342
# only removes the header if it matches the vocabulary's header.
343 4601 aaronmk
344 4664 aaronmk
%/unmapped_terms.csv: %/map.csv $(coreMap)
345 4858 aaronmk
	tail -n +2 $<|$(bin)/cols 1|$(bin)/filter_out_ci 0 $(coreMap) >$@
346
	$(bin)/autoremove $@
347 4601 aaronmk
348 7484 aaronmk
%/new_terms.csv: %/map.csv $(vocab) $(thesaurus) %/unmapped_terms.csv
349 4857 aaronmk
	$(newTerms)
350
	$(bin)/autoremove $@
351
newTerms = tail -n +2 $<|$(bin)/filter_out_ci 0 $(vocab)|$(bin)/filter_out_ci 0\
352 7484 aaronmk
$(thesaurus) $(if $(wildcard $(word 4,$+)),|$(bin)/filter_out_ci 0 $(word 4,$+))\
353 7216 aaronmk
|grep -vE -e '^"?:' -e 'UNUSED' >$@; exit 0 # grep exits nonzero if no match
354 4601 aaronmk
355 6157 aaronmk
termsSubdirs := $(importTables)
356 4600 aaronmk
357 3764 aaronmk
include $(root)/lib/mappings.Makefile
358 1742 aaronmk
359 1509 aaronmk
##### External dependencies
360
361
$(root)/%: _always
362
	+$(subMake)
363 1628 aaronmk
.PRECIOUS: $(root)/% # let ext. dir's Makefile decide whether to delete on error
364 1509 aaronmk
365 783 aaronmk
##### Mapping
366 368 aaronmk
367 4182 aaronmk
+maps = $(filter %/map.csv %/VegBIEN.csv $(mappings)/%,$(+_))
368 4746 aaronmk
map2db = env in_database=vegbien in_schema=$(datasrc) in_table=$*\
369 4748 aaronmk
out_database=vegbien $(root)/map $(+maps)
370 368 aaronmk
371 783 aaronmk
##### Import to VegBIEN
372 772 aaronmk
373 7281 aaronmk
import: $(importTables:%=%/import) _always ;
374
375
rm: _always
376
	echo "DELETE FROM source WHERE shortname = '$(datasrc)';"\
377
|"time" $(psqlAsBien)
378
379 7284 aaronmk
reimport: _always rm import ;
380
381 1853 aaronmk
profileTest = $(if $(profile),$(if $(test),1))
382
profileOnly = -env profile_to=/dev/fd/3 $(map2db) 3>&1 1>&2|\
383
$(bin)/profile_stats /dev/fd/0
384
385 5457 aaronmk
log_ = $*/logs/$(if $(n),n=$(n).,)$(version).log.sql
386 7075 aaronmk
387
# Displays the import log file path
388 7076 aaronmk
# Run with `make -s` to avoid echoing make commands
389 7075 aaronmk
%/log_file: _always
390
	echo $$(pwd)/$(log_)
391
392 3317 aaronmk
trace = $(log_:.log.sql=.trace)
393 7080 aaronmk
import = -$(if $(profileTest),$(profileOnly),(set -x; date; "time" env commit=1\
394 7079 aaronmk
$(if $(profile),profile_to=$(trace)) $(map2db))$(if $(log), >>$(log_) 2>&1))
395 1088 aaronmk
# don't abort on import errors, which often relate to invalid input data
396 624 aaronmk
397 7245 aaronmk
import? = $(if $(call and,$(full_import),$(call dontImport,.)),,$(import))
398 7047 aaronmk
399 4252 aaronmk
%/import: %/VegBIEN.csv _always
400 7047 aaronmk
	$(import?)
401 772 aaronmk
# default:
402 4252 aaronmk
%/import: _always ;
403 718 aaronmk
404 7279 aaronmk
#### Taxonomic scrubbing
405
406 7282 aaronmk
scrub: _always
407 7275 aaronmk
	$(selfMake) $(root)/scrub & echo $$!
408 7265 aaronmk
409 7279 aaronmk
# Removes TNRS taxondeterminations
410
unscrub: _always
411
	echo "SELECT delete_scrubbed_taxondeterminations('$(datasrc)');"|\
412
"time" env no_query_results=1 $(psqlAsBien)
413
414 7283 aaronmk
rescrub: _always unscrub scrub ;
415
416 7282 aaronmk
import_scrub: _always import scrub ;
417
418 7285 aaronmk
reimport_scrub: _always reimport scrub ;
419
420 790 aaronmk
##### Log files from import
421
422 4182 aaronmk
logs := $(wildcard */logs/*.log.sql */logs/*.trace)
423 339 aaronmk
424 383 aaronmk
rm_logs: _always
425 339 aaronmk
	$(RM) $(logs)
426
427 783 aaronmk
##### Verification of import
428 264 aaronmk
429 5782 aaronmk
verifyTables := $(patsubst verify/%.ref,%,$(wildcard verify/*.ref))
430 1194 aaronmk
431 5782 aaronmk
verify: $(verifyTables:%=%/verify) _always ;
432
433 4252 aaronmk
%/verify: verify/%.ref verify/%.out _always
434 1184 aaronmk
	-$(diffVerbose) $(+_)
435 1088 aaronmk
# don't abort on verification errors, which are expected during development
436 1194 aaronmk
# default:
437 4252 aaronmk
%/verify: verify/%.out _always
438 1200 aaronmk
	$(if $(shell test -e $< && echo t),cat $<)
439 1199 aaronmk
# don't run if verify/%.out's default do-nothing action was used
440 1200 aaronmk
# can't use $(wildcard) because it won't recheck file after verify/%.out is run
441 368 aaronmk
442 6186 aaronmk
psqlExport := "time" $(psqlNoSearchPath) --no-align --field-separator=$$'\t'\
443 4401 aaronmk
--pset=footer=off --pset=null=NULL
444 5976 aaronmk
# Note that using $(inDatasrc) will not work with datasources whose tables are
445
# the same name as VegBIEN tables (likely only VegBank), because the datasource
446
# is first in the search_path.
447
verify = $(if $(reverify),($(inDatasrc); cat $<)|$(psqlExport)\
448
--set=datasource="'$(datasrc)'" >$@)
449 514 aaronmk
450 1199 aaronmk
verify/%.out: $(mappings)/verify.%.sql _always
451 1192 aaronmk
	$(verify)
452 5973 aaronmk
.PRECIOUS: verify/%.out # save partial output in case of error to help debugging
453 1194 aaronmk
# default:
454 1199 aaronmk
verify/%.out: _always ;
455 369 aaronmk
456 1199 aaronmk
all += $(wildcard verify/*.out)
457 1192 aaronmk
458 4402 aaronmk
%.ref: %.ref.sql
459 4409 aaronmk
	($(inDatasrc); cat $<)|$(psqlExport) >$@
460 5981 aaronmk
.PRECIOUS: %.ref # there must always be a .ref for the make rules to work
461 4402 aaronmk
462 1667 aaronmk
##### Editing import
463
464 6984 aaronmk
rename/%: _always
465
	echo "UPDATE source SET shortname = '$*' WHERE shortname = '$(datasrc)';"\
466
|$(psqlAsBien)
467 1667 aaronmk
468 783 aaronmk
##### Testing
469 368 aaronmk
470 4274 aaronmk
testRefOutput = $(subst .by_col,,$(1))
471 1986 aaronmk
testRef = $(testRefOutput).ref
472
hasOwnRef = $(filter $@,$(call testRefOutput,$@))
473
# filter returns non-empty if they are equal
474 630 aaronmk
475 4246 aaronmk
# `rm $@`: Remove outputs of successful tests to reduce clutter
476 4272 aaronmk
# `$(foreach use_staged...)`: Run with use_staged=1
477 897 aaronmk
define runTest
478 776 aaronmk
@echo "Testing $(abspath $@)..."
479 4272 aaronmk
>$@ env test=1 n=$(test_n) $(1) $(foreach use_staged,1,$(map2db))
480 6446 aaronmk
$(if $(hasOwnRef),,-)@(set -x; $(diffIgnoreSpace) $(call testRef,$@) $@) 2>&1\
481
&& rm $@ || { e=$$?; $(if $(wildcard $(call testRef,$@)),,cat $@;)\
482 777 aaronmk
$(if $(hasOwnRef),\
483 3179 aaronmk
{\
484
read -p $(emph)'Accept new test output? (y/n)'$(endEmph) REPLY;\
485
if test "$$REPLY" = y; then\
486
(set -x; $(MAKE) $@-ok --directory=$(realpath .) --makefile=../input.Makefile);\
487
exit 0;\
488
fi;\
489
};,\
490 777 aaronmk
echo $(emph)"Note: The preceding failed test is compared to another test's\
491
output"$(endEmph);\
492
echo $(emph)"When it fails, this always indicates a bug"$(endEmph);\
493
)\
494 652 aaronmk
exit $$e;}
495 628 aaronmk
endef
496
497 876 aaronmk
tests :=
498
499 6744 aaronmk
%/test: %/test.xml _always ;
500
501 4269 aaronmk
# Requires staging tables. To create them, run `make inputs/<datasrc>/install`.
502
# Non-flat-file inputs fall back to mimicking a successful test
503 4182 aaronmk
%/test.xml: %/VegBIEN.csv _always
504 4452 aaronmk
	$(if $(nonXml),$(call runTest,by_col=))
505 4182 aaronmk
tests += %/test.xml
506 627 aaronmk
507 4274 aaronmk
%/test.by_col.xml: %/VegBIEN.csv _always
508 4831 aaronmk
	$(if $(nonXml),$(call runTest,by_col=1))
509 4275 aaronmk
510
# Only run column-based tests if column-based mode enabled, because these tests
511
# are much slower than the row-based tests for small numbers of rows
512
ifneq ($(by_col),)
513 4274 aaronmk
tests += %/test.by_col.xml
514 4275 aaronmk
endif
515 4274 aaronmk
516 6157 aaronmk
testOutputs := $(foreach test,$(tests),$(importTables:%=$(test)))
517 724 aaronmk
518 1361 aaronmk
.PRECIOUS: $(testOutputs) # save outputs of failed tests so they can be accepted
519
520 876 aaronmk
test: _always $(testOutputs) ;
521
522 4182 aaronmk
all += $(wildcard %/test*.xml)
523 876 aaronmk
524 634 aaronmk
# Accepts a test output: make <test_output_path>-ok
525 626 aaronmk
%-ok: _always
526 4383 aaronmk
	mv $* $(call testRef,$*)
527 502 aaronmk
528 4120 aaronmk
accept-all: _always
529 3579 aaronmk
	+yes|$(selfMake) test
530
531 3133 aaronmk
##### Documentation
532
533 4494 aaronmk
steps = $(selfMake) -s $*/import test=1 by_col=1 verbosity=2 n=100\
534 3385 aaronmk
2>&1|$(bin)/debug2redmine >$@
535 3188 aaronmk
536 4182 aaronmk
%/logs/steps.by_col.log.sql: _always
537 3188 aaronmk
	+$(steps)