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)
133 echo "-- $tableName\n";
136 $schema = Schema::get();
137 $def = getCoreSchema($tableName);
138 $sql = $schema->buildEnsureTable($tableName, $def);
141 echo implode(";\n", $sql);
145 function showDiff($a, $b)
147 $fnameA = tempnam(sys_get_temp_dir(), 'defined-diff-a');
148 file_put_contents($fnameA, $a);
150 $fnameB = tempnam(sys_get_temp_dir(), 'detected-diff-b');
151 file_put_contents($fnameB, $b);
153 $cmd = sprintf('diff -U 100 %s %s',
154 escapeshellarg($fnameA),
155 escapeshellarg($fnameB));
162 if (have_option('all')) {
163 $args = getCoreTables();
167 foreach ($args as $tableName) {
168 if (have_option('diff')) {
170 dumpTable($tableName, false);
171 $defined = ob_get_clean();
174 dumpTable($tableName, true);
175 $detected = ob_get_clean();
177 showDiff($defined, $detected);
178 } else if (have_option('build')) {
179 dumpBuildTable($tableName);
180 } else if (have_option('update')) {
181 dumpEnsureTable($tableName);
183 dumpTable($tableName, true);
187 show_help($helptext);