Project

General

Profile

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

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

    
6
# Commands
7
DIFF = diff --unified=2 $(+:_%=)
8

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

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

    
15
root := $(selfDir_3d1bc249)..
16
psqlAsBien := $(root)/bin/psql_vegbien
17

    
18
#####
19

    
20
all: _always maps import verify
21

    
22
.SUFFIXES:
23

    
24
_always:
25
.PHONY: _always
26

    
27
clean: _always
28
	$(RM) $(all)
29

    
30
rm-%: _always
31
	$(RM) $*
32

    
33
%.out: % _always
34
	./$* >$@
35
.PRECIOUS: %.out
36

    
37
#####
38

    
39
reinstall: _always uninstall install
40

    
41
#####
42

    
43
maps: $(vegbienMaps) _always
44

    
45
all += $(vegbienMaps)
46

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

    
51
#####
52

    
53
import: _always $(addprefix import-,$(tables))
54

    
55
log = $*$(if $(n),.n=$(n),).log
56

    
57
logs := $(wildcard *.log)
58

    
59
rm_logs: _always
60
	$(RM) $(logs)
61

    
62
#####
63

    
64
verify: _always $(addprefix verify-,$(tables))
65

    
66
verify-%: verify.%.ref verify.%.out _always
67
	$(DIFF)
68

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

    
73
all += $(wildcard *.out)
74

    
75
#####
76

    
77
test: _always $(addprefix test-,$(tables))
78

    
79
test-%: test.%.ref test.%.out _always
80
	$(DIFF)
81

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

    
84
#####
85

    
86
ifdef db
87

    
88
mapEnv := in_engine=$(dbEngine) in_database=$(db)
89

    
90
####
91

    
92
ifeq ($(dbEngine),MySQL)
93

    
94
bienPassword := $(shell cat $(root)/config/bien_password)
95
mysqlAs = mysql --user=$(1) --password='$(bienPassword)'
96
mysqlAsRoot := $(call mysqlAs,root)
97
mysqlAsBien := $(call mysqlAs,bien)
98

    
99
import-%: map.VegBIEN.%.csv _always
100
	-$(import_cmd)
101
import_cmd = (set -x; "time" env commit=1 $(map) $<) \
102
$(if $(n),,>>$(log)) 2>&1$(if $(n),|tee -a $(log))
103

    
104
%.ref: %.ref.sql
105
	$(mysqlAsBien) $(db) <$< >$@
106
.PRECIOUS: %.ref
107

    
108
###
109

    
110
install: _always db
111

    
112
uninstall: _always rm_db
113

    
114
db: src.$(db).sql _always
115
	-$(mysqlAsRoot) <$<
116
	echo "GRANT SELECT ON $(db).* TO 'bien'@'localhost';"|$(mysqlAsRoot)
117
# ignore errors in db import so that GRANT will still be run
118

    
119
rm_db: _always
120
	-echo "REVOKE ALL ON $(db).* FROM 'bien'@'localhost';"|$(mysqlAsRoot)
121
	echo "DROP DATABASE IF EXISTS $(db);"|$(mysqlAsRoot)
122
# ignore errors if grant not defined
123

    
124
###
125

    
126
else
127
$(error dbEngine variable must be one of the following: MySQL, PostgreSQL)
128
endif
129

    
130
####
131

    
132
test.%.out: map.VegBIEN.%.csv _always
133
	env n=2 $(map) $< >$@ 2>&1
134
.PRECIOUS: test.%.out
135

    
136
#####
137

    
138
else
139
$(error db variable must be set)
140
endif
141

    
142
#####
143

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