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 6480 aaronmk
	$(call addDirWithIgnore,verify,$$'*.out\n*.csv')
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 5921 aaronmk
	$(call add*,$(svnFiles))
136 766 aaronmk
137 4217 aaronmk
# Adds a new table subdir
138
%/add: _always
139
	$(call addDirWithIgnore,$*,'*')
140
	$(call addDirWithIgnore,$*/logs,$$'*.log.sql\n*.trace')
141
142 1969 aaronmk
##### Existing maps discovery
143 1955 aaronmk
144 5035 aaronmk
anyMap := %/map.csv %/VegBIEN.csv %/unmapped_terms.csv %/new_terms.csv
145 3574 aaronmk
146 4214 aaronmk
extsFilter := $(addprefix %.,$(exts))
147
dataOnly = $(filter $(extsFilter),$(1))
148
149 4182 aaronmk
anyTest = $*/test.%
150 4214 aaronmk
srcsOnly = $(filter-out $(anyMap) $(anyTest) %/logs,$(call dataOnly,$(1)))
151 4120 aaronmk
152 6071 aaronmk
srcDict := map.csv
153
154 4844 aaronmk
vocab := $(mappings)/VegCore.csv
155 4658 aaronmk
coreMap := $(mappings)/VegCore-VegBIEN.csv
156 4687 aaronmk
dict := $(mappings)/Veg+-VegCore.csv
157 783 aaronmk
158 6157 aaronmk
viaMaps := $(importTables:%=%/map.csv)
159 783 aaronmk
160 4117 aaronmk
autogenMaps := $(subst map.,VegBIEN.,$(viaMaps))
161 728 aaronmk
directMaps := $(autogenMaps) $(filter-out $(autogenMaps),\
162 4182 aaronmk
$(wildcard */VegBIEN.csv))
163 728 aaronmk
164 1969 aaronmk
##### Sources
165
166 4182 aaronmk
srcs = $(call sortFilenames,$(call srcsOnly,$(wildcard $*/*)))
167 4450 aaronmk
nonHeaderSrcs = $(filter-out %/header.csv,$(srcs))
168
isRef = $(if $(nonHeaderSrcs),,1)
169
    # empty subdir, so references an already-installed staging table
170 4452 aaronmk
isXml = $(filter %.xml,$(nonHeaderSrcs))
171
nonXml = $(if $(isXml),,1)
172
isCsv = $(if $(nonHeaderSrcs),$(if $(isXml),,1))
173 1969 aaronmk
    # true if $(srcs) non-empty and contains no *.xml
174 4465 aaronmk
catSrcs = $(bin)/cat$(if $(nonXml),_csv) $(srcs)
175 1969 aaronmk
withCatSrcs = $(catSrcs:$(bin)/%=$(bin)/with_%) --
176
177 1986 aaronmk
# Usage: `make {--silent|-s} inputs/<datasrc>/cat` (don't echo make commands)
178 6157 aaronmk
cat: $(importTables:%=%/cat) _always ;
179 1969 aaronmk
180 4252 aaronmk
%/cat: _always
181 1969 aaronmk
	$(catSrcs)
182
183 6363 aaronmk
##### Input data retrieval
184
185
# Must come before `%.sql: _MySQL/%.sql` to override it
186
%.sql: %.sql.make
187
	$(make_script)
188
189
# The export must be created with:
190
# `--compatible=postgresql --add-locks=false --set-charset --no-create-info`
191
# Must come before `%.sql: _MySQL/%.sql` to override it
192
%.data.sql: _MySQL/%.data.sql
193
	$(if $(wildcard $@),,$(bin)/my2pg.data <$< >$@)
194
195
# The export must be created with:
196
# `--compatible=postgresql --add-locks=false --set-charset`
197
# Add `--no-data` to create a schema-only export.
198
%.sql: _MySQL/%.sql
199
	$(if $(wildcard $@),,$(bin)/my2pg <$< >$@)
200
201 4121 aaronmk
##### Staging tables installation
202 1921 aaronmk
203 5693 aaronmk
srcTable := %.src
204
205 6364 aaronmk
dbExportsWildcard = $(sort $(patsubst _MySQL/%.make,%,$(wildcard\
206
$(1) _MySQL/$(1).make)))
207
208
dbExports := $(call dbExportsWildcard,*schema*.sql)# schemas first
209
ifeq ($(schema_only),) # add rest of .sql files
210
dbExports += $(filter-out $(dbExports),$(call dbExportsWildcard,*.sql))
211 5810 aaronmk
endif
212 4438 aaronmk
dbExports := $(strip $(dbExports))# += adds extra whitespace
213 6565 aaronmk
allInstalls := $(if $(dbExports),sql) $(filter-out Source,$(allTables))
214 1921 aaronmk
215 4413 aaronmk
install: _always schema $(allInstalls:%=%/install) ;
216
217 5209 aaronmk
uninstall: _always confirm_rm_schema rm_schema ;
218 4121 aaronmk
# rm_schema will also drop all staging tables
219 1921 aaronmk
220
reinstall: _always uninstall install ;
221
222 5209 aaronmk
confirm_rm_schema: _always
223
	$(if $(filter TNRS,$(datasrc)),$(call confirm,WARNING: This will delete the\
224
TNRS cache!,To save it: make backups/TNRS.backup-remake))
225
226 4121 aaronmk
schema: _always
227 6186 aaronmk
	-echo 'CREATE SCHEMA "$(datasrc)";'|$(psqlNoSearchPath)
228 1921 aaronmk
# ignore errors if schema exists
229
230 4121 aaronmk
rm_schema: _always
231 6186 aaronmk
	echo 'DROP SCHEMA IF EXISTS "$(datasrc)" CASCADE;'|$(psqlNoSearchPath)
232 1921 aaronmk
233 5162 aaronmk
installLog := logs/install.log.sql
234 4453 aaronmk
235 5162 aaronmk
logInstall = $(if $(log),$(if $(quiet),$(2)$(1)$(installLog) 2>&1,2>&1|tee $(3)\
236
$(1)$(installLog)))
237 5692 aaronmk
logInstallRoot = $(call logInstall,,>)
238 5162 aaronmk
logInstall* = $(call logInstall,$*/,>)
239
logInstall*Add = $(call logInstall,$*/,>>,-a)# append to log
240
241 4413 aaronmk
# Must come before %/install to override it
242 4434 aaronmk
sql/install: $(dbExports)
243 6186 aaronmk
	($(inDatasrc); cat $+|pg_dump_limit)|"time" $(psqlNoSearchPath) \
244 5796 aaronmk
--set=schema='"$(datasrc)"' $(logInstallRoot)
245 4413 aaronmk
246 5795 aaronmk
cleanup = $(if $(wildcard $*/cleanup.sql),($(inDatasrc); cat $*/cleanup.sql)\
247 6186 aaronmk
|"time" $(psqlNoSearchPath) --echo-all --set=table='"$*"' $(logInstall*Add),\
248
(prefix=; . $(bin)/vegbien_dest; unset schemas; env schema=$(datasrc) table=$*\
249
$(bin)/csv2db) $(logInstall*Add))
250 4449 aaronmk
251
define exportHeader
252
$(cleanup)
253 6186 aaronmk
echo 'SELECT * FROM "$(datasrc)"."$*" LIMIT 0;'|$(psqlNoSearchPath) \
254 4449 aaronmk
--no-align --field-separator=, --pset=footer=off >$*/header.csv
255
endef
256
257 4260 aaronmk
# For staging tables which are derived by joining together other staging tables.
258 4263 aaronmk
%/install %/header.csv: %/create.sql _always
259 4527 aaronmk
	($(inDatasrc); echo 'CREATE TABLE "$*" AS'; cat $<; echo ';')|"time" \
260 6186 aaronmk
$(psqlNoSearchPath) --echo-all --set=schema='"$(datasrc)"' --set=table='"$*"' \
261 5912 aaronmk
$(logInstall*)
262 4449 aaronmk
	$(exportHeader)
263 5441 aaronmk
.PRECIOUS: %/header.csv
264 4260 aaronmk
265 4252 aaronmk
%/install: _always
266 4456 aaronmk
	$(if $(isRef),$(exportHeader),$(if $(nonXml),$(import_install_)))
267 6078 aaronmk
	$(if $(wildcard $*/postprocess.sql),($(inDatasrc); cat $*/postprocess.sql;)\
268 6186 aaronmk
|"time" $(psqlNoSearchPath) --echo-all --set=table='"$*"' $(logInstall*Add))
269 5693 aaronmk
define import_install_
270 6186 aaronmk
(prefix=; . $(bin)/vegbien_dest; unset schemas; "time" nice -n +5\
271 5028 aaronmk
env schema=$(datasrc) table=$* $(bin)/csv2db $(catSrcs) $(logInstall*))
272 5693 aaronmk
$(if $(filter $(srcTable),$*),($(inDatasrc);\
273
echo 'ALTER TABLE "$(datasrc)"."$*" RENAME row_num TO "$*.row_num";')|"time"\
274 6186 aaronmk
$(psqlNoSearchPath) --echo-all --set=table='"$*"' $(logInstall*Add))
275 5693 aaronmk
endef
276 5697 aaronmk
# table-scope src table's row_num col to allow joining it with other tables
277 1921 aaronmk
278 5819 aaronmk
%/uninstall: _always
279 6186 aaronmk
	echo 'DROP TABLE IF EXISTS "$(datasrc)"."$*" CASCADE;'|$(psqlNoSearchPath)
280 5819 aaronmk
281
%/reinstall: _always %/uninstall %/install ;
282
283 5155 aaronmk
cleanup: _always $(tables:%=%/cleanup) ;
284
285 5184 aaronmk
# WARNING: This removes any index comments, due to a PostgreSQL bug.
286
# This occurs because ALTER TABLE recreates the index but not its comment.
287 5155 aaronmk
%/cleanup: _always
288
	$(cleanup)
289
290 1777 aaronmk
##### Maps building
291
292 4345 aaronmk
# WARNING: You CANNOT make a subdir using `make inputs/<datasrc>/<subdir>/`.
293
# You must instead make the entire datasource dir: `make inputs/<datasrc>/`
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)