Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 880 | Go to most recent revision | 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
 
890 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
890 raphael 40
isfan ?= 2013
828 raphael 41
bdtfx_table = bdtfx_v$(bdtfx)
829 raphael 42
bdtxa_table = bdtxa_v$(bdtxa)
890 raphael 43
isfan_table = isfan_v$(isfan)
828 raphael 44
 
890 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
890 raphael 63
is_table = $(mysqlcmd) -N $(1) <<<"DESC $(2)" &> /dev/null
822 raphael 64
 
829 raphael 65
# macro utilisable pour effectuer des substitutions:
890 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' \
890 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' \
890 raphael 75
		   -e 's/TABLEISFAN/`$(isfan_table)`/g' \
829 raphael 76
		   $(1)
823 raphael 77
 
890 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
890 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'  \
890 raphael 90
		   -e 's/`TABLEISFAN`/`$(isfan_table)`/g'  \
816 raphael 91
		   $(fichiers)
890 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` \
890 raphael 97
			`grep ^TABLE_BDTXA .current|cut -d '=' -f2` \
98
			`grep ^TABLE_ISFAN .current|cut -d '=' -f2`
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
 
890 raphael 110
# mise à jour de septembre 2013
822 raphael 111
# spécifier dans les targets l'ordre (cf A_LIRE.txt)
829 raphael 112
# attention, si un prérequis ne génère pas de SQL, cela n'empêchera pas le fichier
113
# final de maj d'être généré (bien,
890 raphael 114
maj1: compile cel_references maj-struct-201307 maj-cleanup-201307 fix-utilisateur-32 dedup-ordre-201307 maj-referentiel-201307
115
	echo done
821 raphael 116
 
829 raphael 117
o_maj1: fichiers_generes = $(addsuffix .comp.sql,$(filter-out clean,$?))
890 raphael 118
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 119
	cat $(fichiers_generes) > maj1.comp.sql
890 raphael 120
	echo done
822 raphael 121
 
829 raphael 122
### fin: mises à jour
822 raphael 123
 
829 raphael 124
 
125
### tools
126
 
127
check_cel_obs:
128
	$(call is_table,$(alterdb),cel_obs)
129
 
130
### fin: tools
131
 
132
 
133
### mises à jour individuelles (scripts)
134
### pour chacun d'entre-eux, deux versions existent,
135
### 1) L'un compile (après substitution des noms dans le fichier SQL original)
136
### et pipe vers mysql directement, ce qui suppose aussi un .my.cnf ou autre
137
### 2) L'autre (préfixé par o_), renvoie le fichier substitué en sortie standard
138
### et le target principal s'occupe de concaténer et de créer un fichier de destination
890 raphael 139
### Cette méthode est de loin préférable et conforme à la philosophie Makefile
821 raphael 140
cel_references:
829 raphael 141
	$(call is_table,$(sourcedb),$(bdtfx_table))
890 raphael 142
	$(call is_table,$(sourcedb),nvjfl_v2007)
143
	$(call is_table,$(sourcedb),nva_index_v2_03)
144
	$(call is_table,$(alterdb),cel_references) || $(mysqlcmd) < cel_references.sql
829 raphael 145
o_cel_references:
146
	$(call is_table,$(sourcedb),$(bdtfx_table))
890 raphael 147
	$(call is_table,$(sourcedb),nvjfl_v2007)
148
	$(call is_table,$(sourcedb),nva_index_v2_03)
829 raphael 149
	$(call is_table,$(alterdb),cel_references) || $(call do_subst,cel_references.sql) > $@.comp.sql
822 raphael 150
 
829 raphael 151
maj-struct-201307: check_cel_obs
823 raphael 152
	$(call is_table,$(annuairedb),annuaire_tela)
890 raphael 153
	$(mysqlcmd) -N $(alterdb) <<<"DESC cel_obs nom_sel"|grep -q 601 || $(mysqlcmd) < maj-struct-201307.sql
829 raphael 154
o_maj-struct-201307: check_cel_obs
155
	$(call is_table,$(annuairedb),annuaire_tela)
890 raphael 156
	$(mysqlcmd) -N $(alterdb) <<<"DESC cel_obs nom_sel"|grep -q 601 || $(call do_subst,maj-struct-201307.sql) > $@.comp.sql
822 raphael 157
 
829 raphael 158
maj-cleanup-201307: check_cel_obs
890 raphael 159
	! $(mysqlcmd) -N $(alterdb) <<<"SELECT 1 FROM cel_obs WHERE nom_ret = 'null' LIMIT 1"|grep -q 1 || $(mysqlcmd) < maj-cleanup-201307.sql
829 raphael 160
o_maj-cleanup-201307:
890 raphael 161
#	tb_cel_test clean
162
#	 ! $(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
163
	$(call do_subst,maj-cleanup-201307.sql) > $@.comp.sql
822 raphael 164
 
829 raphael 165
fix-utilisateur-32: check_cel_obs
890 raphael 166
	$(mysqlcmd) -N $(alterdb) <<<"DESC cel_obs ce_utilisateur"|grep -q 255 || $(mysqlcmd) < fix-utilisateur-32.sql
829 raphael 167
o_fix-utilisateur-32: check_cel_obs
890 raphael 168
	$(mysqlcmd) -N $(alterdb) <<<"DESC cel_obs ce_utilisateur"|grep -q 255 || $(call do_subst,fix-utilisateur-32.sql) > $@.comp.sql
822 raphael 169
 
829 raphael 170
dedup-ordre-201307: check_cel_obs
890 raphael 171
#$(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
172
	$(mysqlcmd) -N $(alterdb) <<<"SHOW INDEX FROM cel_obs"|grep -q couple_user_ordre || $(mysqlcmd) < dedup-ordre-201307.sql
829 raphael 173
o_dedup-ordre-201307: check_cel_obs
890 raphael 174
	$(mysqlcmd) -N $(alterdb) <<<"SHOW INDEX FROM cel_obs"|grep -q couple_user_ordre || $(call do_subst,dedup-ordre-201307.sql) > $@.comp.sql
822 raphael 175
 
890 raphael 176
# maj-referentiel-201307.sql: # pas de test aisé et rapide
177
# doit passer APRÈS o_maj-cleanup-201307 (pas de nom_ret_nn = 0)
178
o_maj-referentiel-201307: check_cel_obs
179
	$(call do_subst,maj-referentiel-201307.sql) > $@.comp.sql
822 raphael 180
 
890 raphael 181
# pas de test aisé non plus pour savoir s'il doit repasser
182
# néanmoins c'est un script sur (peut-être invoqué répétivement)
183
o_referonosaure: check_cel_obs
184
	$(call do_subst,referonosaure.sql) > $@.comp.sql
829 raphael 185
 
186
 
890 raphael 187
 
822 raphael 188
# pour une prochaine maj
821 raphael 189
maj-nom-ret:
890 raphael 190
	$(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
191
o_maj-nom-ret:
192
	$(call do_subst,maj-nom-ret.sql) > $@.comp.sql