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