Project

General

Profile

1
selfDir_uZPPqC := $(dir $(lastword $(MAKEFILE_LIST)))
2
root := $(selfDir_uZPPqC)..
3
include $(root)/lib/common.Makefile
4

    
5

    
6
##### Configuration
7

    
8
# Command line
9
continue ?=
10
debug ?=
11
full_import ?=
12
import_source ?= 1
13
log ?= $(if $(test),,1)
14
noclobber ?=
15
profile ?=
16
quiet ?=
17
reverify ?= 1
18
schema_only ?=
19
use_staged ?= $(by_col)
20

    
21
# Makefile
22
exts ?= csv tsv tab txt dat dmp xml
23
test_n ?= 2
24

    
25
##### Vars/functions
26

    
27
# Paths
28
datasrc := $(patsubst .%,%,$(notdir $(realpath .)))
29
bin := $(root)/bin
30
mappings := $(root)/mappings
31

    
32
# Make
33
SHELL := /bin/bash
34
selfMake = $(MAKE) --makefile=../input.Makefile
35
subMake = $(MAKE) $(@:$(root)/%=%) --directory=$(root)
36
+_ = $(+:_%=)
37
# used to filter the output of embedded $(shell make ...) invocations
38
filter_make := grep -vF -e lib -e ".Makefile'."
39
addBeforeExt = $(basename $(2))$(1)$(suffix $(2))
40

    
41
# Terminal
42
termCols := $(shell tput cols)
43

    
44
# Commands
45
MKDIR = mkdir -p
46
mkdir = $(MKDIR) $(@D)
47
diff = diff --unified=2
48
diffIgnoreSpace = $(diff) --ignore-space-change
49
diffVerbose = $(if $(verbose),diff --side-by-side --left-column\
50
--width=$(termCols),$(diff))
51

    
52
# BIEN commands
53
sortFilenames = $(shell $(bin)/sort_filenames $(1))
54
selfMap = $(bin)/cols 0 0
55
psqlAsBien := $(bin)/psql_verbose_vegbien
56
psqlNoSearchPath := env no_search_path=1 $(psqlAsBien)
57
# Usage: ($(inDatasrc); cat $(file))|$(psqlCmd)
58
inDatasrc := echo 'SET search_path TO "$(datasrc)";'
59

    
60
# SVN
61
setSvnIgnore = svn propset svn:ignore $(2) $(1)
62
define addDirWithIgnore
63
$(addDir)
64
$(setSvnIgnore)
65
endef
66

    
67
##### Environment
68

    
69
export PATH := $(bin):$(PATH)
70

    
71
##### General targets
72

    
73
all: _always maps ;
74

    
75
clean: _always
76
	$(RM) $(all)
77

    
78
remake: _always clean
79
	+$(selfMake)
80
# re-run make so that cache of existing files is reset
81

    
82
# Only remake if doesn't exist. This prevents unintentional remaking when the
83
# make script is newly checked out from svn (which sets the mod time to now) but
84
# the output is synced externally.
85
# Can't remove prereq to do this, because it determines when the rule applies.
86
make_script = $(if $(wildcard $@),,"time" ./$< >$@)
87

    
88
%/: %/map.csv _always ;
89

    
90
%/: % _always ;
91

    
92
%: %.make
93
	$(make_script)
94
.PRECIOUS: % # save partial outputs of aborted src make scripts
95

    
96
##### Tables discovery
97

    
98
sortFile := import_order.txt
99
noImportFile := _no_import
100

    
101
dontImport = $(wildcard $(noImportFile))$(wildcard $(1)/$(noImportFile))$(if\
102
$(import_source),,$(filter Source,$(1)))
103

    
104
ifeq ($(sort_file_updated),)# keep $(sortFile) up-to-date
105
$(shell sort_file_updated=1 $(selfMake) $(sortFile)|$(filter_make) >&2)
106
export sort_file_updated=1
107
endif
108

    
109
sort_file_tables := $(if $(wildcard $(sortFile)),$(shell cat $(sortFile)))
110
    # $(shell) replaces "\n" with " "
111
tables := $(sort_file_tables)
112
allSubdirs := $(call wildcard/,*/)
113
allTables := $(call sortFilenames,$(filter-out _% verify logs,$(allSubdirs:%/=%)))
114
joinedTables := $(filter-out $(tables),$(allTables))
115
allTables := $(strip $(joinedTables) $(tables))# move joined tables to beginning
116
has_visible_files = $(call wildcard/,$(1)/*)
117
allTables := $(foreach table,$(allTables),$(if\
118
$(call has_visible_files,$(table)),$(table)))
119
ifeq ($(tables),)# none specified in sort file
120
tables := $(allTables)
121
endif
122
importTables := $(foreach table,$(tables),$(if\
123
$(call dontImport,$(table)),,$(table)))
124

    
125
list_tables: _always # use `make -s` to avoid echoing commands
126
	@for table in $(tables); do echo "$$table"; done
127

    
128
$(sortFile): _always
129
	# add any missing tables to $(sortFile)
130
	$(if $(filter-out $(sort_file_tables),$(tables)),$(selfMake) -s list_tables >$@)
131

    
132
##### SVN
133

    
134
svnFilesGlob:= */{,$(noImportFile),{,.}{data,map,VegBIEN}.csv{,.*},*header.*,*.sql,test.xml.ref}
135
svnFilesGlob := {map.csv,*{schema,grants,~}*.sql,{,*/}{*.make,*terms.csv},$(svnFilesGlob)}
136
_svnFilesGlob := {_MySQL/{,*schema*.sql,*.make},{,*/}{*{run,Makefile},*.{md5,url,pdf},*README.TXT}}
137
svnFiles = $(filter-out _% logs/% %.data.sql,$(call wildcard/,$(svnFilesGlob)))\
138
$(call wildcard/,$(_svnFilesGlob))
139

    
140
add: _always $(if $(call dontImport,.),,add!) ;
141

    
142
# To update all inputs with these settings: make inputs/add
143
add!: _always Source/add $(allTables:%=%/add)
144
	$(call setSvnIgnore,.,'*')
145
	$(call addDirWithIgnore,logs,$$'*.gz\n*.log.sql\n*.trace')
146
	$(call addDirWithIgnore,verify,$$'*.out\n*.csv\n*.xls\n*.xlsx')
147
	$(call addFile,import_order.txt)
148
	$(if $(wildcard _MySQL/),$(call addDirWithIgnore,_MySQL,'*'))
149
	$(if $(wildcard _src/),$(call addDirWithIgnore,_src,'*'))
150
	$(if $(wildcard _archive/),$(call addDirWithIgnore,_archive,'*'))
151
	$(call add*,$(svnFiles))
152

    
153
# Adds a new table subdir
154
%/add: _always
155
	$(call addDirWithIgnore,$*,'*')
156
	$(call addDirWithIgnore,$*/logs,$$'*.gz\n*.log.sql\n*.trace')
157

    
158
##### Existing maps discovery
159

    
160
anyMap := %/map.csv %/VegBIEN.csv %/unmapped_terms.csv %/new_terms.csv
161

    
162
exts := $(call ci,$(exts))
163
extsFilter := $(addprefix %.,$(exts))
164
dataOnly = $(filter $(extsFilter),$(1))
165

    
166
anyTest = $*/test.%
167
srcsOnly = $(filter-out $(anyMap) $(anyTest) %/logs,$(call dataOnly,$(1)))
168

    
169
srcDict := map.csv
170

    
171
vocab := $(mappings)/VegCore.vocab.csv
172
thesaurus := $(mappings)/VegCore.thesaurus.csv
173
coreMap := $(mappings)/VegCore-VegBIEN.csv
174

    
175
viaMaps := $(tables:%=%/map.csv)
176

    
177
autogenMaps := $(subst map.,VegBIEN.,$(viaMaps))
178
directMaps := $(autogenMaps) $(filter-out $(autogenMaps),\
179
$(wildcard */VegBIEN.csv))
180

    
181
##### Sources
182

    
183
srcs = $(call sortFilenames,$(call srcsOnly,$(wildcard $*/*)))
184
nonHeaderSrcs = $(filter-out %/header.csv,$(srcs))
185
isRef = $(if $(nonHeaderSrcs),,1)
186
    # empty subdir, so references an already-installed staging table
187
isXml = $(filter %.xml,$(nonHeaderSrcs))
188
nonXml = $(if $(isXml),,1)
189
isCsv = $(if $(nonHeaderSrcs),$(if $(isXml),,1))
190
    # true if $(srcs) non-empty and contains no *.xml
191
catSrcs = $(bin)/cat$(if $(nonXml),_csv) $(nonHeaderSrcs)
192
withCatSrcs = $(catSrcs:$(bin)/%=$(bin)/with_%) --
193

    
194
# Run with `make -s` to avoid echoing make commands
195
cat: $(importTables:%=%/cat) _always ;
196

    
197
%/cat: _always
198
	$(catSrcs)
199

    
200
##### Input data retrieval
201

    
202
# Must come before `%.sql: _MySQL/%.sql` to override it
203
%.sql: %.sql.make
204
	$(make_script)
205

    
206
# The export must be created with:
207
# `--compatible=postgresql --add-locks=false --set-charset --no-create-info`
208
# Must come before `%.sql: _MySQL/%.sql` to override it
209
%.data.sql: _MySQL/%.data.sql
210
	$(if $(wildcard $@),,$(bin)/my2pg.data <$< >$@)
211

    
212
# The export must be created with:
213
# `--compatible=postgresql --add-locks=false --set-charset`
214
# Add `--no-data` to create a schema-only export.
215
%.sql: _MySQL/%.sql
216
	$(if $(wildcard $@),,$(bin)/my2pg <$< >$@)
217

    
218
##### Staging tables installation
219

    
220
srcTable := %.src
221

    
222
dbExportsWildcard = $(sort $(patsubst _MySQL/%.make,%,$(wildcard\
223
$(1) _MySQL/$(1).make)))
224

    
225
dbExports := $(call dbExportsWildcard,*schema*.sql)# schemas first
226
ifeq ($(schema_only),) # add rest of .sql files
227
dbExports += $(filter-out $(dbExports),$(call dbExportsWildcard,*.sql))
228
endif
229
dbExports := $(filter-out grants.sql,$(dbExports))
230
dbExports := $(strip $(dbExports))# += adds extra whitespace
231
allInstalls := $(if $(dbExports),sql) $(allTables)
232

    
233
install: _always schema $(allInstalls:%=%/install) ;
234

    
235
uninstall: _always confirm_rm_schema rm_schema ;
236
# rm_schema will also drop all staging tables
237

    
238
reinstall: _always uninstall install ;
239

    
240
confirm_rm_schema: _always
241
	$(if $(filter TNRS,$(datasrc)),$(call confirm,WARNING: This will delete the\
242
TNRS cache!,To save it: make backups/TNRS.backup-remake))
243

    
244
schema: _always
245
	-echo 'CREATE SCHEMA "$(datasrc)";'|$(psqlNoSearchPath)
246
# ignore errors if schema exists
247

    
248
rm_schema: _always
249
	echo 'DROP SCHEMA IF EXISTS "$(datasrc)" CASCADE;'|$(psqlNoSearchPath)
250

    
251
installLog := logs/install.log.sql
252

    
253
log_dir = $(1)logs
254
has_log_dir = $(call and,$(wildcard $(log_dir)),$(call not,$(noclobber)))
255
logInstall = $(if $(has_log_dir),$(if $(quiet),$(2)$(1)$(installLog)\
256
2>&1,2>&1|tee $(3) $(1)$(installLog)))
257
logInstallRoot = $(call logInstall,,>)
258
logInstall* = $(call logInstall,$*/,>)
259
logInstall*Add = $(call logInstall,$*/,>>,-a)# append to log
260

    
261
# Must come before %/install to override it
262
sql/install: $(dbExports)
263
	($(inDatasrc); cat $+|pg_dump_limit; $(if $(wildcard\
264
grants.sql),cat grants.sql))|"time" env no_search_path=1 \
265
$(bin)/psql_$(if $(debug),verbose,script)_vegbien --set=schema='"$(datasrc)"' \
266
$(logInstallRoot)
267

    
268
# $debug option runs the *.sql import verbosely, to display which statements are
269
# being run. this should only be used for SQL files that use COPY FROM to import
270
# data, to avoid echoing pages of insert statements.
271
cleanup = $(if $(wildcard $*/cleanup.sql),($(inDatasrc); cat $*/cleanup.sql)\
272
|"time" $(psqlNoSearchPath) --echo-all --set=table='"$*"' $(logInstall*Add),\
273
(export schema=$(datasrc) table=$*; . $(bin)/vegbien_dest; unset schemas; \
274
$(bin)/csv2db) $(logInstall*Add))
275

    
276
%/header.csv:
277
	echo 'COPY (SELECT * FROM "$(datasrc)"."$*" LIMIT 0) TO STDOUT CSV HEADER;'|\
278
env no_search_path=1 $(bin)/psql_script_vegbien >$*/header.csv
279

    
280
exportHeader = $(selfMake) $*/header.csv
281

    
282
# Don't try to edit a view. Must come before %/install to override it.
283
%_view/install: _always ;
284

    
285
%.sql/run: _always
286
	$(if $(wildcard $(@D)),($(inDatasrc); cat $(@D))|(cd '$(*D)';\
287
"time" env no_search_path=1 ../$(bin)/psql_verbose_vegbien \
288
--set=table='"$(*D)"' --set=table_str=\''"$(*D)"'\'))
289

    
290
%/postprocess.sql: %/map.csv
291
	$(if $(wildcard $*/run),$(bin)/in_place $@ env text=1 $(bin)/repl $<)
292

    
293
%/postprocess: _always
294
	$(if $(wildcard $*/run),,$(selfMake) $*/postprocess.sql/run)
295

    
296
%/map_table: _always
297
	$(if $(wildcard $*/run),$*/run map_table)
298

    
299
# For staging tables which are derived by joining together other staging tables.
300
%/install %/header.csv: %/create.sql _always
301
	($(inDatasrc); echo 'CREATE TABLE "$*" AS'; cat $<; echo ';')|"time" \
302
$(psqlNoSearchPath) --echo-all --set=schema='"$(datasrc)"' --set=table='"$*"' \
303
$(logInstall*)
304
	$(selfMake) $*/postprocess
305
	$(exportHeader)
306
	$(cleanup)
307
	$(selfMake) $*/map_table
308
ifneq ($(noclobber),)
309
.PRECIOUS: %/header.csv
310
endif
311

    
312
%/install: _always
313
	$(if $(isCsv),$(import_install_))
314
	$(selfMake) $*/postprocess
315
	-$(exportHeader)
316
	-$(if $(isCsv),,$(cleanup))
317
	$(selfMake) $*/map_table
318
# ignore errors if table does not exist (non-data dir)
319
define import_install_
320
(. $(bin)/vegbien_dest; unset schemas; "time" nice -n +5\
321
env schema=$(datasrc) table=$* $(bin)/csv2db $(catSrcs) $(logInstall*))
322
$(if $(filter $(srcTable),$*),($(inDatasrc);\
323
echo 'ALTER TABLE "$(datasrc)"."$*" RENAME row_num TO "$*.row_num";')|"time"\
324
$(psqlNoSearchPath) --echo-all --set=table='"$*"' $(logInstall*Add))
325
endef
326
# table-scope src table's row_num col to allow joining it with other tables
327

    
328
%/uninstall: _always
329
	echo 'DROP TABLE IF EXISTS "$(datasrc)"."$*" CASCADE;'|$(psqlNoSearchPath)
330

    
331
%/reinstall: _always %/uninstall %/install ;
332

    
333
postprocess: _always $(allTables:%=%/postprocess) ;
334

    
335
cleanup: _always $(tables:%=%/cleanup) ;
336

    
337
# WARNING: This removes any index comments, due to a PostgreSQL bug.
338
# This occurs because ALTER TABLE recreates the index but not its comment.
339
%/cleanup: _always
340
	$(cleanup)
341

    
342
##### Maps building
343

    
344
# Maps to (try to) build are added to this
345
maps :=
346

    
347
srcRoot = $(mappings)/root.sh
348
mkSrcMap = (. $(srcRoot); env datasrc=$(datasrc) $(bin)/src_map <$*/header.csv\
349
>$@)
350

    
351
translate = $(if $(wildcard $(1)),$(bin)/in_place $< $(bin)/translate_ci 1 $(1))
352

    
353
$(srcDict):# empty target in case it doesn't exist
354

    
355
# Via maps cleanup
356
ifneq ($(filter %/.map.csv.last_cleanup,$(MAKECMDGOALS)),)
357
%/.map.csv.last_cleanup: %/map.csv $(vocab) $(thesaurus) $(coreMap) $(srcDict)
358
	$(call translate,$(srcDict))
359
	$(bin)/in_place $< $(bin)/canon 1 $(vocab)
360
	$(call translate,$(thesaurus))
361
	$(bin)/in_place $< $(bin)/fix_line_endings
362
	touch $@
363
	+$(selfMake) $(<:%/map.csv=%/unmapped_terms.csv)
364
	+$(selfMake) $(<:%/map.csv=%/new_terms.csv)
365
.PRECIOUS: %/.map.csv.last_cleanup
366
else
367
%/map.csv: _always
368
	$(if $(wildcard $@),,$(mk_map_csv))
369
	+$(selfMake) $(@:%/map.csv=%/.map.csv.last_cleanup)
370
define mk_map_csv
371
+$(selfMake) $*/header.csv
372
$(if $(nonXml),$(mkSrcMap))
373
endef
374
endif
375

    
376
%/VegBIEN.csv: %/map.csv $(coreMap)
377
	$(if $(wildcard $*/run),-ln -s ../$(coreMap) $@\
378
,<$< $(bin)/cat_cols 1 2|$(bin)/join $(coreMap)|$(bin)/sort_map >$@)
379
# ignore errors if symlink exists
380
maps += $(autogenMaps)
381

    
382
maps: $(maps) _always ;
383

    
384
all += $(maps)
385

    
386
##### Maps validation
387

    
388
# `tail -n +2`: Remove header before running filter_out_ci because filter_out_ci
389
# only removes the header if it matches the vocabulary's header.
390

    
391
%/unmapped_terms.csv: %/map.csv $(coreMap)
392
	tail -n +2 $<|$(bin)/cols 1|$(bin)/filter_out_ci 0 $(coreMap) >$@
393
	$(bin)/autoremove $@
394

    
395
%/new_terms.csv: %/map.csv $(vocab) $(thesaurus) %/unmapped_terms.csv
396
	$(newTerms)
397
	$(bin)/autoremove $@
398
newTerms = tail -n +2 $<|$(bin)/filter_out_ci 0 $(vocab)|$(bin)/filter_out_ci 0\
399
$(thesaurus) $(if $(wildcard $(word 4,$+)),|$(bin)/filter_out_ci 0 $(word 4,$+))\
400
|grep -vE -e '^"?:' -e 'UNUSED' >$@; exit 0 # grep exits nonzero if no match
401

    
402
termsSubdirs := $(tables)
403

    
404
include $(root)/lib/mappings.Makefile
405

    
406
##### External dependencies
407

    
408
$(root)/%: _always
409
	+$(subMake)
410
.PRECIOUS: $(root)/% # let ext. dir's Makefile decide whether to delete on error
411

    
412
##### Mapping
413

    
414
+maps = $(filter %/map.csv %/VegBIEN.csv $(mappings)/%,$(+_))
415
map2db = env in_database=vegbien in_schema=$(datasrc) in_table=$*\
416
out_database=vegbien $(root)/map $(+maps)
417

    
418
##### Import to VegBIEN
419

    
420
import: $(importTables:%=%/import) _always ;
421

    
422
rm: _always
423
	echo "DELETE FROM source WHERE shortname = '$(datasrc)';"\
424
|"time" $(psqlAsBien)
425

    
426
reimport: _always rm import ;
427

    
428
profileTest = $(if $(profile),$(if $(test),1))
429
profileOnly = -env profile_to=/dev/fd/3 $(map2db) 3>&1 1>&2|\
430
$(bin)/profile_stats /dev/fd/0
431

    
432
log_ = $*/logs/$(if $(n),n=$(n).,)$(version).log.sql
433

    
434
# Displays the import log file path
435
# Run with `make -s` to avoid echoing make commands
436
%/log_file: _always
437
	echo $$(pwd)/$(log_)
438

    
439
trace = $(log_:.log.sql=.trace)
440
restart_row = $(shell set -x; grep -F Partition: $(log_)|tail -1|$(sed)\
441
's/^.* rows ([[:digit:]]+)-.*$$/\1/')
442
import = -$(if $(profileTest),$(profileOnly),(set -x; date; "time" env commit=1\
443
$(if $(profile),profile_to=$(trace)) $(if $(continue),start=$(restart_row))\
444
$(map2db))$(if $(log), >>$(log_) 2>&1))
445
# don't abort on import errors, which often relate to invalid input data
446

    
447
import? = $(if $(call and,$(full_import),$(call dontImport,.)),,$(import))
448

    
449
%/import: %/VegBIEN.csv _always
450
	$(import?)
451
# default:
452
%/import: _always ;
453

    
454
#### Taxonomic scrubbing
455

    
456
scrub: _always
457
	$(selfMake) $(root)/scrub & echo $$!
458
# using & (background process) ignores TNRS errors, so that TNRS bugs do not
459
# prevent the remaining tables from being imported even if TNRS can't be run
460

    
461
# Removes TNRS taxondeterminations
462
unscrub: _always
463
	echo "SELECT delete_scrubbed_taxondeterminations('$(datasrc)');"|\
464
"time" env no_query_results=1 $(psqlAsBien)
465

    
466
rescrub: _always unscrub scrub ;
467

    
468
import_scrub: _always import scrub ;
469

    
470
reimport_scrub: _always reimport scrub ;
471

    
472
%/import_scrub: _always %/import scrub ;
473

    
474
##### Log files from import
475

    
476
logs := $(wildcard */logs/*.log.sql */logs/*.trace)
477

    
478
rm_logs: _always
479
	$(RM) $(logs)
480

    
481
##### Verification of import
482

    
483
verifyTables := $(patsubst verify/%.ref,%,$(wildcard verify/*.ref))
484

    
485
verify: $(verifyTables:%=%/verify) _always ;
486

    
487
%/verify: verify/%.ref verify/%.out _always
488
	-$(diffVerbose) $(+_)
489
# don't abort on verification errors, which are expected during development
490
# default:
491
%/verify: verify/%.out _always
492
	$(if $(shell test -e $< && echo t),cat $<)
493
# don't run if verify/%.out's default do-nothing action was used
494
# can't use $(wildcard) because it won't recheck file after verify/%.out is run
495

    
496
psqlExport := "time" $(psqlNoSearchPath) --no-align --field-separator=$$'\t'\
497
--pset=footer=off --pset=null=NULL
498
# Note that using $(inDatasrc) will not work with datasources whose tables are
499
# the same name as VegBIEN tables (likely only VegBank), because the datasource
500
# is first in the search_path.
501
verify = $(if $(reverify),($(inDatasrc); cat $<)|$(psqlExport)\
502
--set=datasource="'$(datasrc)'" >$@)
503

    
504
verify/%.out: $(mappings)/verify.%.sql _always
505
	$(verify)
506
.PRECIOUS: verify/%.out # save partial output in case of error to help debugging
507
# default:
508
verify/%.out: _always ;
509

    
510
all += $(wildcard verify/*.out)
511

    
512
%.ref: %.ref.sql
513
	($(inDatasrc); cat $<)|$(psqlExport) >$@
514
.PRECIOUS: %.ref # there must always be a .ref for the make rules to work
515

    
516
##### Editing import
517

    
518
rename/%: _always
519
	echo "UPDATE source SET shortname = '$*' WHERE shortname = '$(datasrc)';"\
520
|$(psqlAsBien)
521

    
522
##### Testing
523

    
524
testRefOutput = $(subst .by_col,,$(1))
525
testRef = $(testRefOutput).ref
526
hasOwnRef = $(filter $@,$(call testRefOutput,$@))
527
# filter returns non-empty if they are equal
528

    
529
# `rm $@`: Remove outputs of successful tests to reduce clutter
530
# `$(foreach use_staged...)`: Run with use_staged=1
531
define runTest
532
@echo "Testing $(abspath $@)..."
533
>$@ env test=1 n=$(test_n) $(1) $(foreach use_staged,1,$(map2db))
534
$(if $(hasOwnRef),,-)@(set -x; $(diffIgnoreSpace) $(call testRef,$@) $@) 2>&1\
535
&& rm $@ || { e=$$?; $(if $(wildcard $(call testRef,$@)),,cat $@;)\
536
$(if $(hasOwnRef),\
537
{\
538
read -p $(emph)'Accept new test output? (y/n)'$(endEmph) REPLY;\
539
if test "$$REPLY" = y; then\
540
(set -x; $(MAKE) $@-ok --directory=$(realpath .) --makefile=../input.Makefile);\
541
exit 0;\
542
fi;\
543
};,\
544
echo $(emph)"Note: The preceding failed test is compared to another test's\
545
output"$(endEmph);\
546
echo $(emph)"When it fails, this always indicates a bug"$(endEmph);\
547
)\
548
exit $$e;}
549
endef
550

    
551
tests :=
552

    
553
%/test: %/test.xml _always ;
554

    
555
# Requires staging tables. To create them, run `make inputs/<datasrc>/install`.
556
# Non-flat-file inputs fall back to mimicking a successful test
557
%/test.xml: %/VegBIEN.csv _always
558
	$(if $(nonXml),$(call runTest,by_col=))
559
tests += %/test.xml
560

    
561
%/test.by_col.xml: %/VegBIEN.csv _always
562
	$(if $(nonXml),$(call runTest,by_col=1))
563

    
564
# Only run column-based tests if column-based mode enabled, because these tests
565
# are much slower than the row-based tests for small numbers of rows
566
ifneq ($(by_col),)
567
tests += %/test.by_col.xml
568
endif
569

    
570
testOutputs := $(foreach test,$(tests),$(tables:%=$(test)))
571

    
572
.PRECIOUS: $(testOutputs) # save outputs of failed tests so they can be accepted
573

    
574
test: _always $(testOutputs) ;
575

    
576
all += $(wildcard %/test*.xml)
577

    
578
# Accepts a test output: make <test_output_path>-ok
579
%-ok: _always
580
	mv $* $(call testRef,$*)
581

    
582
accept-all: _always
583
	+yes|$(selfMake) test
584

    
585
##### Documentation
586

    
587
steps = $(selfMake) -s $*/import test=1 by_col=1 verbosity=2 n=100\
588
2>&1|$(bin)/debug2redmine >$@
589

    
590
%/logs/steps.by_col.log.sql: _always
591
	+$(steps)
(9-9/10)