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