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
#####
21

    
22
all: _always maps import verify
23

    
24
.SUFFIXES:
25

    
26
_always:
27
.PHONY: _always
28

    
29
clean: _always
30
	$(RM) $(all)
31

    
32
rm-%: _always
33
	$(RM) $*
34

    
35
%.out: % _always
36
	./$* >$@
37
.PRECIOUS: %.out
38

    
39
#####
40

    
41
reinstall: _always uninstall install
42

    
43
#####
44

    
45
maps: $(vegbienMaps) _always
46

    
47
all += $(vegbienMaps)
48

    
49
map.VegBIEN.%.csv: map.VegX.%.csv
50
	$(root)/bin/join_sort <$< $(root)/mappings/VegX-VegBIEN.$(table).csv >$@
51
.PRECIOUS: map.VegBIEN.%.csv
52

    
53
#####
54

    
55
import: _always $(addprefix import-,$(tables))
56

    
57
log = $*$(if $(n),.n=$(n),).log
58

    
59
logs := $(wildcard *.log)
60

    
61
rm_logs: _always
62
	$(RM) $(logs)
63

    
64
#####
65

    
66
verify: _always $(addprefix verify-,$(tables))
67

    
68
verify-%: verify.%.ref verify.%.out _always
69
	$(DIFF)
70

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

    
75
all += $(wildcard *.out)
76

    
77
#####
78

    
79
test: _always $(addprefix test-,$(tables))
80

    
81
test-%: test.%.out _always
82
	$(DIFF) $(<:.out=.ref) $<
83

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

    
86
accept-%: _always
87
	$(CP) $* $(*:.out=.ref)
88

    
89
#####
90

    
91
ifdef db
92

    
93
mapEnv := in_engine=$(dbEngine) in_database=$(db)
94

    
95
####
96

    
97
ifeq ($(dbEngine),MySQL)
98

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

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

    
109
%.ref: %.ref.sql
110
	$(mysqlAsBien) $(db) <$< >$@
111
.PRECIOUS: %.ref
112

    
113
###
114

    
115
install: _always db
116

    
117
uninstall: _always rm_db
118

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

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

    
129
###
130

    
131
else
132
$(error dbEngine variable must be one of the following: MySQL, PostgreSQL)
133
endif
134

    
135
####
136

    
137
test.%.out: map.VegBIEN.%.csv _always
138
	env n=2 $(map) $< >$@ 2>&1
139
.PRECIOUS: test.%.out
140

    
141
#####
142

    
143
else
144
$(error db variable must be set)
145
endif
146

    
147
#####
148

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