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 do a raw text diff between the expected and live table defs
28 --update dump SQL that would be run to update or create this table
29 --build dump SQL that would be run to create this table fresh
32 END_OF_CHECKSCHEMA_HELP;
34 $longoptions = array('diff', 'all', 'build', 'update');
35 require_once INSTALLDIR.'/scripts/commandline.inc';
37 function indentOptions($indent)
40 if ($indent < $cutoff) {
41 $space = $indent ? str_repeat(' ', $indent * 4) : '';
44 $endspace = "$lf" . ($indent ? str_repeat(' ', ($indent - 1) * 4) : '');
51 if ($indent - 1 < $cutoff) {
53 return array($space, $sep, $lf, $endspace);
56 function prettyDumpArray($arr, $key=null, $indent=0)
59 if ($key == 'primary key') {
60 $subIndent = $indent + 2;
62 $subIndent = $indent + 1;
65 list($space, $sep, $lf, $endspace) = indentOptions($indent);
66 list($inspace, $insep, $inlf, $inendspace) = indentOptions($subIndent);
69 if (!is_numeric($key)) {
73 print "array({$inlf}";
75 foreach ($arr as $key => $row) {
77 prettyDumpArray($row, $key, $subIndent);
78 if ($n < count($arr)) {
83 print "{$inendspace})";
85 print var_export($arr, true);
89 function getCoreSchema($tableName)
92 include INSTALLDIR . '/db/core.php';
93 return $schema[$tableName];
96 function getCoreTables()
99 include INSTALLDIR . '/db/core.php';
100 return array_keys($schema);
103 function dumpTable($tableName, $live)
106 $schema = Schema::get();
107 $def = $schema->getTableDef($tableName);
110 $def = getCoreSchema($tableName);
112 prettyDumpArray($def, $tableName);
115 function dumpBuildTable($tableName)
118 echo "-- $tableName\n";
121 $schema = Schema::get();
122 $def = getCoreSchema($tableName);
123 $sql = $schema->buildCreateTable($tableName, $def);
126 echo implode(";\n", $sql);
130 function dumpEnsureTable($tableName)
132 $schema = Schema::get();
133 $def = getCoreSchema($tableName);
134 $sql = $schema->buildEnsureTable($tableName, $def);
138 echo "-- $tableName\n";
142 echo implode(";\n", $sql);
147 function showDiff($a, $b)
149 $fnameA = tempnam(sys_get_temp_dir(), 'defined-diff-a');
150 file_put_contents($fnameA, $a);
152 $fnameB = tempnam(sys_get_temp_dir(), 'detected-diff-b');
153 file_put_contents($fnameB, $b);
155 $cmd = sprintf('diff -U 100 %s %s',
156 escapeshellarg($fnameA),
157 escapeshellarg($fnameB));
164 if (have_option('all')) {
165 $args = getCoreTables();
169 foreach ($args as $tableName) {
170 if (have_option('diff')) {
172 dumpTable($tableName, false);
173 $defined = ob_get_clean();
176 dumpTable($tableName, true);
177 $detected = ob_get_clean();
179 showDiff($defined, $detected);
180 } else if (have_option('build')) {
181 dumpBuildTable($tableName);
182 } else if (have_option('update')) {
183 dumpEnsureTable($tableName);
185 dumpTable($tableName, true);
189 show_help($helptext);