4 * StatusNet - a distributed open-source microblogging tool
5 * Copyright (C) 2008, 2009, StatusNet, Inc.
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
23 $helptext = <<<END_OF_CHECKSCHEMA_HELP
24 Attempt to pull a schema definition for a given table.
26 --all run over all defined core tables
27 --diff show differences between the expected and live table defs
28 --raw skip compatibility filtering for diffs
29 --create dump SQL that would be run to update or create this table
30 --build dump SQL that would be run to create this table fresh
33 END_OF_CHECKSCHEMA_HELP;
35 $longoptions = array('diff', 'all', 'create', 'update', 'raw');
36 require_once INSTALLDIR.'/scripts/commandline.inc';
38 function indentOptions($indent)
41 if ($indent < $cutoff) {
42 $space = $indent ? str_repeat(' ', $indent * 4) : '';
45 $endspace = "$lf" . ($indent ? str_repeat(' ', ($indent - 1) * 4) : '');
52 if ($indent - 1 < $cutoff) {
54 return array($space, $sep, $lf, $endspace);
57 function prettyDumpArray($arr, $key=null, $indent=0)
60 if ($key == 'primary key') {
61 $subIndent = $indent + 2;
63 $subIndent = $indent + 1;
66 list($space, $sep, $lf, $endspace) = indentOptions($indent);
67 list($inspace, $insep, $inlf, $inendspace) = indentOptions($subIndent);
70 if (!is_numeric($key)) {
74 print "array({$inlf}";
76 foreach ($arr as $key => $row) {
78 prettyDumpArray($row, $key, $subIndent);
79 if ($n < count($arr)) {
84 print "{$inendspace})";
86 print var_export($arr, true);
90 function getCoreSchema($tableName)
93 include INSTALLDIR . '/db/core.php';
94 return $schema[$tableName];
97 function getCoreTables()
100 include INSTALLDIR . '/db/core.php';
101 return array_keys($schema);
104 function dumpTable($tableName, $live)
107 $schema = Schema::get();
108 $def = $schema->getTableDef($tableName);
111 $def = getCoreSchema($tableName);
113 prettyDumpArray($def, $tableName);
117 function dumpBuildTable($tableName)
120 echo "-- $tableName\n";
123 $schema = Schema::get();
124 $def = getCoreSchema($tableName);
125 $sql = $schema->buildCreateTable($tableName, $def);
128 echo implode(";\n", $sql);
132 function dumpEnsureTable($tableName)
134 $schema = Schema::get();
135 $def = getCoreSchema($tableName);
136 $sql = $schema->buildEnsureTable($tableName, $def);
140 echo "-- $tableName\n";
144 echo implode(";\n", $sql);
149 function dumpDiff($tableName, $filter)
151 $schema = Schema::get();
152 $def = getCoreSchema($tableName);
154 $old = $schema->getTableDef($tableName);
155 } catch (Exception $e) {
156 // @fixme this is a terrible check :D
157 if (preg_match('/no such table/i', $e->getMessage())) {
158 return dumpTable($tableName, false);
165 //$old = $schema->filterDef($old);
166 $def = $schema->filterDef($def);
170 $old = tweakPrimaryKey($old);
171 $def = tweakPrimaryKey($def);
173 $sections = array_unique(array_merge(array_keys($old), array_keys($def)));
175 foreach ($sections as $section) {
176 if ($section == 'fields') {
177 // this shouldn't be needed maybe... wait what?
179 $diff = $schema->diffArrays($old, $def, $section, $compare);
180 $chunks = array('del', 'mod', 'add');
181 foreach ($chunks as $chunk) {
183 foreach ($diff[$chunk] as $key) {
184 if ($chunk == 'del') {
185 $final[$section]["DEL $key"] = $old[$section][$key];
186 } else if ($chunk == 'add') {
187 $final[$section]["ADD $key"] = $def[$section][$key];
188 } else if ($chunk == 'mod') {
189 $final[$section]["OLD $key"] = $old[$section][$key];
190 $final[$section]["NEW $key"] = $def[$section][$key];
197 prettyDumpArray($final, $tableName);
201 function tweakPrimaryKey($def)
203 if (isset($def['primary key'])) {
204 $def['primary keys'] = array('primary key' => $def['primary key']);
205 unset($def['primary key']);
210 if (have_option('all')) {
211 $args = getCoreTables();
215 foreach ($args as $tableName) {
216 if (have_option('diff')) {
217 dumpDiff($tableName, !have_option('raw'));
218 } else if (have_option('create')) {
219 dumpBuildTable($tableName);
220 } else if (have_option('update')) {
221 dumpEnsureTable($tableName);
223 dumpTable($tableName, true);
227 show_help($helptext);