forked from emoncms/emoncms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbschemasetup.php
103 lines (90 loc) · 4.44 KB
/
dbschemasetup.php
1
2
3
4
5
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
/*
All Emoncms code is released under the GNU Affero General Public License.
See COPYRIGHT.txt and LICENSE.txt.
---------------------------------------------------------------------
Emoncms - open source energy visualisation
Part of the OpenEnergyMonitor project:
http://openenergymonitor.org
*/
// no direct access
defined('EMONCMS_EXEC') or die('Restricted access');
function db_schema_setup($mysqli, $schema, $apply)
{
$operations = array();
while ($table = key($schema))
{
// if table exists:
$result = $mysqli->query("SHOW TABLES LIKE '".$table."'");
if (($result != null ) && ($result->num_rows==1))
{
// $out[] = array('Table',$table,"ok");
//-----------------------------------------------------
// Check table fields from schema
//-----------------------------------------------------
while ($field = key($schema[$table]))
{
$type = $schema[$table][$field]['type'];
if (isset($schema[$table][$field]['Null'])) $null = $schema[$table][$field]['Null']; else $null = "YES";
if (isset($schema[$table][$field]['Key'])) $key = $schema[$table][$field]['Key']; else $key = null;
if (isset($schema[$table][$field]['default'])) $default = $schema[$table][$field]['default']; else unset($default);
if (isset($schema[$table][$field]['Extra'])) $extra = $schema[$table][$field]['Extra']; else $extra = null;
// if field exists:
$result = $mysqli->query("SHOW COLUMNS FROM `$table` LIKE '$field'");
if ($result->num_rows==0)
{
$query = "ALTER TABLE `$table` ADD `$field` $type";
if ($null) $query .= " NOT NULL";
if (isset($default)) $query .= " DEFAULT '$default'";
$operations[] = $query;
if ($apply) $mysqli->query($query);
}
else
{
$result = $mysqli->query("DESCRIBE $table `$field`");
$array = $result->fetch_array();
$query = "";
if (isset($default) && $array['Default']!=$default) $query .= " Default '$default'";
if ($array['Null']!=$null && $null=="NO") $query .= " not null";
if ($array['Extra']!=$extra && $extra=="auto_increment") $query .= " auto_increment";
if ($array['Key']!=$key && $key=="PRI") $query .= " primary key";
if ($array['Type']!=$type) $query .= ";";
if ($query) $query = "ALTER TABLE $table MODIFY `$field` $type".$query;
if ($query) $operations[] = $query;
if ($query && $apply) $mysqli->query($query);
}
next($schema[$table]);
}
} else {
//-----------------------------------------------------
// Create table from schema
//-----------------------------------------------------
$query = "CREATE TABLE " . $table . " (";
while ($field = key($schema[$table]))
{
$type = $schema[$table][$field]['type'];
if (isset($schema[$table][$field]['Null'])) $null = $schema[$table][$field]['Null']; else $null = "YES";
if (isset($schema[$table][$field]['Key'])) $key = $schema[$table][$field]['Key']; else $key = null;
if (isset($schema[$table][$field]['default'])) $default = $schema[$table][$field]['default']; else $default = null;
if (isset($schema[$table][$field]['Extra'])) $extra = $schema[$table][$field]['Extra']; else $extra = null;
$query .= '`'.$field.'`';
$query .= " $type";
if ($default) $query .= " Default '$default'";
if ($null=="NO") $query .= " not null";
if ($extra) $query .= " auto_increment";
if ($key) $query .= " primary key";
next($schema[$table]);
if (key($schema[$table]))
{
$query .= ", ";
}
}
$query .= ")";
$query .= " ENGINE=MYISAM";
if ($query) $operations[] = $query;
if ($query && $apply) $mysqli->query($query);
}
next($schema);
}
return $operations;
}