source: trunk/admin/aggiornadb.php@ 357

Last change on this file since 357 was 357, checked in by roby, 3 years ago

admin: funzioni di controllo del db e suoi aggiornamenti.

File size: 10.2 KB
RevLine 
[296]1<?php
[324]2
[338]3#implementare controllo con: SHOW INDEX FROM tua_tabella WHERE tua_tabella.tua_colonna='nome_colonna';
[296]4@require_once("config.php");
5 try{
6 $dbi = new PDO("mysql:host=$dbhost;charset=latin1", $dbuname, $dbpass, array(PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION));
7 $sql = "use $dbname";
8 $dbi->exec($sql);
9 }
10 catch(PDOException $e)
11 {
12 die( $sql . "<br>" . $e->getMessage());
13 }
14###############
[357]15function aggiorna($sql,$dbi,$sql2,$num){
[330]16 $ret=0;
[296]17 try{
18 $res = $dbi->prepare("$sql");
19 $res->execute();
[330]20 $ret= 1;
[296]21 }
22 catch(PDOException $e)
23 {
[330]24 $ret=0;
[357]25 echo "<br>".$num.") Fallito: $sql";
26 return $ret;
[330]27 }
28
29 if("$sql2"!=""){
30 try{
31 $res = $dbi->prepare("$sql2");
32 $res->execute();
33 $ret=2;
34 }
35 catch(PDOException $e)
36 {
37 $ret=0;
[357]38 echo "<br>".$num.") Fallito: $sql2";
39 return $ret;
[330]40 }
[296]41 }
[357]42 echo "<br>".$num.") Aggiornato<br>";
[330]43 return $ret;
[324]44}
[357]45function aggiorna_index($sql,$dbi,$sql2,$num){
46 $ret=0;
47 try{
48 $res = $dbi->prepare("$sql");
49 $res->execute();
50 $ret= 1;
51 }
52 catch(PDOException $e)
53 {
54 $ret=0;
55 echo "<br>".$num.") Fallito: $sql";
56 return $ret;
57 }
[324]58
[357]59 if("$sql2"!=""){
60 try{
61 $res = $dbi->prepare("$sql2");
62 $res->execute();
63 $ret=2;
64 }
65 catch(PDOException $e)
66 {
67 $ret=0;
68 echo "<br>".$num.") Fallito: $sql2";
69 return $ret;
70 }
71 }
72 echo "<br>".$num.") Index aggiornato<br>";
73 return $ret;
74}
[324]75
[357]76
77$num=0;
78
79function controllo($tabella,$campo,$num)
80{
81 global $dbi, $dbname;
82 $sql="SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '$dbname' AND TABLE_NAME = '$tabella'";
83 $res = $dbi->prepare("$sql");
84 $res->execute();
85 if($res->rowCount() and $campo=='') return 1;
86 while(list($nome)=$res->fetch(PDO::FETCH_NUM)) {if($nome==$campo) { echo "<br>".$num.") Aggiornamento già effettuato<br>"; return 1;}}
87 return 0;
88}
89
[324]90$sql="SELECT * FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = '$dbname' AND COLUMN_NAME = 'Versione'";
[357]91$res = $dbi->prepare("$sql");
92$res->execute();
93
94if(!controllo($prefix.'_config','versione',++$num))
[324]95{
[357]96 $sql="alter table `".$prefix."_config` change column `Versione` `versione` int(3)";
97 $ret=aggiorna($sql,$dbi,'',$num);
[296]98}
99
[357]100if(!controllo($prefix.'_ele_gruppo','num_circ',++$num))
101{
102 $sql="ALTER TABLE `".$prefix."_ele_gruppo` ADD `num_circ` INT(2) UNSIGNED NOT NULL AFTER `id_circ`";
103 $ret=aggiorna($sql,$dbi,'',$num);
104# if(!$ret) echo "<br>".$num.") Fallito: $sql"; else echo "<br>".$num.") Aggiornato<br>";
105}
106
107if(!controllo($prefix.'_ele_voti_gruppo','num_gruppo',++$num))
108{
109 $sql="ALTER TABLE `".$prefix."_ele_voti_gruppo` ADD `num_gruppo` INT(2) UNSIGNED NOT NULL AFTER `id_sez`";
110 $ret=aggiorna($sql,$dbi,'',$num);
111}
112
113if(!controllo($prefix.'_ele_lista','num_gruppo',++$num))
114{
115 $sql="ALTER TABLE `".$prefix."_ele_lista` ADD `num_gruppo` INT(2) UNSIGNED NOT NULL AFTER `id_gruppo`";
116 $ret=aggiorna($sql,$dbi,'',$num);
117}
118
119if(!controllo($prefix.'_ele_lista','num_circ',++$num))
120{
121 $sql="ALTER TABLE `".$prefix."_ele_lista` ADD `num_circ` INT(2) UNSIGNED NOT NULL AFTER `id_circ`";
122 $ret=aggiorna($sql,$dbi,'',$num);
123}
124
125if(!controllo($prefix.'_ele_voti_lista','num_lista',++$num))
126{
127 $sql="ALTER TABLE `".$prefix."_ele_voti_lista` ADD `num_lista` INT(2) UNSIGNED NOT NULL AFTER `id_sez`";
128 $ret=aggiorna($sql,$dbi,'',$num);
129}
130
131if(!controllo($prefix.'_ele_voti_ref','num_gruppo',++$num))
132{
133 $sql="ALTER TABLE `".$prefix."_ele_voti_ref` ADD `num_gruppo` INT(2) UNSIGNED NOT NULL AFTER `id_sez`";
134 $ret=aggiorna($sql,$dbi,'',$num);
135}
136
137if(!controllo($prefix.'_ele_voti_candidati','num_cand',++$num))
138{
139 $sql="ALTER TABLE `".$prefix."_ele_voti_candidati` ADD `num_cand` INT(2) UNSIGNED NOT NULL AFTER `id_sez`";
140 $ret=aggiorna($sql,$dbi,'',$num);
141}
142
143if(!controllo($prefix.'_ele_candidati','num_lista',++$num))
144{
145 $sql="ALTER TABLE `".$prefix."_ele_candidati` ADD `num_lista` INT(2) UNSIGNED NOT NULL AFTER `id_lista`";
146 $ret=aggiorna($sql,$dbi,'',$num);
147}
148
149if(!controllo($prefix.'_ele_sezioni','colore',++$num))
150{
151 $sql="ALTER TABLE `".$prefix."_ele_sezioni` ADD `colore` VARCHAR(50) NULL AFTER `solo_lista`";
152 $ret=aggiorna($sql,$dbi,$sql2,$num);
153}
154echo "<br>Aggiornamento per nuovo sistema dei controlli di congruità";
155
156if(!controllo($prefix.'_ele_controlli','id_cons',++$num))
157{
158 $sql="CREATE TABLE if not exists`".$prefix."_ele_controlli` ( `id_cons` INT(11) NOT NULL , `id_sez` INT(11) NOT NULL , `tipo` VARCHAR(10) NOT NULL , `id` INT(11) NOT NULL , INDEX `sezione` (`id_sez`)) ENGINE = MyISAM";
159 echo "<br>".$num.") Creazione tabella dei controlli: ";
160 $ret=aggiorna($sql,$dbi,'',$num);
161}
162
163echo "<br>Eliminazione della vecchia tabella dei controlli: ";
164if(controllo($prefix.'_ele_controllisez','',++$num))
165{
166$sql="DROP TABLE if exists `".$prefix."_ele_controllisez`";
167$ret=aggiorna($sql,$dbi,'',$num);
168} else echo "<br>$num) Tabella non presente<br>";
169
170$sql="update `".$prefix."_ele_voti_gruppo` as t1 left join `".$prefix."_ele_gruppo` as t2 on t1.id_gruppo=t2.id_gruppo set t1.num_gruppo=t2.num_gruppo;";
171$ret=aggiorna($sql,$dbi,'',++$num);
172
173$sql="update `".$prefix."_ele_voti_lista` as t1 left join `".$prefix."_ele_lista` as t2 on t1.id_lista=t2.id_lista set t1.num_lista=t2.num_lista;";
174$ret=aggiorna($sql,$dbi,'',++$num);
175##############################################
176
177$sql="ALTER TABLE `".$prefix."_ele_gruppo` CHANGE `num_circ` `num_circ` INT(2) UNSIGNED NOT NULL DEFAULT '1';";
178$ret=aggiorna($sql,$dbi,'',++$num);
179
180$sql="ALTER TABLE `".$prefix."_ele_lista` CHANGE `num_gruppo` `num_gruppo` INT(2) UNSIGNED NOT NULL DEFAULT '0';";
181$ret=aggiorna($sql,$dbi,'',++$num);
182
183$sql="ALTER TABLE `".$prefix."_ele_lista` CHANGE `num_circ` `num_circ` INT(2) UNSIGNED NOT NULL DEFAULT '1';";
184$ret=aggiorna($sql,$dbi,'',++$num);
185
186$sql="DROP TABLE `".$prefix."_ele_fasce`";
187$ret=aggiorna($sql,$dbi,'',++$num);
188
189$sql="CREATE TABLE `".$prefix."_ele_fasce` (
190 `id_fascia` int(2) NOT NULL,
191 `abitanti` int(11) NOT NULL,
192 `seggi` int(4) NOT NULL,
193 `id_conf` int(11) DEFAULT 1
194) ENGINE=MyISAM DEFAULT CHARSET=latin1;";
195
196
197$sql2="INSERT INTO `".$prefix."_ele_fasce` (`id_fascia`, `abitanti`, `seggi`, `id_conf`) VALUES
198(1, 3000, 12, 1),
199(2, 10000, 16, 1),
200(3, 15000, 20, 1),
201(4, 30000, 20, 1),
202(5, 100000, 30, 1),
203(6, 250000, 40, 1),
204(7, 500000, 46, 1),
205(8, 1000000, 50, 1),
206(9, 100000000, 60, 1),
207(1, 3000, 12, 2),
208(2, 10000, 16, 2),
209(3, 15000, 20, 2),
210(4, 30000, 20, 2),
211(5, 100000, 30, 2),
212(6, 250000, 40, 2),
213(7, 500000, 46, 2),
214(8, 1000000, 50, 2),
215(9, 100000000, 60, 2),
216(1, 3000, 9, 3),
217(2, 5000, 9, 3),
218(4, 15000, 16, 3),
219(3, 10000, 12, 3),
220(5, 30000, 16, 3),
221(6, 100000, 24, 3),
222(7, 250000, 32, 3),
223(8, 500000, 36, 3),
224(9, 1000000, 40, 3),
225(10, 100000000, 48, 3),
226(1, 3000, 6, 4),
227(2, 5000, 7, 4),
228(3, 10000, 12, 4),
229(4, 15000, 16, 4),
230(5, 30000, 16, 4),
231(6, 100000, 24, 4),
232(7, 250000, 32, 4),
233(8, 500000, 36, 4),
234(9, 1000000, 40, 4),
235(10, 100000000, 48, 4),
236(1, 3000, 9, 5),
237(2, 5000, 9, 5),
238(3, 10000, 12, 5),
239(4, 15000, 16, 5),
240(5, 30000, 16, 5),
241(6, 100000, 24, 5),
242(7, 250000, 32, 5),
243(8, 500000, 36, 5),
244(9, 1000000, 40, 5),
245(10, 100000000, 48, 5),
246(1, 3000, 6, 6),
247(2, 5000, 7, 6),
248(3, 10000, 10, 6),
249(4, 15000, 16, 6),
250(5, 30000, 16, 6),
251(6, 100000, 24, 6),
252(7, 250000, 32, 6),
253(8, 500000, 36, 6),
254(9, 1000000, 40, 6),
255(10, 100000000, 48, 6),
256(1, 3000, 10, 7),
257(2, 10000, 12, 7),
258(3, 15000, 16, 7),
259(4, 30000, 16, 7),
260(5, 100000, 24, 7),
261(6, 250000, 32, 7),
262(7, 500000, 36, 7),
263(8, 1000000, 40, 7),
264(9, 100000000, 48, 7);";
265$ret=aggiorna($sql,$dbi,$sql2,++$num);
[324]266#if(!$ret) echo "<br>".$num++.") Fallito: $sql"; else echo "<br>".$num++.") Aggiornato<br>";
[296]267
[357]268$sql="ALTER TABLE `".$prefix."_ele_fasce`
269 ADD KEY `id_fascia` (`id_fascia`);";
270$ret=aggiorna($sql,$dbi,'',++$num);
271#if(!$ret) echo "<br>".$num++.") Fallito: $sql"; else echo "<br>".$num++.") Aggiornato<br>";
272
273
274 $sql="ALTER TABLE `".$prefix."_ele_voti_ref` DROP INDEX `id_cons`; ";
275 $sql2="ALTER TABLE `".$prefix."_ele_voti_ref` ADD INDEX `id_cons` (`id_cons`, `id_gruppo`) USING BTREE";
276 $ret=aggiorna_index($sql,$dbi,$sql2,++$num);
277
278#if(!$ret)
279# echo "<br>".$num++.") Fallito: $sql";
280#elseif ($ret==1)
281# echo "<br>".$num++.") Index eliminato";
282#else
283# echo "<br>".$num++.") Index aggiunto";
284$sql="ALTER TABLE `".$prefix."_ele_voti_lista` DROP INDEX `id_cons`";
285$sql2="ALTER TABLE `".$prefix."_ele_voti_lista` ADD INDEX `id_cons` (`id_cons`, `id_sez`, `id_lista`) USING BTREE";
286$ret=aggiorna_index($sql,$dbi,$sql2,++$num);
287
288$sql="ALTER TABLE `".$prefix."_ele_voti_gruppo` DROP INDEX `id_cons`";
289$sql2="ALTER TABLE `".$prefix."_ele_voti_gruppo` ADD INDEX `id_cons` (`id_cons`, `id_sez`, `id_gruppo`) USING BTREE";
290$ret=aggiorna_index($sql,$dbi,$sql2,++$num);
291
292$sql="ALTER TABLE `".$prefix."_ele_voti_candidati` DROP INDEX `id_cons`";
293$sql2="ALTER TABLE `".$prefix."_ele_voti_candidati` ADD INDEX `id_cons` (`id_cons`, `id_sez`, `id_cand`) USING BTREE";
294$ret=aggiorna_index($sql,$dbi,$sql2,++$num);
295
296$sql="ALTER TABLE `".$prefix."_ele_lista` DROP INDEX `id_cons`";
297$sql2="ALTER TABLE `".$prefix."_ele_lista` ADD INDEX `id_cons` (`id_cons`, `id_gruppo`) USING BTREE";
298$ret=aggiorna_index($sql,$dbi,$sql2,++$num);
299
300$sql="ALTER TABLE `".$prefix."_ele_lista` DROP INDEX `PRIMARY`";
301$sql2="ALTER TABLE `".$prefix."_ele_lista` ADD PRIMARY KEY (`id_lista`) USING BTREE";
302$ret=aggiorna_index($sql,$dbi,$sql2,++$num);
303
304$sql="ALTER TABLE `".$prefix."_ele_gruppo` DROP INDEX `id_cons`";
305$sql2="ALTER TABLE `".$prefix."_ele_gruppo` ADD INDEX `id_cons` (`id_cons`, `id_circ`) USING BTREE";
306$ret=aggiorna_index($sql,$dbi,$sql2,++$num);
307
308$sql="ALTER TABLE `".$prefix."_ele_gruppo` DROP INDEX `PRIMARY`";
309$sql2="ALTER TABLE `".$prefix."_ele_gruppo` ADD PRIMARY KEY (`id_gruppo`) USING BTREE";
310$ret=aggiorna_index($sql,$dbi,$sql2,++$num);
311
312$sql="ALTER TABLE `".$prefix."_ele_candidati` DROP INDEX `id_cons`";
313$sql2="ALTER TABLE `".$prefix."_ele_candidati` ADD INDEX `id_cons` (`id_cons`, `id_lista`) USING BTREE";
314$ret=aggiorna_index($sql,$dbi,$sql2,++$num);
315
316$sql="ALTER TABLE `".$prefix."_ele_candidati` DROP INDEX `PRIMARY`";
317$sql2="ALTER TABLE `".$prefix."_ele_candidati` ADD PRIMARY KEY (`id_cand`) USING BTREE";
318$ret=aggiorna_index($sql,$dbi,$sql2,++$num);
319
320
[296]321?>
Note: See TracBrowser for help on using the repository browser.