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