Project

General

Profile

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

    
3
# Make
4
addBeforeExt = $(basename $(2))$(1)$(suffix $(2))
5

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

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

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

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

    
20
# Config
21
test_n ?= 2
22

    
23
#####
24

    
25
all: _always maps import verify
26

    
27
.SUFFIXES:
28

    
29
_always:
30
.PHONY: _always
31

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

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

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

    
42
#####
43

    
44
reinstall: _always uninstall install
45

    
46
#####
47

    
48
maps: $(vegbienMaps) _always
49

    
50
all += $(vegbienMaps)
51

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

    
56
#####
57

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

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

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

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

    
67
#####
68

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

    
71
verify-%: verify.%.ref verify.%.out _always
72
	$(DIFF)
73

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

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

    
80
#####
81

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

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

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

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

    
92
#####
93

    
94
ifdef db
95

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

    
98
####
99

    
100
ifeq ($(dbEngine),MySQL)
101

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

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

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

    
116
###
117

    
118
install: _always db
119

    
120
uninstall: _always rm_db
121

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

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

    
132
###
133

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

    
138
####
139

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

    
144
#####
145

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

    
150
#####
151

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