Project

General

Profile

1
selfDir_3d1bc249 := $(dir $(lastword $(MAKEFILE_LIST)))
2

    
3
# Make
4
+_ = $(+:_%=)
5
addBeforeExt = $(basename $(2))$(1)$(suffix $(2))
6

    
7
# Commands
8
CP = cp -p
9
copyTo = $(CP) $(+_)
10
DIFF = diff --unified=2
11

    
12
table = $(shell s="$(*F)"; echo "$${s\#\#*.}")# remove sort order prefix
13

    
14
vegxMaps := $(wildcard map.VegX.*.csv)
15
vegbienMaps := $(subst .VegX.,.VegBIEN.,$(vegxMaps))
16
tables := $(vegbienMaps:map.VegBIEN.%.csv=%)
17

    
18
root := $(selfDir_3d1bc249)..
19
psqlAsBien := $(root)/bin/psql_vegbien
20

    
21
# Config
22
test_n ?= 2
23

    
24
#####
25

    
26
all: _always maps import verify
27

    
28
.SUFFIXES:
29

    
30
_always:
31
.PHONY: _always
32

    
33
clean: _always
34
	$(RM) $(all)
35

    
36
rm-%: _always
37
	$(RM) $*
38

    
39
%.out: % _always
40
	./$* >$@
41
.PRECIOUS: %.out
42

    
43
#####
44

    
45
reinstall: _always uninstall install
46

    
47
#####
48

    
49
maps: $(vegbienMaps) _always
50

    
51
all += $(vegbienMaps)
52

    
53
map.VegBIEN.%.csv: map.VegX.%.csv
54
	$(root)/bin/join_sort <$< $(root)/mappings/VegX-VegBIEN.$(table).csv >$@
55
.PRECIOUS: map.VegBIEN.%.csv
56

    
57
#####
58

    
59
import: _always $(addprefix import-,$(tables))
60

    
61
log = $*$(if $(n),.n=$(n),).log
62

    
63
logs := $(wildcard *.log)
64

    
65
rm_logs: _always
66
	$(RM) $(logs)
67

    
68
#####
69

    
70
verify: _always $(addprefix verify-,$(tables))
71

    
72
verify-%: verify.%.ref verify.%.out _always
73
	$(DIFF) $(+_)
74

    
75
%.out: %.sql _always
76
	$(psqlAsBien) --no-align --field-separator='	' --pset=footer=off <$< >$@
77
.PRECIOUS: %.out
78

    
79
all += $(wildcard *.out)
80

    
81
#####
82

    
83
test: _always $(addprefix test-,$(tables))
84

    
85
test-%: test.%.out _always
86
	$(DIFF) $(<:.out=.ref) $<
87

    
88
all += $(filter-out %.ref,$(wildcard test.*.out))
89

    
90
accept-%: _always
91
	$(CP) $* $(*:.out=.ref)
92

    
93
#####
94

    
95
ifdef db
96

    
97
mapEnv := in_engine=$(dbEngine) in_database=$(db)
98

    
99
####
100

    
101
ifeq ($(dbEngine),MySQL)
102

    
103
bienPassword := $(shell cat $(root)/config/bien_password)
104
mysqlAs = mysql --user=$(1) --password='$(bienPassword)'
105
mysqlAsRoot := $(call mysqlAs,root)
106
mysqlAsBien := $(call mysqlAs,bien)
107

    
108
import-%: map.VegBIEN.%.csv _always
109
	-$(import_cmd)
110
import_cmd = (set -x; "time" env commit=1 $(map) $<) \
111
$(if $(n),,>>$(log)) 2>&1$(if $(n),|tee -a $(log))
112

    
113
%.ref: %.ref.sql
114
	$(mysqlAsBien) $(db) <$< >$@
115
.PRECIOUS: %.ref
116

    
117
###
118

    
119
install: _always db
120

    
121
uninstall: _always rm_db
122

    
123
db: src.$(db).sql _always
124
	-$(mysqlAsRoot) <$<
125
	echo "GRANT SELECT ON $(db).* TO 'bien'@'localhost';"|$(mysqlAsRoot)
126
# ignore errors in db import so that GRANT will still be run
127

    
128
rm_db: _always
129
	-echo "REVOKE ALL ON $(db).* FROM 'bien'@'localhost';"|$(mysqlAsRoot)
130
	echo "DROP DATABASE IF EXISTS $(db);"|$(mysqlAsRoot)
131
# ignore errors if grant not defined
132

    
133
###
134

    
135
else
136
$(error dbEngine variable must be one of the following: MySQL, PostgreSQL)
137
endif
138

    
139
####
140

    
141
test.%.out: map.VegBIEN.%.csv _always
142
	env test=1 n=$(test_n) $(map) $< >$@ 2>&1 || { e=$$?; cat $@; exit $$e;}
143
.PRECIOUS: test.%.out
144

    
145
#####
146

    
147
else
148
$(error db variable must be set)
149
endif
150

    
151
#####
152

    
153
# Must come after mapEnv is set
154
map := env $(mapEnv) out_database=vegbien $(root)/map
    (1-1/1)