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