fk_generate.php 2.47 KB
Newer Older
moyooo's avatar
moyooo committed
1
<?php
2 3 4
/**
 * ---------------------------------------------------------------------
 * GLPI - Gestionnaire Libre de Parc Informatique
5
 * Copyright (C) 2015-2018 Teclib' and contributors.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
 *
 * http://glpi-project.org
 *
 * based on GLPI - Gestionnaire Libre de Parc Informatique
 * Copyright (C) 2003-2014 by the INDEPNET Development Team.
 *
 * ---------------------------------------------------------------------
 *
 * LICENSE
 *
 * This file is part of GLPI.
 *
 * GLPI is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * GLPI is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with GLPI. If not, see <http://www.gnu.org/licenses/>.
 * ---------------------------------------------------------------------
moyooo's avatar
moyooo committed
31
 */
moyooo's avatar
moyooo committed
32

33
include ('../inc/includes.php');
moyooo's avatar
moyooo committed
34

yllen's avatar
yllen committed
35 36 37 38 39
$DB->query("SET FOREIGN_KEY_CHECKS = '0';");
$result = $DB->list_tables();
$numtab = 0;

while ($t=$DB->fetch_array($result)) {
yllen's avatar
yllen committed
40 41 42
   $query = "ALTER TABLE `$t[0]`
             TYPE = innodb";
   $DB->query($query);
moyooo's avatar
moyooo committed
43 44
}

moyooo's avatar
moyooo committed
45
$relations = getDbRelations();
moyooo's avatar
moyooo committed
46

47
$query = [];
Anael Mobilia's avatar
Anael Mobilia committed
48
foreach ($relations as $totable => $rels) {
yllen's avatar
yllen committed
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
   foreach ($rels as $fromtable => $fromfield) {

      if ($fromtable[0]=="_") {
         $fromtable = substr($fromtable, 1);
      }

      if (!is_array($fromfield)) {
         $query[$fromtable][] = " ADD CONSTRAINT `". $fromtable."_".$fromfield."`
                                  FOREIGN KEY (`$fromfield`)
                                  REFERENCES `$totable` (`id`) ";
      } else {
         foreach ($fromfield as $f) {
            $query[$fromtable][] = " ADD CONSTRAINT `".$fromtable."_".$f."`
                                     FOREIGN KEY (`$f`)
                                     REFERENCES `$totable` (`id`) ";
         }
      }

   }
moyooo's avatar
moyooo committed
68
}
moyooo's avatar
moyooo committed
69

70

yllen's avatar
yllen committed
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
foreach ($query as $table => $constraints) {
   $q = "ALTER TABLE `$table` ";
   $first = true;

   foreach ($constraints as $c) {
      if ($first) {
         $first = false;
      } else {
         $q .= ", ";
      }
      $q .= $c;
   }

   echo $q."<br><br>";
   $DB->query($q) or die($q." ".$DB->error());
86 87 88

}

moyooo's avatar
moyooo committed
89
$DB->query("SET FOREIGN_KEY_CHECKS = 1;");
moyooo's avatar
moyooo committed
90