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