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 1604 aaronmk
%/: % _always ;
95
96 4379 aaronmk
%: %.make
97 5876 aaronmk
	$(make_script)
98 4182 aaronmk
.PRECIOUS: % # save partial outputs of aborted src make scripts
99 1627 aaronmk
100 6379 aaronmk
##### Tables discovery
101 250 aaronmk
102 6379 aaronmk
sortFile := import_order.txt
103 6156 aaronmk
noImportFile := _no_import
104
105 6592 aaronmk
dontImport = $(wildcard $(1)/$(noImportFile))$(if\
106
$(import_source),,$(filter Source,$(1)))
107 6379 aaronmk
108
tables := $(if $(wildcard $(sortFile)),$(shell cat $(sortFile)))
109
    # $(shell) replaces "\n" with " "
110
allSubdirs := $(call wildcard/,*/)
111
allTables := $(call sortFilenames,$(filter-out _% verify logs,$(allSubdirs:%/=%)))
112
joinedTables := $(filter-out $(tables),$(allTables))
113
allTables := $(strip $(joinedTables) $(tables))# move joined tables to beginning
114
ifeq ($(tables),)# none specified in sort file
115
tables := $(allTables)
116
endif
117
importTables := $(foreach table,$(tables),$(if\
118
$(call dontImport,$(table)),,$(table)))
119
120
##### SVN
121
122 6581 aaronmk
svnFilesGlob:= */{,$(noImportFile),{,.}{map,*terms,VegBIEN}.csv{,.*},*header.*,*.sql,test.xml.ref}
123 6125 aaronmk
svnFilesGlob := {map.csv,*{schema,~}*.sql,{,*/}*.make,$(svnFilesGlob)}
124 6064 aaronmk
_svnFilesGlob := {_MySQL/{,*schema*.sql,*.make},_src/*.{url,pdf}}
125 5923 aaronmk
svnFiles = $(filter-out _% logs/%,$(call wildcard/,$(svnFilesGlob)))\
126
$(call wildcard/,$(_svnFilesGlob))
127 5878 aaronmk
128 6591 aaronmk
add: _always Source/add Source/map.csv $(allTables:%=%/add)
129 4182 aaronmk
	$(call setSvnIgnore,.,'*')
130 4459 aaronmk
	$(call addDirWithIgnore,logs,$$'*.log.sql\n*.trace')
131 6649 aaronmk
	$(call addDirWithIgnore,verify,$$'*.out\n*.csv\n*.xls')
132 4223 aaronmk
	$(call addFile,import_order.txt)
133 6014 aaronmk
	$(if $(wildcard _MySQL/),$(call addDirWithIgnore,_MySQL,'*'))
134 6064 aaronmk
	$(if $(wildcard _src/),$(call addDirWithIgnore,_src,'*'))
135 6733 aaronmk
	$(if $(wildcard _archive/),$(call addDirWithIgnore,_archive,'*'))
136 5921 aaronmk
	$(call add*,$(svnFiles))
137 766 aaronmk
138 4217 aaronmk
# Adds a new table subdir
139
%/add: _always
140
	$(call addDirWithIgnore,$*,'*')
141
	$(call addDirWithIgnore,$*/logs,$$'*.log.sql\n*.trace')
142
143 1969 aaronmk
##### Existing maps discovery
144 1955 aaronmk
145 5035 aaronmk
anyMap := %/map.csv %/VegBIEN.csv %/unmapped_terms.csv %/new_terms.csv
146 3574 aaronmk
147 4214 aaronmk
extsFilter := $(addprefix %.,$(exts))
148
dataOnly = $(filter $(extsFilter),$(1))
149
150 4182 aaronmk
anyTest = $*/test.%
151 4214 aaronmk
srcsOnly = $(filter-out $(anyMap) $(anyTest) %/logs,$(call dataOnly,$(1)))
152 4120 aaronmk
153 6071 aaronmk
srcDict := map.csv
154
155 4844 aaronmk
vocab := $(mappings)/VegCore.csv
156 4658 aaronmk
coreMap := $(mappings)/VegCore-VegBIEN.csv
157 4687 aaronmk
dict := $(mappings)/Veg+-VegCore.csv
158 783 aaronmk
159 6157 aaronmk
viaMaps := $(importTables:%=%/map.csv)
160 783 aaronmk
161 4117 aaronmk
autogenMaps := $(subst map.,VegBIEN.,$(viaMaps))
162 728 aaronmk
directMaps := $(autogenMaps) $(filter-out $(autogenMaps),\
163 4182 aaronmk
$(wildcard */VegBIEN.csv))
164 728 aaronmk
165 1969 aaronmk
##### Sources
166
167 4182 aaronmk
srcs = $(call sortFilenames,$(call srcsOnly,$(wildcard $*/*)))
168 4450 aaronmk
nonHeaderSrcs = $(filter-out %/header.csv,$(srcs))
169
isRef = $(if $(nonHeaderSrcs),,1)
170
    # empty subdir, so references an already-installed staging table
171 4452 aaronmk
isXml = $(filter %.xml,$(nonHeaderSrcs))
172
nonXml = $(if $(isXml),,1)
173
isCsv = $(if $(nonHeaderSrcs),$(if $(isXml),,1))
174 1969 aaronmk
    # true if $(srcs) non-empty and contains no *.xml
175 4465 aaronmk
catSrcs = $(bin)/cat$(if $(nonXml),_csv) $(srcs)
176 1969 aaronmk
withCatSrcs = $(catSrcs:$(bin)/%=$(bin)/with_%) --
177
178 1986 aaronmk
# Usage: `make {--silent|-s} inputs/<datasrc>/cat` (don't echo make commands)
179 6157 aaronmk
cat: $(importTables:%=%/cat) _always ;
180 1969 aaronmk
181 4252 aaronmk
%/cat: _always
182 1969 aaronmk
	$(catSrcs)
183
184 6363 aaronmk
##### Input data retrieval
185
186
# Must come before `%.sql: _MySQL/%.sql` to override it
187
%.sql: %.sql.make
188
	$(make_script)
189
190
# The export must be created with:
191
# `--compatible=postgresql --add-locks=false --set-charset --no-create-info`
192
# Must come before `%.sql: _MySQL/%.sql` to override it
193
%.data.sql: _MySQL/%.data.sql
194
	$(if $(wildcard $@),,$(bin)/my2pg.data <$< >$@)
195
196
# The export must be created with:
197
# `--compatible=postgresql --add-locks=false --set-charset`
198
# Add `--no-data` to create a schema-only export.
199
%.sql: _MySQL/%.sql
200
	$(if $(wildcard $@),,$(bin)/my2pg <$< >$@)
201
202 4121 aaronmk
##### Staging tables installation
203 1921 aaronmk
204 5693 aaronmk
srcTable := %.src
205
206 6364 aaronmk
dbExportsWildcard = $(sort $(patsubst _MySQL/%.make,%,$(wildcard\
207
$(1) _MySQL/$(1).make)))
208
209
dbExports := $(call dbExportsWildcard,*schema*.sql)# schemas first
210
ifeq ($(schema_only),) # add rest of .sql files
211
dbExports += $(filter-out $(dbExports),$(call dbExportsWildcard,*.sql))
212 5810 aaronmk
endif
213 4438 aaronmk
dbExports := $(strip $(dbExports))# += adds extra whitespace
214 6565 aaronmk
allInstalls := $(if $(dbExports),sql) $(filter-out Source,$(allTables))
215 1921 aaronmk
216 4413 aaronmk
install: _always schema $(allInstalls:%=%/install) ;
217
218 5209 aaronmk
uninstall: _always confirm_rm_schema rm_schema ;
219 4121 aaronmk
# rm_schema will also drop all staging tables
220 1921 aaronmk
221
reinstall: _always uninstall install ;
222
223 5209 aaronmk
confirm_rm_schema: _always
224
	$(if $(filter TNRS,$(datasrc)),$(call confirm,WARNING: This will delete the\
225
TNRS cache!,To save it: make backups/TNRS.backup-remake))
226
227 4121 aaronmk
schema: _always
228 6186 aaronmk
	-echo 'CREATE SCHEMA "$(datasrc)";'|$(psqlNoSearchPath)
229 1921 aaronmk
# ignore errors if schema exists
230
231 4121 aaronmk
rm_schema: _always
232 6186 aaronmk
	echo 'DROP SCHEMA IF EXISTS "$(datasrc)" CASCADE;'|$(psqlNoSearchPath)
233 1921 aaronmk
234 5162 aaronmk
installLog := logs/install.log.sql
235 4453 aaronmk
236 5162 aaronmk
logInstall = $(if $(log),$(if $(quiet),$(2)$(1)$(installLog) 2>&1,2>&1|tee $(3)\
237
$(1)$(installLog)))
238 5692 aaronmk
logInstallRoot = $(call logInstall,,>)
239 5162 aaronmk
logInstall* = $(call logInstall,$*/,>)
240
logInstall*Add = $(call logInstall,$*/,>>,-a)# append to log
241
242 4413 aaronmk
# Must come before %/install to override it
243 4434 aaronmk
sql/install: $(dbExports)
244 6186 aaronmk
	($(inDatasrc); cat $+|pg_dump_limit)|"time" $(psqlNoSearchPath) \
245 5796 aaronmk
--set=schema='"$(datasrc)"' $(logInstallRoot)
246 4413 aaronmk
247 5795 aaronmk
cleanup = $(if $(wildcard $*/cleanup.sql),($(inDatasrc); cat $*/cleanup.sql)\
248 6186 aaronmk
|"time" $(psqlNoSearchPath) --echo-all --set=table='"$*"' $(logInstall*Add),\
249
(prefix=; . $(bin)/vegbien_dest; unset schemas; env schema=$(datasrc) table=$*\
250
$(bin)/csv2db) $(logInstall*Add))
251 4449 aaronmk
252
define exportHeader
253
$(cleanup)
254 6186 aaronmk
echo 'SELECT * FROM "$(datasrc)"."$*" LIMIT 0;'|$(psqlNoSearchPath) \
255 4449 aaronmk
--no-align --field-separator=, --pset=footer=off >$*/header.csv
256
endef
257
258 4260 aaronmk
# For staging tables which are derived by joining together other staging tables.
259 4263 aaronmk
%/install %/header.csv: %/create.sql _always
260 4527 aaronmk
	($(inDatasrc); echo 'CREATE TABLE "$*" AS'; cat $<; echo ';')|"time" \
261 6186 aaronmk
$(psqlNoSearchPath) --echo-all --set=schema='"$(datasrc)"' --set=table='"$*"' \
262 5912 aaronmk
$(logInstall*)
263 4449 aaronmk
	$(exportHeader)
264 5441 aaronmk
.PRECIOUS: %/header.csv
265 4260 aaronmk
266 4252 aaronmk
%/install: _always
267 4456 aaronmk
	$(if $(isRef),$(exportHeader),$(if $(nonXml),$(import_install_)))
268 6078 aaronmk
	$(if $(wildcard $*/postprocess.sql),($(inDatasrc); cat $*/postprocess.sql;)\
269 6186 aaronmk
|"time" $(psqlNoSearchPath) --echo-all --set=table='"$*"' $(logInstall*Add))
270 5693 aaronmk
define import_install_
271 6186 aaronmk
(prefix=; . $(bin)/vegbien_dest; unset schemas; "time" nice -n +5\
272 5028 aaronmk
env schema=$(datasrc) table=$* $(bin)/csv2db $(catSrcs) $(logInstall*))
273 5693 aaronmk
$(if $(filter $(srcTable),$*),($(inDatasrc);\
274
echo 'ALTER TABLE "$(datasrc)"."$*" RENAME row_num TO "$*.row_num";')|"time"\
275 6186 aaronmk
$(psqlNoSearchPath) --echo-all --set=table='"$*"' $(logInstall*Add))
276 5693 aaronmk
endef
277 5697 aaronmk
# table-scope src table's row_num col to allow joining it with other tables
278 1921 aaronmk
279 5819 aaronmk
%/uninstall: _always
280 6186 aaronmk
	echo 'DROP TABLE IF EXISTS "$(datasrc)"."$*" CASCADE;'|$(psqlNoSearchPath)
281 5819 aaronmk
282
%/reinstall: _always %/uninstall %/install ;
283
284 5155 aaronmk
cleanup: _always $(tables:%=%/cleanup) ;
285
286 5184 aaronmk
# WARNING: This removes any index comments, due to a PostgreSQL bug.
287
# This occurs because ALTER TABLE recreates the index but not its comment.
288 5155 aaronmk
%/cleanup: _always
289
	$(cleanup)
290
291 1777 aaronmk
##### Maps building
292
293 4345 aaronmk
# WARNING: You CANNOT make a subdir using `make inputs/<datasrc>/<subdir>/`.
294
# You must instead make the entire datasource dir: `make inputs/<datasrc>/`
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 5926 aaronmk
$(dict) $(if $(wildcard $(word 4,$+)),|$(bin)/filter_out_ci 0 $(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 4269 aaronmk
# Requires staging tables. To create them, run `make inputs/<datasrc>/install`.
470
# Non-flat-file inputs fall back to mimicking a successful test
471 4182 aaronmk
%/test.xml: %/VegBIEN.csv _always
472 4452 aaronmk
	$(if $(nonXml),$(call runTest,by_col=))
473 4182 aaronmk
tests += %/test.xml
474 627 aaronmk
475 4274 aaronmk
%/test.by_col.xml: %/VegBIEN.csv _always
476 4831 aaronmk
	$(if $(nonXml),$(call runTest,by_col=1))
477 4275 aaronmk
478
# Only run column-based tests if column-based mode enabled, because these tests
479
# are much slower than the row-based tests for small numbers of rows
480
ifneq ($(by_col),)
481 4274 aaronmk
tests += %/test.by_col.xml
482 4275 aaronmk
endif
483 4274 aaronmk
484 6157 aaronmk
testOutputs := $(foreach test,$(tests),$(importTables:%=$(test)))
485 724 aaronmk
486 1361 aaronmk
.PRECIOUS: $(testOutputs) # save outputs of failed tests so they can be accepted
487
488 876 aaronmk
test: _always $(testOutputs) ;
489
490 4182 aaronmk
all += $(wildcard %/test*.xml)
491 876 aaronmk
492 634 aaronmk
# Accepts a test output: make <test_output_path>-ok
493 626 aaronmk
%-ok: _always
494 4383 aaronmk
	mv $* $(call testRef,$*)
495 502 aaronmk
496 4120 aaronmk
accept-all: _always
497 3579 aaronmk
	+yes|$(selfMake) test
498
499 3133 aaronmk
##### Documentation
500
501 4494 aaronmk
steps = $(selfMake) -s $*/import test=1 by_col=1 verbosity=2 n=100\
502 3385 aaronmk
2>&1|$(bin)/debug2redmine >$@
503 3188 aaronmk
504 4182 aaronmk
%/logs/steps.by_col.log.sql: _always
505 3188 aaronmk
	+$(steps)