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