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=1 $(+:_%=)
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: _not_file maps import verify
21

    
22
.SUFFIXES:
23

    
24
_not_file:
25
.PHONY: _not_file
26

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

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

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

    
37
#####
38

    
39
reinstall: _not_file uninstall install
40

    
41
#####
42

    
43
maps: $(vegbienMaps) _not_file
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: _not_file $(addprefix import-,$(tables))
54

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

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

    
59
all += $(logs)
60

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

    
64
#####
65

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

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

    
71
verify.%.out: verify.%.sql _not_file
72
	$(psqlAsBien) --no-align --tuples-only <$< >$@
73
.PRECIOUS: verify.%.out
74

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

    
77
#####
78

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

    
81
test-%: test.%.ref test.%.out _not_file
82
	$(DIFF)
83

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

    
86
#####
87

    
88
ifdef db
89

    
90
mapEnv := in_engine=$(dbEngine) in_database=$(db)
91

    
92
####
93

    
94
ifeq ($(dbEngine),MySQL)
95

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

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

    
106
all += $(wildcard *.out)
107

    
108
%.ref: %.ref.sql
109
	$(mysqlAsBien) --skip-column-names $(db) <$< >$@
110
.PRECIOUS: %.ref
111

    
112
###
113

    
114
install: _not_file db
115

    
116
uninstall: _not_file rm_db
117

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

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

    
128
###
129

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

    
134
####
135

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

    
140
#####
141

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

    
146
#####
147

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