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