Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 892 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
821 raphael 1
# Ce Makefile effectue les substitutions de nom de base de données
2
# nécessaire au préalable de l'application des scripts SQL
3
 
4
# Cela est d'un part moins complexe:
5
# - qu'un script PHP (interpréteur, getopt, framework, ...)
6
# - qu'un shell-script (lancement avec make)
7
# et d'autre part plus maintenable qu'un shell-script car
8
# le versionnage des fichiers (inc ".current") permet certaines facilités.
9
 
10
 
11
# TODO:
12
# idéalement, ce Makefile devrait permettre une bonne gestion du jeu de dépendances
13
# entre les scripts, seulement le lancement d'un script pouvant nécessiter un login/mdp
14
# il est difficile de vouloir rester "simple".
822 raphael 15
# Ce serait cependant la meilleure manière de procéder, ainsi "maj2" ne serait lancé qu'en
16
# cas de succès de "maj1", celui-ci pouvant être détecté comme "déjà exécuté" ou non.
17
# cf target "maj1" ci-dessous
821 raphael 18
 
19
 
882 raphael 20
# à l'aide de, note certains de ces fichiers n'ont pas cours dans le cadre de la maj1 (septembre 2013)
816 raphael 21
# echo $(egrep -l 'BASE(SOURCE|EDIT|ANNUAIRE)' *.sql)
823 raphael 22
fichiers = cel_references.sql dedup-ordre-201307.sql fix-utilisateur-32.sql maj-cleanup-201307.sql maj-nom-ret.sql \
23
		   maj-referentiel-201307.sql maj-referentiel-und-201307.sql maj-struct-201307.sql redempteur.sql \
24
		   referonosaure.sql \
25
		   .current
816 raphael 26
 
27
# la base de données à modifier
28
alterdb ?= tb_cel_test
29
 
829 raphael 30
# pour bdtfx, bdtxa, isfan, nvjfl, nva, baseflor, ... lecture seule;
816 raphael 31
# utilisée pour actualiser les enregistrements de cel_obs dans referonosaure.sql
32
sourcedb ?= tb_eflore
33
 
34
# pour annuaire_tela, lecture seule;
35
# utilisée pour initialiser cel_utilisateurs dans maj-struct-201307.sql
36
annuairedb ?= tela_prod_v4
37
 
828 raphael 38
bdtfx ?= 1_01
39
bdtxa ?= 1_00
882 raphael 40
isfan ?= 2013
828 raphael 41
bdtfx_table = bdtfx_v$(bdtfx)
829 raphael 42
bdtxa_table = bdtxa_v$(bdtxa)
882 raphael 43
isfan_table = isfan_v$(isfan)
828 raphael 44
 
885 raphael 45
# TODO: simply override bdd_user
46
ifdef bdd_user
47
	bdd_user_h = -u$(bdd_user)
48
endif
49
 
50
ifneq ($(origin bdd_pass), undefined)
51
	bdd_pass_h = "-p$(bdd_pass)"
52
endif
53
 
54
mysqlbin ?= mysql
55
mysqlcmd = $(mysqlbin) $(bdd_user_h) $(bdd_pass_h)
56
 
823 raphael 57
# macro utilisable pour les targets nécessitant de tester la présence d'un couple (base,table)
58
# exemples:
59
# * $(call is_table,tb_eflore,bdtfx_v1_01)
60
# * $(call is_table,$(annuairedb),annuaire_tela)
61
# argument 1: base de données
62
# argument 2: table
885 raphael 63
is_table = $(mysqlcmd) -N $(1) <<<"DESC $(2)" &> /dev/null
822 raphael 64
 
829 raphael 65
# macro utilisable pour effectuer des substitutions:
882 raphael 66
do_subst = sed -e "1i--\n-- fichier d'origine: \"${1}\"\n" \
67
		   -e 's/`BASEEDIT`/`$(alterdb)`/g' \
829 raphael 68
		   -e 's/`BASEANNUAIRE`/`$(annuairedb)`/g' \
69
		   -e 's/`BASESOURCE`/`$(sourcedb)`/g' \
882 raphael 70
		   -e 's/`TABLEBDTFX`/`$(bdtfx_table)`/g' \
71
		   -e 's/`TABLEBDTXA`/`$(bdtxa_table)`/g' \
72
		   -e 's/`TABLEISFAN`/`$(isfan_table)`/g' \
829 raphael 73
		   -e 's/TABLEBDTFX/`$(bdtfx_table)`/g' \
74
		   -e 's/TABLEBDTXA/`$(bdtxa_table)`/g' \
882 raphael 75
		   -e 's/TABLEISFAN/`$(isfan_table)`/g' \
829 raphael 76
		   $(1)
823 raphael 77
 
885 raphael 78
# default target
79
help:
80
	@echo "make [alterdb=<$(alterdb)>] [sourcedb=<$(sourcedb)>] [annuairedb=<$(annuairedb)>] [bdtfx=<$(bdtfx)>] [bdtxa=<$(bdtxa)>] [isfan=$(isfan)] [bdd_user=\"\"] [bdd_pass=\"\"] [mysqlbin=mysql]"
81
	@echo "make o_maj1 mysqlbin=/usr/local/mysql/bin/mysql bdd_user=telabotap bdd_pass=XXX"
82
 
822 raphael 83
# génère les fichiers avec les bases de données souhaitées
84
compile: reset
885 raphael 85
	sed -i -e 's/`BASEEDIT`/`$(alterdb)`/g'  \
820 raphael 86
		   -e 's/`BASEANNUAIRE`/`$(annuairedb)`/g'  \
87
		   -e 's/`BASESOURCE`/`$(sourcedb)`/g'  \
829 raphael 88
		   -e 's/TABLEBDTFX/`$(bdtfx_table)`/g'  \
89
		   -e 's/TABLEBDTXA/`$(bdtxa_table)`/g'  \
882 raphael 90
		   -e 's/`TABLEISFAN`/`$(isfan_table)`/g'  \
816 raphael 91
		   $(fichiers)
885 raphael 92
	printf "Attention: les changements s'appliqueront sur la base \"%s\"\nLes sources utilisées seront: annuaire=\"%s\" ,  sources=\"%s\" (%s,%s,%s)\n(Ctrl+C pour interrompre, Enter pour continuer)\n" \
822 raphael 93
			`grep ^BASEEDIT .current|cut -d '\`' -f2` \
94
			`grep ^BASEANNUAIRE .current|cut -d '\`' -f2` \
828 raphael 95
			`grep ^BASESOURCE .current|cut -d '\`' -f2` \
96
			`grep ^TABLE_BDTFX .current|cut -d '=' -f2` \
882 raphael 97
			`grep ^TABLE_BDTXA .current|cut -d '=' -f2` \
98
			`grep ^TABLE_ISFAN .current|cut -d '=' -f2`
885 raphael 99
	read
820 raphael 100
 
101
reset:
829 raphael 102
	svn revert -q $(fichiers)
820 raphael 103
 
829 raphael 104
# supprime les fichiers "compilés" (concaténation de plusieurs scripts SQL substitués)
105
clean:
106
	rm -f *.comp.sql
823 raphael 107
 
829 raphael 108
### mises à jour
109
 
882 raphael 110
# mise à jour de septembre 2013
892 raphael 111
# spécifier les targets dans l'ordre (cf A_LIRE.txt)
112
 
113
 
114
# première version: substitution des fichiers: pas bon
829 raphael 115
# attention, si un prérequis ne génère pas de SQL, cela n'empêchera pas le fichier
892 raphael 116
# final de maj d'être généré,
117
#maj1: compile cel_references maj-struct-201307 maj-cleanup-201307 fix-utilisateur-32 dedup-ordre-201307 maj-referentiel-201307
118
#	echo done
821 raphael 119
 
829 raphael 120
o_maj1: fichiers_generes = $(addsuffix .comp.sql,$(filter-out clean,$?))
882 raphael 121
o_maj1: clean o_cel_references o_maj-struct-201307 o_maj-cleanup-201307 o_fix-utilisateur-32 o_dedup-ordre-201307 o_maj-referentiel-201307 o_referonosaure
829 raphael 122
	cat $(fichiers_generes) > maj1.comp.sql
885 raphael 123
	echo done
822 raphael 124
 
829 raphael 125
### fin: mises à jour
822 raphael 126
 
829 raphael 127
 
128
### tools
129
 
130
check_cel_obs:
131
	$(call is_table,$(alterdb),cel_obs)
132
 
133
### fin: tools
134
 
135
 
136
### mises à jour individuelles (scripts)
137
### pour chacun d'entre-eux, deux versions existent,
138
### 1) L'un compile (après substitution des noms dans le fichier SQL original)
139
### et pipe vers mysql directement, ce qui suppose aussi un .my.cnf ou autre
140
### 2) L'autre (préfixé par o_), renvoie le fichier substitué en sortie standard
141
### et le target principal s'occupe de concaténer et de créer un fichier de destination
882 raphael 142
### Cette méthode est de loin préférable et conforme à la philosophie Makefile
821 raphael 143
cel_references:
829 raphael 144
	$(call is_table,$(sourcedb),$(bdtfx_table))
882 raphael 145
	$(call is_table,$(sourcedb),nvjfl_v2007)
146
	$(call is_table,$(sourcedb),nva_index_v2_03)
885 raphael 147
	$(call is_table,$(alterdb),cel_references) || $(mysqlcmd) < cel_references.sql
829 raphael 148
o_cel_references:
149
	$(call is_table,$(sourcedb),$(bdtfx_table))
882 raphael 150
	$(call is_table,$(sourcedb),nvjfl_v2007)
151
	$(call is_table,$(sourcedb),nva_index_v2_03)
829 raphael 152
	$(call is_table,$(alterdb),cel_references) || $(call do_subst,cel_references.sql) > $@.comp.sql
822 raphael 153
 
829 raphael 154
maj-struct-201307: check_cel_obs
823 raphael 155
	$(call is_table,$(annuairedb),annuaire_tela)
885 raphael 156
	$(mysqlcmd) -N $(alterdb) <<<"DESC cel_obs nom_sel"|grep -q 601 || $(mysqlcmd) < maj-struct-201307.sql
829 raphael 157
o_maj-struct-201307: check_cel_obs
158
	$(call is_table,$(annuairedb),annuaire_tela)
885 raphael 159
	$(mysqlcmd) -N $(alterdb) <<<"DESC cel_obs nom_sel"|grep -q 601 || $(call do_subst,maj-struct-201307.sql) > $@.comp.sql
822 raphael 160
 
829 raphael 161
maj-cleanup-201307: check_cel_obs
885 raphael 162
	! $(mysqlcmd) -N $(alterdb) <<<"SELECT 1 FROM cel_obs WHERE nom_ret = 'null' LIMIT 1"|grep -q 1 || $(mysqlcmd) < maj-cleanup-201307.sql
829 raphael 163
o_maj-cleanup-201307:
892 raphael 164
# tb_cel_test clean
165
	! $(mysqlcmd) -N $(alterdb) <<<"SELECT 1 FROM cel_obs WHERE nom_ret = 'null' LIMIT 1"|grep -q 1 || $(call do_subst,maj-cleanup-201307.sql) > $@.comp.sql
822 raphael 166
 
829 raphael 167
fix-utilisateur-32: check_cel_obs
885 raphael 168
	$(mysqlcmd) -N $(alterdb) <<<"DESC cel_obs ce_utilisateur"|grep -q 255 || $(mysqlcmd) < fix-utilisateur-32.sql
829 raphael 169
o_fix-utilisateur-32: check_cel_obs
885 raphael 170
	$(mysqlcmd) -N $(alterdb) <<<"DESC cel_obs ce_utilisateur"|grep -q 255 || $(call do_subst,fix-utilisateur-32.sql) > $@.comp.sql
822 raphael 171
 
829 raphael 172
dedup-ordre-201307: check_cel_obs
885 raphael 173
#$(mysqlcmd) -N $(alterdb) <<<'SELECT distinct ce_utilisateur FROM `cel_obs` GROUP BY ce_utilisateur, ordre HAVING COUNT(*) > 1'|grep -q . || $(mysqlcmd) < dedup-ordre-201307.sql
174
	$(mysqlcmd) -N $(alterdb) <<<"SHOW INDEX FROM cel_obs"|grep -q couple_user_ordre || $(mysqlcmd) < dedup-ordre-201307.sql
829 raphael 175
o_dedup-ordre-201307: check_cel_obs
893 raphael 176
# l'index doit sur cel_obs doit avoir deux lignes dont le champs "non_unique" = 0
177
	$(mysqlcmd) -N $(alterdb) <<<"SHOW INDEX FROM cel_obs"|grep -w id_obs|awk '{print $$2}'|tr -d "\n"|grep -q 00 || $(call do_subst,dedup-ordre-201307.sql) > $@.comp.sql
822 raphael 178
 
882 raphael 179
# maj-referentiel-201307.sql: # pas de test aisé et rapide
180
# doit passer APRÈS o_maj-cleanup-201307 (pas de nom_ret_nn = 0)
181
o_maj-referentiel-201307: check_cel_obs
182
	$(call do_subst,maj-referentiel-201307.sql) > $@.comp.sql
822 raphael 183
 
882 raphael 184
# pas de test aisé non plus pour savoir s'il doit repasser
185
# néanmoins c'est un script sur (peut-être invoqué répétivement)
186
o_referonosaure: check_cel_obs
187
	$(call do_subst,referonosaure.sql) > $@.comp.sql
829 raphael 188
 
189
 
882 raphael 190
 
822 raphael 191
# pour une prochaine maj
821 raphael 192
maj-nom-ret:
885 raphael 193
	$(mysqlcmd) -N <<<'SELECT count(1) FROM `$(alterdb)`.`cel_obs` c LEFT JOIN `$(sourcedb)`.`$(bdtfx_table)` b on (c.nom_ret = b.nom_sci) WHERE nom_ret_nn = 0 AND c.nom_ret != "" AND id_observation NOT IN ( SELECT id_observation FROM `$(alterdb)`.`cel_obs` c, `$(sourcedb)`.`$(bdtfx_table)` b WHERE c.nom_ret = b.nom_sci AND c.nom_ret_nn = 0 );'|grep -q 0
882 raphael 194
o_maj-nom-ret:
195
	$(call do_subst,maj-nom-ret.sql) > $@.comp.sql