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 6592 aaronmk
import_source ?= 1
10 1190 aaronmk
log ?= $(if $(test),,1)
11 1257 aaronmk
profile ?=
12 1983 aaronmk
quiet ?=
13 1620 aaronmk
reverify ?= 1
14 5810 aaronmk
schema_only ?=
15 1990 aaronmk
use_staged ?= $(by_col)
16 1921 aaronmk
17
# Makefile
18 5427 aaronmk
exts ?= csv tsv txt dmp xml
19 783 aaronmk
test_n ?= 2
20
21
##### Vars/functions
22
23 1821 aaronmk
# Paths
24 5008 aaronmk
datasrc := $(patsubst .%,%,$(notdir $(realpath .)))
25 1821 aaronmk
bin := $(root)/bin
26
mappings := $(root)/mappings
27
28 368 aaronmk
# Make
29 640 aaronmk
SHELL := /bin/bash
30 1509 aaronmk
selfMake = $(MAKE) --makefile=../input.Makefile
31 1821 aaronmk
subMake = $(MAKE) $(@:$(root)/%=%) --directory=$(root)
32 404 aaronmk
+_ = $(+:_%=)
33 368 aaronmk
addBeforeExt = $(basename $(2))$(1)$(suffix $(2))
34
35 640 aaronmk
# Terminal
36 1184 aaronmk
termCols := $(shell tput cols)
37 640 aaronmk
esc := '['
38
reset := $(esc)'0m'
39
emph := $(esc)'7m '
40
endEmph := ' '$(reset)
41
42 5209 aaronmk
# User interaction
43
44
confirm = $(if $(shell read -p $(emph)"$(1)"$(endEmph)$$'$(if\
45
$(2),\n$(2))\nContinue? (y/n) ' REPLY; test "$$REPLY" = y && echo t),,\
46
$(error Aborting))
47
48 368 aaronmk
# Commands
49 1245 aaronmk
MKDIR = mkdir -p
50
mkdir = $(MKDIR) $(@D)
51 395 aaronmk
CP = cp -p
52 1184 aaronmk
diff = diff --unified=2
53 3721 aaronmk
diffIgnoreSpace = $(diff) --ignore-space-change
54 1184 aaronmk
diffVerbose = $(if $(verbose),diff --side-by-side --left-column\
55
--width=$(termCols),$(diff))
56 368 aaronmk
57 1918 aaronmk
# BIEN commands
58 4137 aaronmk
sortFilenames = $(shell $(bin)/sort_filenames $(1))
59 1524 aaronmk
selfMap = $(bin)/cols 0 0
60 6244 aaronmk
psqlAsBien := $(bin)/psql_script_vegbien
61 6186 aaronmk
psqlNoSearchPath := env no_search_path=1 $(psqlAsBien)
62 4408 aaronmk
# Usage: ($(inDatasrc); cat $(file))|$(psqlCmd)
63 6186 aaronmk
inDatasrc := echo 'SET search_path TO "$(datasrc)";'
64 353 aaronmk
65 1594 aaronmk
# SVN
66
addDir = $(if $(wildcard $(1)/),svn add --depth=empty $(1),svn mkdir $(1))
67 1771 aaronmk
setSvnIgnore = svn propset svn:ignore $(2) $(1)
68 1594 aaronmk
define addDirWithIgnore
69
$(addDir)
70 1771 aaronmk
$(setSvnIgnore)
71 1594 aaronmk
endef
72
73 4414 aaronmk
##### Environment
74
75
export PATH := $(bin):$(PATH)
76
77 783 aaronmk
##### General targets
78 250 aaronmk
79 418 aaronmk
all: _always maps ;
80 247 aaronmk
81 383 aaronmk
clean: _always
82 256 aaronmk
	$(RM) $(all)
83
84 1742 aaronmk
remake: _always clean
85
	+$(selfMake)
86
# re-run make so that cache of existing files is reset
87
88 5876 aaronmk
# Only remake if doesn't exist. This prevents unintentional remaking when the
89
# make script is newly checked out from svn (which sets the mod time to now) but
90
# the output is synced externally.
91
# Can't remove prereq to do this, because it determines when the rule applies.
92
make_script = $(if $(wildcard $@),,"time" ./$< >$@)
93 368 aaronmk
94 6743 aaronmk
%/: %/map.csv _always ;
95
96 1604 aaronmk
%/: % _always ;
97
98 4379 aaronmk
%: %.make
99 5876 aaronmk
	$(make_script)
100 4182 aaronmk
.PRECIOUS: % # save partial outputs of aborted src make scripts
101 1627 aaronmk
102 6379 aaronmk
##### Tables discovery
103 250 aaronmk
104 6379 aaronmk
sortFile := import_order.txt
105 6156 aaronmk
noImportFile := _no_import
106
107 6592 aaronmk
dontImport = $(wildcard $(1)/$(noImportFile))$(if\
108
$(import_source),,$(filter Source,$(1)))
109 6379 aaronmk
110
tables := $(if $(wildcard $(sortFile)),$(shell cat $(sortFile)))
111
    # $(shell) replaces "\n" with " "
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
ifeq ($(tables),)# none specified in sort file
117
tables := $(allTables)
118
endif
119
importTables := $(foreach table,$(tables),$(if\
120
$(call dontImport,$(table)),,$(table)))
121
122
##### SVN
123
124 6581 aaronmk
svnFilesGlob:= */{,$(noImportFile),{,.}{map,*terms,VegBIEN}.csv{,.*},*header.*,*.sql,test.xml.ref}
125 6125 aaronmk
svnFilesGlob := {map.csv,*{schema,~}*.sql,{,*/}*.make,$(svnFilesGlob)}
126 6064 aaronmk
_svnFilesGlob := {_MySQL/{,*schema*.sql,*.make},_src/*.{url,pdf}}
127 5923 aaronmk
svnFiles = $(filter-out _% logs/%,$(call wildcard/,$(svnFilesGlob)))\
128
$(call wildcard/,$(_svnFilesGlob))
129 5878 aaronmk
130 6806 aaronmk
# To update all inputs with these settings: make inputs/add
131 6591 aaronmk
add: _always Source/add Source/map.csv $(allTables:%=%/add)
132 4182 aaronmk
	$(call setSvnIgnore,.,'*')
133 4459 aaronmk
	$(call addDirWithIgnore,logs,$$'*.log.sql\n*.trace')
134 6805 aaronmk
	$(call addDirWithIgnore,verify,$$'*.out\n*.csv\n*.xls\n*.xlsx')
135 4223 aaronmk
	$(call addFile,import_order.txt)
136 6014 aaronmk
	$(if $(wildcard _MySQL/),$(call addDirWithIgnore,_MySQL,'*'))
137 6064 aaronmk
	$(if $(wildcard _src/),$(call addDirWithIgnore,_src,'*'))
138 6733 aaronmk
	$(if $(wildcard _archive/),$(call addDirWithIgnore,_archive,'*'))
139 5921 aaronmk
	$(call add*,$(svnFiles))
140 766 aaronmk
141 4217 aaronmk
# Adds a new table subdir
142
%/add: _always
143
	$(call addDirWithIgnore,$*,'*')
144
	$(call addDirWithIgnore,$*/logs,$$'*.log.sql\n*.trace')
145
146 1969 aaronmk
##### Existing maps discovery
147 1955 aaronmk
148 5035 aaronmk
anyMap := %/map.csv %/VegBIEN.csv %/unmapped_terms.csv %/new_terms.csv
149 3574 aaronmk
150 4214 aaronmk
extsFilter := $(addprefix %.,$(exts))
151
dataOnly = $(filter $(extsFilter),$(1))
152
153 4182 aaronmk
anyTest = $*/test.%
154 4214 aaronmk
srcsOnly = $(filter-out $(anyMap) $(anyTest) %/logs,$(call dataOnly,$(1)))
155 4120 aaronmk
156 6071 aaronmk
srcDict := map.csv
157
158 4844 aaronmk
vocab := $(mappings)/VegCore.csv
159 4658 aaronmk
coreMap := $(mappings)/VegCore-VegBIEN.csv
160 4687 aaronmk
dict := $(mappings)/Veg+-VegCore.csv
161 783 aaronmk
162 6157 aaronmk
viaMaps := $(importTables:%=%/map.csv)
163 783 aaronmk
164 4117 aaronmk
autogenMaps := $(subst map.,VegBIEN.,$(viaMaps))
165 728 aaronmk
directMaps := $(autogenMaps) $(filter-out $(autogenMaps),\
166 4182 aaronmk
$(wildcard */VegBIEN.csv))
167 728 aaronmk
168 1969 aaronmk
##### Sources
169
170 4182 aaronmk
srcs = $(call sortFilenames,$(call srcsOnly,$(wildcard $*/*)))
171 4450 aaronmk
nonHeaderSrcs = $(filter-out %/header.csv,$(srcs))
172
isRef = $(if $(nonHeaderSrcs),,1)
173
    # empty subdir, so references an already-installed staging table
174 4452 aaronmk
isXml = $(filter %.xml,$(nonHeaderSrcs))
175
nonXml = $(if $(isXml),,1)
176
isCsv = $(if $(nonHeaderSrcs),$(if $(isXml),,1))
177 1969 aaronmk
    # true if $(srcs) non-empty and contains no *.xml
178 4465 aaronmk
catSrcs = $(bin)/cat$(if $(nonXml),_csv) $(srcs)
179 1969 aaronmk
withCatSrcs = $(catSrcs:$(bin)/%=$(bin)/with_%) --
180
181 1986 aaronmk
# Usage: `make {--silent|-s} inputs/<datasrc>/cat` (don't echo make commands)
182 6157 aaronmk
cat: $(importTables:%=%/cat) _always ;
183 1969 aaronmk
184 4252 aaronmk
%/cat: _always
185 1969 aaronmk
	$(catSrcs)
186
187 6363 aaronmk
##### Input data retrieval
188
189
# Must come before `%.sql: _MySQL/%.sql` to override it
190
%.sql: %.sql.make
191
	$(make_script)
192
193
# The export must be created with:
194
# `--compatible=postgresql --add-locks=false --set-charset --no-create-info`
195
# Must come before `%.sql: _MySQL/%.sql` to override it
196
%.data.sql: _MySQL/%.data.sql
197
	$(if $(wildcard $@),,$(bin)/my2pg.data <$< >$@)
198
199
# The export must be created with:
200
# `--compatible=postgresql --add-locks=false --set-charset`
201
# Add `--no-data` to create a schema-only export.
202
%.sql: _MySQL/%.sql
203
	$(if $(wildcard $@),,$(bin)/my2pg <$< >$@)
204
205 4121 aaronmk
##### Staging tables installation
206 1921 aaronmk
207 5693 aaronmk
srcTable := %.src
208
209 6364 aaronmk
dbExportsWildcard = $(sort $(patsubst _MySQL/%.make,%,$(wildcard\
210
$(1) _MySQL/$(1).make)))
211
212
dbExports := $(call dbExportsWildcard,*schema*.sql)# schemas first
213
ifeq ($(schema_only),) # add rest of .sql files
214
dbExports += $(filter-out $(dbExports),$(call dbExportsWildcard,*.sql))
215 5810 aaronmk
endif
216 4438 aaronmk
dbExports := $(strip $(dbExports))# += adds extra whitespace
217 6565 aaronmk
allInstalls := $(if $(dbExports),sql) $(filter-out Source,$(allTables))
218 1921 aaronmk
219 4413 aaronmk
install: _always schema $(allInstalls:%=%/install) ;
220
221 5209 aaronmk
uninstall: _always confirm_rm_schema rm_schema ;
222 4121 aaronmk
# rm_schema will also drop all staging tables
223 1921 aaronmk
224
reinstall: _always uninstall install ;
225
226 5209 aaronmk
confirm_rm_schema: _always
227
	$(if $(filter TNRS,$(datasrc)),$(call confirm,WARNING: This will delete the\
228
TNRS cache!,To save it: make backups/TNRS.backup-remake))
229
230 4121 aaronmk
schema: _always
231 6186 aaronmk
	-echo 'CREATE SCHEMA "$(datasrc)";'|$(psqlNoSearchPath)
232 1921 aaronmk
# ignore errors if schema exists
233
234 4121 aaronmk
rm_schema: _always
235 6186 aaronmk
	echo 'DROP SCHEMA IF EXISTS "$(datasrc)" CASCADE;'|$(psqlNoSearchPath)
236 1921 aaronmk
237 5162 aaronmk
installLog := logs/install.log.sql
238 4453 aaronmk
239 5162 aaronmk
logInstall = $(if $(log),$(if $(quiet),$(2)$(1)$(installLog) 2>&1,2>&1|tee $(3)\
240
$(1)$(installLog)))
241 5692 aaronmk
logInstallRoot = $(call logInstall,,>)
242 5162 aaronmk
logInstall* = $(call logInstall,$*/,>)
243
logInstall*Add = $(call logInstall,$*/,>>,-a)# append to log
244
245 4413 aaronmk
# Must come before %/install to override it
246 4434 aaronmk
sql/install: $(dbExports)
247 6186 aaronmk
	($(inDatasrc); cat $+|pg_dump_limit)|"time" $(psqlNoSearchPath) \
248 5796 aaronmk
--set=schema='"$(datasrc)"' $(logInstallRoot)
249 4413 aaronmk
250 5795 aaronmk
cleanup = $(if $(wildcard $*/cleanup.sql),($(inDatasrc); cat $*/cleanup.sql)\
251 6186 aaronmk
|"time" $(psqlNoSearchPath) --echo-all --set=table='"$*"' $(logInstall*Add),\
252
(prefix=; . $(bin)/vegbien_dest; unset schemas; env schema=$(datasrc) table=$*\
253
$(bin)/csv2db) $(logInstall*Add))
254 4449 aaronmk
255
define exportHeader
256
$(cleanup)
257 6186 aaronmk
echo 'SELECT * FROM "$(datasrc)"."$*" LIMIT 0;'|$(psqlNoSearchPath) \
258 4449 aaronmk
--no-align --field-separator=, --pset=footer=off >$*/header.csv
259
endef
260
261 4260 aaronmk
# For staging tables which are derived by joining together other staging tables.
262 4263 aaronmk
%/install %/header.csv: %/create.sql _always
263 4527 aaronmk
	($(inDatasrc); echo 'CREATE TABLE "$*" AS'; cat $<; echo ';')|"time" \
264 6186 aaronmk
$(psqlNoSearchPath) --echo-all --set=schema='"$(datasrc)"' --set=table='"$*"' \
265 5912 aaronmk
$(logInstall*)
266 4449 aaronmk
	$(exportHeader)
267 5441 aaronmk
.PRECIOUS: %/header.csv
268 4260 aaronmk
269 4252 aaronmk
%/install: _always
270 4456 aaronmk
	$(if $(isRef),$(exportHeader),$(if $(nonXml),$(import_install_)))
271 6078 aaronmk
	$(if $(wildcard $*/postprocess.sql),($(inDatasrc); cat $*/postprocess.sql;)\
272 6186 aaronmk
|"time" $(psqlNoSearchPath) --echo-all --set=table='"$*"' $(logInstall*Add))
273 5693 aaronmk
define import_install_
274 6186 aaronmk
(prefix=; . $(bin)/vegbien_dest; unset schemas; "time" nice -n +5\
275 5028 aaronmk
env schema=$(datasrc) table=$* $(bin)/csv2db $(catSrcs) $(logInstall*))
276 5693 aaronmk
$(if $(filter $(srcTable),$*),($(inDatasrc);\
277
echo 'ALTER TABLE "$(datasrc)"."$*" RENAME row_num TO "$*.row_num";')|"time"\
278 6186 aaronmk
$(psqlNoSearchPath) --echo-all --set=table='"$*"' $(logInstall*Add))
279 5693 aaronmk
endef
280 5697 aaronmk
# table-scope src table's row_num col to allow joining it with other tables
281 1921 aaronmk
282 5819 aaronmk
%/uninstall: _always
283 6186 aaronmk
	echo 'DROP TABLE IF EXISTS "$(datasrc)"."$*" CASCADE;'|$(psqlNoSearchPath)
284 5819 aaronmk
285
%/reinstall: _always %/uninstall %/install ;
286
287 5155 aaronmk
cleanup: _always $(tables:%=%/cleanup) ;
288
289 5184 aaronmk
# WARNING: This removes any index comments, due to a PostgreSQL bug.
290
# This occurs because ALTER TABLE recreates the index but not its comment.
291 5155 aaronmk
%/cleanup: _always
292
	$(cleanup)
293
294 1777 aaronmk
##### Maps building
295
296 4646 aaronmk
# Maps to (try to) build are added to this
297 1779 aaronmk
maps :=
298
299 4208 aaronmk
srcRoot = $(mappings)/root.sh
300 3569 aaronmk
mkSrcMap = $(catSrcs)|(. $(srcRoot); env datasrc=$(datasrc) $(bin)/src_map >$@)
301 1779 aaronmk
302 6074 aaronmk
define translate
303
$(bin)/in_place $< $(bin)/canon 1 $(1)
304
$(bin)/in_place $< $(bin)/translate 1 $(1)
305
endef
306
translate? = $(if $(wildcard $(1)),$(translate))
307
308 6076 aaronmk
$(srcDict):# empty target in case it doesn't exist
309
310 3572 aaronmk
# Via maps cleanup
311 4182 aaronmk
ifneq ($(filter %/.map.csv.last_cleanup,$(MAKECMDGOALS)),)
312 6076 aaronmk
%/.map.csv.last_cleanup: %/map.csv $(vocab) $(dict) $(coreMap) $(srcDict)
313 6074 aaronmk
	$(call translate?,$(srcDict))
314 4844 aaronmk
	$(bin)/in_place $< $(bin)/canon 1 $(vocab)
315 6077 aaronmk
	$(call translate?,$(dict))
316 3572 aaronmk
	touch $@
317 4601 aaronmk
	+$(selfMake) $(<:%/map.csv=%/unmapped_terms.csv)
318
	+$(selfMake) $(<:%/map.csv=%/new_terms.csv)
319 5253 aaronmk
.PRECIOUS: %/.map.csv.last_cleanup
320 3572 aaronmk
else
321 4644 aaronmk
%/map.csv: _always
322
	$(if $(wildcard $@),,$(if $(nonXml),$(mkSrcMap)))
323 4182 aaronmk
	+$(selfMake) $(@:%/map.csv=%/.map.csv.last_cleanup)
324 5254 aaronmk
.PRECIOUS: %/map.csv
325 3572 aaronmk
endif
326
327 4641 aaronmk
%/VegBIEN.csv: %/map.csv $(coreMap)
328 4656 aaronmk
	<$< $(bin)/cat_cols 1 2|$(bin)/join $(coreMap)|$(bin)/sort_map >$@
329 1530 aaronmk
maps += $(autogenMaps)
330
331 4646 aaronmk
maps: $(maps) _always ;
332 1139 aaronmk
333
all += $(maps)
334
335 1742 aaronmk
##### Maps validation
336
337 4663 aaronmk
# `tail -n +2`: Remove header before running filter_out_ci because filter_out_ci
338
# only removes the header if it matches the vocabulary's header.
339 4601 aaronmk
340 4664 aaronmk
%/unmapped_terms.csv: %/map.csv $(coreMap)
341 4858 aaronmk
	tail -n +2 $<|$(bin)/cols 1|$(bin)/filter_out_ci 0 $(coreMap) >$@
342
	$(bin)/autoremove $@
343 4601 aaronmk
344 4844 aaronmk
%/new_terms.csv: %/map.csv $(vocab) $(dict) %/unmapped_terms.csv
345 4857 aaronmk
	$(newTerms)
346
	$(bin)/autoremove $@
347
newTerms = tail -n +2 $<|$(bin)/filter_out_ci 0 $(vocab)|$(bin)/filter_out_ci 0\
348 6807 aaronmk
$(dict) $(if $(wildcard $(word 4,$+)),|$(bin)/filter_out_ci 1 $(word 4,$+))\
349 5932 aaronmk
|grep -vE '^"?:' >$@; exit 0# because grep exits nonzero if no match
350 4601 aaronmk
351 6157 aaronmk
termsSubdirs := $(importTables)
352 4600 aaronmk
353 3764 aaronmk
include $(root)/lib/mappings.Makefile
354 1742 aaronmk
355 1509 aaronmk
##### External dependencies
356
357
$(root)/%: _always
358
	+$(subMake)
359 1628 aaronmk
.PRECIOUS: $(root)/% # let ext. dir's Makefile decide whether to delete on error
360 1509 aaronmk
361 783 aaronmk
##### Mapping
362 368 aaronmk
363 4182 aaronmk
+maps = $(filter %/map.csv %/VegBIEN.csv $(mappings)/%,$(+_))
364 4746 aaronmk
map2db = env in_database=vegbien in_schema=$(datasrc) in_table=$*\
365 4748 aaronmk
out_database=vegbien $(root)/map $(+maps)
366 368 aaronmk
367 783 aaronmk
##### Import to VegBIEN
368 772 aaronmk
369 1853 aaronmk
profileTest = $(if $(profile),$(if $(test),1))
370
profileOnly = -env profile_to=/dev/fd/3 $(map2db) 3>&1 1>&2|\
371
$(bin)/profile_stats /dev/fd/0
372
373 5457 aaronmk
log_ = $*/logs/$(if $(n),n=$(n).,)$(version).log.sql
374 3317 aaronmk
trace = $(log_:.log.sql=.trace)
375 1853 aaronmk
import = -$(if $(profileTest),$(profileOnly),(set -x; "time" env commit=1\
376 1190 aaronmk
$(if $(profile),profile_to=$(trace)) $(map2db)) $(if $(log),\
377 3616 aaronmk
$(if $(n),,&>$(log_)))$(if $(log),$(if $(n), 2>&1|tee -a $(log_))))
378 1088 aaronmk
# don't abort on import errors, which often relate to invalid input data
379 624 aaronmk
380 6156 aaronmk
import: $(importTables:%=%/import) _always ;
381 718 aaronmk
382 4252 aaronmk
%/import: %/VegBIEN.csv _always
383 718 aaronmk
	$(import)
384 772 aaronmk
# default:
385 4252 aaronmk
%/import: _always ;
386 718 aaronmk
387 790 aaronmk
##### Log files from import
388
389 4182 aaronmk
logs := $(wildcard */logs/*.log.sql */logs/*.trace)
390 339 aaronmk
391 383 aaronmk
rm_logs: _always
392 339 aaronmk
	$(RM) $(logs)
393
394 783 aaronmk
##### Verification of import
395 264 aaronmk
396 5782 aaronmk
verifyTables := $(patsubst verify/%.ref,%,$(wildcard verify/*.ref))
397 1194 aaronmk
398 5782 aaronmk
verify: $(verifyTables:%=%/verify) _always ;
399
400 4252 aaronmk
%/verify: verify/%.ref verify/%.out _always
401 1184 aaronmk
	-$(diffVerbose) $(+_)
402 1088 aaronmk
# don't abort on verification errors, which are expected during development
403 1194 aaronmk
# default:
404 4252 aaronmk
%/verify: verify/%.out _always
405 1200 aaronmk
	$(if $(shell test -e $< && echo t),cat $<)
406 1199 aaronmk
# don't run if verify/%.out's default do-nothing action was used
407 1200 aaronmk
# can't use $(wildcard) because it won't recheck file after verify/%.out is run
408 368 aaronmk
409 6186 aaronmk
psqlExport := "time" $(psqlNoSearchPath) --no-align --field-separator=$$'\t'\
410 4401 aaronmk
--pset=footer=off --pset=null=NULL
411 5976 aaronmk
# Note that using $(inDatasrc) will not work with datasources whose tables are
412
# the same name as VegBIEN tables (likely only VegBank), because the datasource
413
# is first in the search_path.
414
verify = $(if $(reverify),($(inDatasrc); cat $<)|$(psqlExport)\
415
--set=datasource="'$(datasrc)'" >$@)
416 514 aaronmk
417 1199 aaronmk
verify/%.out: $(mappings)/verify.%.sql _always
418 1192 aaronmk
	$(verify)
419 5973 aaronmk
.PRECIOUS: verify/%.out # save partial output in case of error to help debugging
420 1194 aaronmk
# default:
421 1199 aaronmk
verify/%.out: _always ;
422 369 aaronmk
423 1199 aaronmk
all += $(wildcard verify/*.out)
424 1192 aaronmk
425 4402 aaronmk
%.ref: %.ref.sql
426 4409 aaronmk
	($(inDatasrc); cat $<)|$(psqlExport) >$@
427 5981 aaronmk
.PRECIOUS: %.ref # there must always be a .ref for the make rules to work
428 4402 aaronmk
429 1667 aaronmk
##### Editing import
430
431 4121 aaronmk
rotate: _always
432 6206 aaronmk
	echo "UPDATE source SET shortname = shortname||'.$(version)'\
433
WHERE shortname = '$(datasrc)';"|$(psqlAsBien)
434 1667 aaronmk
435 4121 aaronmk
rm: _always
436 6206 aaronmk
	echo "DELETE FROM source WHERE shortname = '$(datasrc)';"|$(psqlAsBien)
437 1667 aaronmk
438 783 aaronmk
##### Testing
439 368 aaronmk
440 4274 aaronmk
testRefOutput = $(subst .by_col,,$(1))
441 1986 aaronmk
testRef = $(testRefOutput).ref
442
hasOwnRef = $(filter $@,$(call testRefOutput,$@))
443
# filter returns non-empty if they are equal
444 630 aaronmk
445 4246 aaronmk
# `rm $@`: Remove outputs of successful tests to reduce clutter
446 4272 aaronmk
# `$(foreach use_staged...)`: Run with use_staged=1
447 897 aaronmk
define runTest
448 776 aaronmk
@echo "Testing $(abspath $@)..."
449 4272 aaronmk
>$@ env test=1 n=$(test_n) $(1) $(foreach use_staged,1,$(map2db))
450 6446 aaronmk
$(if $(hasOwnRef),,-)@(set -x; $(diffIgnoreSpace) $(call testRef,$@) $@) 2>&1\
451
&& rm $@ || { e=$$?; $(if $(wildcard $(call testRef,$@)),,cat $@;)\
452 777 aaronmk
$(if $(hasOwnRef),\
453 3179 aaronmk
{\
454
read -p $(emph)'Accept new test output? (y/n)'$(endEmph) REPLY;\
455
if test "$$REPLY" = y; then\
456
(set -x; $(MAKE) $@-ok --directory=$(realpath .) --makefile=../input.Makefile);\
457
exit 0;\
458
fi;\
459
};,\
460 777 aaronmk
echo $(emph)"Note: The preceding failed test is compared to another test's\
461
output"$(endEmph);\
462
echo $(emph)"When it fails, this always indicates a bug"$(endEmph);\
463
)\
464 652 aaronmk
exit $$e;}
465 628 aaronmk
endef
466
467 876 aaronmk
tests :=
468
469 6744 aaronmk
%/test: %/test.xml _always ;
470
471 4269 aaronmk
# Requires staging tables. To create them, run `make inputs/<datasrc>/install`.
472
# Non-flat-file inputs fall back to mimicking a successful test
473 4182 aaronmk
%/test.xml: %/VegBIEN.csv _always
474 4452 aaronmk
	$(if $(nonXml),$(call runTest,by_col=))
475 4182 aaronmk
tests += %/test.xml
476 627 aaronmk
477 4274 aaronmk
%/test.by_col.xml: %/VegBIEN.csv _always
478 4831 aaronmk
	$(if $(nonXml),$(call runTest,by_col=1))
479 4275 aaronmk
480
# Only run column-based tests if column-based mode enabled, because these tests
481
# are much slower than the row-based tests for small numbers of rows
482
ifneq ($(by_col),)
483 4274 aaronmk
tests += %/test.by_col.xml
484 4275 aaronmk
endif
485 4274 aaronmk
486 6157 aaronmk
testOutputs := $(foreach test,$(tests),$(importTables:%=$(test)))
487 724 aaronmk
488 1361 aaronmk
.PRECIOUS: $(testOutputs) # save outputs of failed tests so they can be accepted
489
490 876 aaronmk
test: _always $(testOutputs) ;
491
492 4182 aaronmk
all += $(wildcard %/test*.xml)
493 876 aaronmk
494 634 aaronmk
# Accepts a test output: make <test_output_path>-ok
495 626 aaronmk
%-ok: _always
496 4383 aaronmk
	mv $* $(call testRef,$*)
497 502 aaronmk
498 4120 aaronmk
accept-all: _always
499 3579 aaronmk
	+yes|$(selfMake) test
500
501 3133 aaronmk
##### Documentation
502
503 4494 aaronmk
steps = $(selfMake) -s $*/import test=1 by_col=1 verbosity=2 n=100\
504 3385 aaronmk
2>&1|$(bin)/debug2redmine >$@
505 3188 aaronmk
506 4182 aaronmk
%/logs/steps.by_col.log.sql: _always
507 3188 aaronmk
	+$(steps)