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 7047 aaronmk
public_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 5427 aaronmk
exts ?= csv tsv txt 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 6244 aaronmk
psqlAsBien := $(bin)/psql_script_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 6064 aaronmk
_svnFilesGlob := {_MySQL/{,*schema*.sql,*.make},_src/*.{url,pdf}}
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 4214 aaronmk
extsFilter := $(addprefix %.,$(exts))
140
dataOnly = $(filter $(extsFilter),$(1))
141
142 4182 aaronmk
anyTest = $*/test.%
143 4214 aaronmk
srcsOnly = $(filter-out $(anyMap) $(anyTest) %/logs,$(call dataOnly,$(1)))
144 4120 aaronmk
145 6071 aaronmk
srcDict := map.csv
146
147 4844 aaronmk
vocab := $(mappings)/VegCore.csv
148 4658 aaronmk
coreMap := $(mappings)/VegCore-VegBIEN.csv
149 4687 aaronmk
dict := $(mappings)/Veg+-VegCore.csv
150 783 aaronmk
151 6157 aaronmk
viaMaps := $(importTables:%=%/map.csv)
152 783 aaronmk
153 4117 aaronmk
autogenMaps := $(subst map.,VegBIEN.,$(viaMaps))
154 728 aaronmk
directMaps := $(autogenMaps) $(filter-out $(autogenMaps),\
155 4182 aaronmk
$(wildcard */VegBIEN.csv))
156 728 aaronmk
157 1969 aaronmk
##### Sources
158
159 4182 aaronmk
srcs = $(call sortFilenames,$(call srcsOnly,$(wildcard $*/*)))
160 4450 aaronmk
nonHeaderSrcs = $(filter-out %/header.csv,$(srcs))
161
isRef = $(if $(nonHeaderSrcs),,1)
162
    # empty subdir, so references an already-installed staging table
163 4452 aaronmk
isXml = $(filter %.xml,$(nonHeaderSrcs))
164
nonXml = $(if $(isXml),,1)
165
isCsv = $(if $(nonHeaderSrcs),$(if $(isXml),,1))
166 1969 aaronmk
    # true if $(srcs) non-empty and contains no *.xml
167 4465 aaronmk
catSrcs = $(bin)/cat$(if $(nonXml),_csv) $(srcs)
168 1969 aaronmk
withCatSrcs = $(catSrcs:$(bin)/%=$(bin)/with_%) --
169
170 1986 aaronmk
# Usage: `make {--silent|-s} inputs/<datasrc>/cat` (don't echo make commands)
171 6157 aaronmk
cat: $(importTables:%=%/cat) _always ;
172 1969 aaronmk
173 4252 aaronmk
%/cat: _always
174 1969 aaronmk
	$(catSrcs)
175
176 6363 aaronmk
##### Input data retrieval
177
178
# Must come before `%.sql: _MySQL/%.sql` to override it
179
%.sql: %.sql.make
180
	$(make_script)
181
182
# The export must be created with:
183
# `--compatible=postgresql --add-locks=false --set-charset --no-create-info`
184
# Must come before `%.sql: _MySQL/%.sql` to override it
185
%.data.sql: _MySQL/%.data.sql
186
	$(if $(wildcard $@),,$(bin)/my2pg.data <$< >$@)
187
188
# The export must be created with:
189
# `--compatible=postgresql --add-locks=false --set-charset`
190
# Add `--no-data` to create a schema-only export.
191
%.sql: _MySQL/%.sql
192
	$(if $(wildcard $@),,$(bin)/my2pg <$< >$@)
193
194 4121 aaronmk
##### Staging tables installation
195 1921 aaronmk
196 5693 aaronmk
srcTable := %.src
197
198 6364 aaronmk
dbExportsWildcard = $(sort $(patsubst _MySQL/%.make,%,$(wildcard\
199
$(1) _MySQL/$(1).make)))
200
201
dbExports := $(call dbExportsWildcard,*schema*.sql)# schemas first
202
ifeq ($(schema_only),) # add rest of .sql files
203
dbExports += $(filter-out $(dbExports),$(call dbExportsWildcard,*.sql))
204 5810 aaronmk
endif
205 4438 aaronmk
dbExports := $(strip $(dbExports))# += adds extra whitespace
206 6565 aaronmk
allInstalls := $(if $(dbExports),sql) $(filter-out Source,$(allTables))
207 1921 aaronmk
208 4413 aaronmk
install: _always schema $(allInstalls:%=%/install) ;
209
210 5209 aaronmk
uninstall: _always confirm_rm_schema rm_schema ;
211 4121 aaronmk
# rm_schema will also drop all staging tables
212 1921 aaronmk
213
reinstall: _always uninstall install ;
214
215 5209 aaronmk
confirm_rm_schema: _always
216
	$(if $(filter TNRS,$(datasrc)),$(call confirm,WARNING: This will delete the\
217
TNRS cache!,To save it: make backups/TNRS.backup-remake))
218
219 4121 aaronmk
schema: _always
220 6186 aaronmk
	-echo 'CREATE SCHEMA "$(datasrc)";'|$(psqlNoSearchPath)
221 1921 aaronmk
# ignore errors if schema exists
222
223 4121 aaronmk
rm_schema: _always
224 6186 aaronmk
	echo 'DROP SCHEMA IF EXISTS "$(datasrc)" CASCADE;'|$(psqlNoSearchPath)
225 1921 aaronmk
226 5162 aaronmk
installLog := logs/install.log.sql
227 4453 aaronmk
228 5162 aaronmk
logInstall = $(if $(log),$(if $(quiet),$(2)$(1)$(installLog) 2>&1,2>&1|tee $(3)\
229
$(1)$(installLog)))
230 5692 aaronmk
logInstallRoot = $(call logInstall,,>)
231 5162 aaronmk
logInstall* = $(call logInstall,$*/,>)
232
logInstall*Add = $(call logInstall,$*/,>>,-a)# append to log
233
234 4413 aaronmk
# Must come before %/install to override it
235 4434 aaronmk
sql/install: $(dbExports)
236 6186 aaronmk
	($(inDatasrc); cat $+|pg_dump_limit)|"time" $(psqlNoSearchPath) \
237 5796 aaronmk
--set=schema='"$(datasrc)"' $(logInstallRoot)
238 4413 aaronmk
239 5795 aaronmk
cleanup = $(if $(wildcard $*/cleanup.sql),($(inDatasrc); cat $*/cleanup.sql)\
240 6186 aaronmk
|"time" $(psqlNoSearchPath) --echo-all --set=table='"$*"' $(logInstall*Add),\
241 6950 aaronmk
(. $(bin)/vegbien_dest; unset schemas; env schema=$(datasrc) table=$*\
242 6186 aaronmk
$(bin)/csv2db) $(logInstall*Add))
243 4449 aaronmk
244
define exportHeader
245
$(cleanup)
246 6186 aaronmk
echo 'SELECT * FROM "$(datasrc)"."$*" LIMIT 0;'|$(psqlNoSearchPath) \
247 4449 aaronmk
--no-align --field-separator=, --pset=footer=off >$*/header.csv
248
endef
249
250 4260 aaronmk
# For staging tables which are derived by joining together other staging tables.
251 4263 aaronmk
%/install %/header.csv: %/create.sql _always
252 4527 aaronmk
	($(inDatasrc); echo 'CREATE TABLE "$*" AS'; cat $<; echo ';')|"time" \
253 6186 aaronmk
$(psqlNoSearchPath) --echo-all --set=schema='"$(datasrc)"' --set=table='"$*"' \
254 5912 aaronmk
$(logInstall*)
255 4449 aaronmk
	$(exportHeader)
256 5441 aaronmk
.PRECIOUS: %/header.csv
257 4260 aaronmk
258 4252 aaronmk
%/install: _always
259 4456 aaronmk
	$(if $(isRef),$(exportHeader),$(if $(nonXml),$(import_install_)))
260 6078 aaronmk
	$(if $(wildcard $*/postprocess.sql),($(inDatasrc); cat $*/postprocess.sql;)\
261 6186 aaronmk
|"time" $(psqlNoSearchPath) --echo-all --set=table='"$*"' $(logInstall*Add))
262 5693 aaronmk
define import_install_
263 6950 aaronmk
(. $(bin)/vegbien_dest; unset schemas; "time" nice -n +5\
264 5028 aaronmk
env schema=$(datasrc) table=$* $(bin)/csv2db $(catSrcs) $(logInstall*))
265 5693 aaronmk
$(if $(filter $(srcTable),$*),($(inDatasrc);\
266
echo 'ALTER TABLE "$(datasrc)"."$*" RENAME row_num TO "$*.row_num";')|"time"\
267 6186 aaronmk
$(psqlNoSearchPath) --echo-all --set=table='"$*"' $(logInstall*Add))
268 5693 aaronmk
endef
269 5697 aaronmk
# table-scope src table's row_num col to allow joining it with other tables
270 1921 aaronmk
271 5819 aaronmk
%/uninstall: _always
272 6186 aaronmk
	echo 'DROP TABLE IF EXISTS "$(datasrc)"."$*" CASCADE;'|$(psqlNoSearchPath)
273 5819 aaronmk
274
%/reinstall: _always %/uninstall %/install ;
275
276 5155 aaronmk
cleanup: _always $(tables:%=%/cleanup) ;
277
278 5184 aaronmk
# WARNING: This removes any index comments, due to a PostgreSQL bug.
279
# This occurs because ALTER TABLE recreates the index but not its comment.
280 5155 aaronmk
%/cleanup: _always
281
	$(cleanup)
282
283 1777 aaronmk
##### Maps building
284
285 4646 aaronmk
# Maps to (try to) build are added to this
286 1779 aaronmk
maps :=
287
288 4208 aaronmk
srcRoot = $(mappings)/root.sh
289 3569 aaronmk
mkSrcMap = $(catSrcs)|(. $(srcRoot); env datasrc=$(datasrc) $(bin)/src_map >$@)
290 1779 aaronmk
291 6074 aaronmk
define translate
292
$(bin)/in_place $< $(bin)/canon 1 $(1)
293
$(bin)/in_place $< $(bin)/translate 1 $(1)
294
endef
295
translate? = $(if $(wildcard $(1)),$(translate))
296
297 6076 aaronmk
$(srcDict):# empty target in case it doesn't exist
298
299 3572 aaronmk
# Via maps cleanup
300 4182 aaronmk
ifneq ($(filter %/.map.csv.last_cleanup,$(MAKECMDGOALS)),)
301 6076 aaronmk
%/.map.csv.last_cleanup: %/map.csv $(vocab) $(dict) $(coreMap) $(srcDict)
302 6074 aaronmk
	$(call translate?,$(srcDict))
303 4844 aaronmk
	$(bin)/in_place $< $(bin)/canon 1 $(vocab)
304 6077 aaronmk
	$(call translate?,$(dict))
305 3572 aaronmk
	touch $@
306 4601 aaronmk
	+$(selfMake) $(<:%/map.csv=%/unmapped_terms.csv)
307
	+$(selfMake) $(<:%/map.csv=%/new_terms.csv)
308 5253 aaronmk
.PRECIOUS: %/.map.csv.last_cleanup
309 3572 aaronmk
else
310 4644 aaronmk
%/map.csv: _always
311
	$(if $(wildcard $@),,$(if $(nonXml),$(mkSrcMap)))
312 4182 aaronmk
	+$(selfMake) $(@:%/map.csv=%/.map.csv.last_cleanup)
313 5254 aaronmk
.PRECIOUS: %/map.csv
314 3572 aaronmk
endif
315
316 4641 aaronmk
%/VegBIEN.csv: %/map.csv $(coreMap)
317 4656 aaronmk
	<$< $(bin)/cat_cols 1 2|$(bin)/join $(coreMap)|$(bin)/sort_map >$@
318 1530 aaronmk
maps += $(autogenMaps)
319
320 4646 aaronmk
maps: $(maps) _always ;
321 1139 aaronmk
322
all += $(maps)
323
324 1742 aaronmk
##### Maps validation
325
326 4663 aaronmk
# `tail -n +2`: Remove header before running filter_out_ci because filter_out_ci
327
# only removes the header if it matches the vocabulary's header.
328 4601 aaronmk
329 4664 aaronmk
%/unmapped_terms.csv: %/map.csv $(coreMap)
330 4858 aaronmk
	tail -n +2 $<|$(bin)/cols 1|$(bin)/filter_out_ci 0 $(coreMap) >$@
331
	$(bin)/autoremove $@
332 4601 aaronmk
333 4844 aaronmk
%/new_terms.csv: %/map.csv $(vocab) $(dict) %/unmapped_terms.csv
334 4857 aaronmk
	$(newTerms)
335
	$(bin)/autoremove $@
336
newTerms = tail -n +2 $<|$(bin)/filter_out_ci 0 $(vocab)|$(bin)/filter_out_ci 0\
337 6826 aaronmk
$(dict) $(if $(wildcard $(word 4,$+)),|$(bin)/filter_out_ci 0 $(word 4,$+))\
338 5932 aaronmk
|grep -vE '^"?:' >$@; exit 0# because grep exits nonzero if no match
339 4601 aaronmk
340 6157 aaronmk
termsSubdirs := $(importTables)
341 4600 aaronmk
342 3764 aaronmk
include $(root)/lib/mappings.Makefile
343 1742 aaronmk
344 1509 aaronmk
##### External dependencies
345
346
$(root)/%: _always
347
	+$(subMake)
348 1628 aaronmk
.PRECIOUS: $(root)/% # let ext. dir's Makefile decide whether to delete on error
349 1509 aaronmk
350 783 aaronmk
##### Mapping
351 368 aaronmk
352 4182 aaronmk
+maps = $(filter %/map.csv %/VegBIEN.csv $(mappings)/%,$(+_))
353 4746 aaronmk
map2db = env in_database=vegbien in_schema=$(datasrc) in_table=$*\
354 4748 aaronmk
out_database=vegbien $(root)/map $(+maps)
355 368 aaronmk
356 783 aaronmk
##### Import to VegBIEN
357 772 aaronmk
358 1853 aaronmk
profileTest = $(if $(profile),$(if $(test),1))
359
profileOnly = -env profile_to=/dev/fd/3 $(map2db) 3>&1 1>&2|\
360
$(bin)/profile_stats /dev/fd/0
361
362 5457 aaronmk
log_ = $*/logs/$(if $(n),n=$(n).,)$(version).log.sql
363 3317 aaronmk
trace = $(log_:.log.sql=.trace)
364 1853 aaronmk
import = -$(if $(profileTest),$(profileOnly),(set -x; "time" env commit=1\
365 1190 aaronmk
$(if $(profile),profile_to=$(trace)) $(map2db)) $(if $(log),\
366 3616 aaronmk
$(if $(n),,&>$(log_)))$(if $(log),$(if $(n), 2>&1|tee -a $(log_))))
367 1088 aaronmk
# don't abort on import errors, which often relate to invalid input data
368 624 aaronmk
369 7047 aaronmk
import? = $(if $(call and,$(public_import),$(call dontImport,.)),,$(import))
370
371 6156 aaronmk
import: $(importTables:%=%/import) _always ;
372 718 aaronmk
373 4252 aaronmk
%/import: %/VegBIEN.csv _always
374 7047 aaronmk
	$(import?)
375 772 aaronmk
# default:
376 4252 aaronmk
%/import: _always ;
377 718 aaronmk
378 790 aaronmk
##### Log files from import
379
380 4182 aaronmk
logs := $(wildcard */logs/*.log.sql */logs/*.trace)
381 339 aaronmk
382 383 aaronmk
rm_logs: _always
383 339 aaronmk
	$(RM) $(logs)
384
385 783 aaronmk
##### Verification of import
386 264 aaronmk
387 5782 aaronmk
verifyTables := $(patsubst verify/%.ref,%,$(wildcard verify/*.ref))
388 1194 aaronmk
389 5782 aaronmk
verify: $(verifyTables:%=%/verify) _always ;
390
391 4252 aaronmk
%/verify: verify/%.ref verify/%.out _always
392 1184 aaronmk
	-$(diffVerbose) $(+_)
393 1088 aaronmk
# don't abort on verification errors, which are expected during development
394 1194 aaronmk
# default:
395 4252 aaronmk
%/verify: verify/%.out _always
396 1200 aaronmk
	$(if $(shell test -e $< && echo t),cat $<)
397 1199 aaronmk
# don't run if verify/%.out's default do-nothing action was used
398 1200 aaronmk
# can't use $(wildcard) because it won't recheck file after verify/%.out is run
399 368 aaronmk
400 6186 aaronmk
psqlExport := "time" $(psqlNoSearchPath) --no-align --field-separator=$$'\t'\
401 4401 aaronmk
--pset=footer=off --pset=null=NULL
402 5976 aaronmk
# Note that using $(inDatasrc) will not work with datasources whose tables are
403
# the same name as VegBIEN tables (likely only VegBank), because the datasource
404
# is first in the search_path.
405
verify = $(if $(reverify),($(inDatasrc); cat $<)|$(psqlExport)\
406
--set=datasource="'$(datasrc)'" >$@)
407 514 aaronmk
408 1199 aaronmk
verify/%.out: $(mappings)/verify.%.sql _always
409 1192 aaronmk
	$(verify)
410 5973 aaronmk
.PRECIOUS: verify/%.out # save partial output in case of error to help debugging
411 1194 aaronmk
# default:
412 1199 aaronmk
verify/%.out: _always ;
413 369 aaronmk
414 1199 aaronmk
all += $(wildcard verify/*.out)
415 1192 aaronmk
416 4402 aaronmk
%.ref: %.ref.sql
417 4409 aaronmk
	($(inDatasrc); cat $<)|$(psqlExport) >$@
418 5981 aaronmk
.PRECIOUS: %.ref # there must always be a .ref for the make rules to work
419 4402 aaronmk
420 1667 aaronmk
##### Editing import
421
422 6984 aaronmk
rename/%: _always
423
	echo "UPDATE source SET shortname = '$*' WHERE shortname = '$(datasrc)';"\
424
|$(psqlAsBien)
425 1667 aaronmk
426 4121 aaronmk
rm: _always
427 7058 aaronmk
	echo "DELETE FROM source WHERE shortname = '$(datasrc)';"\
428
|"time" $(psqlAsBien)
429 1667 aaronmk
430 783 aaronmk
##### Testing
431 368 aaronmk
432 4274 aaronmk
testRefOutput = $(subst .by_col,,$(1))
433 1986 aaronmk
testRef = $(testRefOutput).ref
434
hasOwnRef = $(filter $@,$(call testRefOutput,$@))
435
# filter returns non-empty if they are equal
436 630 aaronmk
437 4246 aaronmk
# `rm $@`: Remove outputs of successful tests to reduce clutter
438 4272 aaronmk
# `$(foreach use_staged...)`: Run with use_staged=1
439 897 aaronmk
define runTest
440 776 aaronmk
@echo "Testing $(abspath $@)..."
441 4272 aaronmk
>$@ env test=1 n=$(test_n) $(1) $(foreach use_staged,1,$(map2db))
442 6446 aaronmk
$(if $(hasOwnRef),,-)@(set -x; $(diffIgnoreSpace) $(call testRef,$@) $@) 2>&1\
443
&& rm $@ || { e=$$?; $(if $(wildcard $(call testRef,$@)),,cat $@;)\
444 777 aaronmk
$(if $(hasOwnRef),\
445 3179 aaronmk
{\
446
read -p $(emph)'Accept new test output? (y/n)'$(endEmph) REPLY;\
447
if test "$$REPLY" = y; then\
448
(set -x; $(MAKE) $@-ok --directory=$(realpath .) --makefile=../input.Makefile);\
449
exit 0;\
450
fi;\
451
};,\
452 777 aaronmk
echo $(emph)"Note: The preceding failed test is compared to another test's\
453
output"$(endEmph);\
454
echo $(emph)"When it fails, this always indicates a bug"$(endEmph);\
455
)\
456 652 aaronmk
exit $$e;}
457 628 aaronmk
endef
458
459 876 aaronmk
tests :=
460
461 6744 aaronmk
%/test: %/test.xml _always ;
462
463 4269 aaronmk
# Requires staging tables. To create them, run `make inputs/<datasrc>/install`.
464
# Non-flat-file inputs fall back to mimicking a successful test
465 4182 aaronmk
%/test.xml: %/VegBIEN.csv _always
466 4452 aaronmk
	$(if $(nonXml),$(call runTest,by_col=))
467 4182 aaronmk
tests += %/test.xml
468 627 aaronmk
469 4274 aaronmk
%/test.by_col.xml: %/VegBIEN.csv _always
470 4831 aaronmk
	$(if $(nonXml),$(call runTest,by_col=1))
471 4275 aaronmk
472
# Only run column-based tests if column-based mode enabled, because these tests
473
# are much slower than the row-based tests for small numbers of rows
474
ifneq ($(by_col),)
475 4274 aaronmk
tests += %/test.by_col.xml
476 4275 aaronmk
endif
477 4274 aaronmk
478 6157 aaronmk
testOutputs := $(foreach test,$(tests),$(importTables:%=$(test)))
479 724 aaronmk
480 1361 aaronmk
.PRECIOUS: $(testOutputs) # save outputs of failed tests so they can be accepted
481
482 876 aaronmk
test: _always $(testOutputs) ;
483
484 4182 aaronmk
all += $(wildcard %/test*.xml)
485 876 aaronmk
486 634 aaronmk
# Accepts a test output: make <test_output_path>-ok
487 626 aaronmk
%-ok: _always
488 4383 aaronmk
	mv $* $(call testRef,$*)
489 502 aaronmk
490 4120 aaronmk
accept-all: _always
491 3579 aaronmk
	+yes|$(selfMake) test
492
493 3133 aaronmk
##### Documentation
494
495 4494 aaronmk
steps = $(selfMake) -s $*/import test=1 by_col=1 verbosity=2 n=100\
496 3385 aaronmk
2>&1|$(bin)/debug2redmine >$@
497 3188 aaronmk
498 4182 aaronmk
%/logs/steps.by_col.log.sql: _always
499 3188 aaronmk
	+$(steps)