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
31 --checksum just output checksums from the source schema defs
34 END_OF_CHECKSCHEMA_HELP;
36 $longoptions = array('diff', 'all', 'create', 'update', 'raw', 'checksum');
37 require_once INSTALLDIR.'/scripts/commandline.inc';
39 function indentOptions($indent)
42 if ($indent < $cutoff) {
43 $space = $indent ? str_repeat(' ', $indent * 4) : '';
46 $endspace = "$lf" . ($indent ? str_repeat(' ', ($indent - 1) * 4) : '');
53 if ($indent - 1 < $cutoff) {
55 return array($space, $sep, $lf, $endspace);
58 function prettyDumpArray($arr, $key=null, $indent=0)
61 if ($key == 'primary key') {
62 $subIndent = $indent + 2;
64 $subIndent = $indent + 1;
67 list($space, $sep, $lf, $endspace) = indentOptions($indent);
68 list($inspace, $insep, $inlf, $inendspace) = indentOptions($subIndent);
71 if (!is_numeric($key)) {
75 print "array({$inlf}";
77 foreach ($arr as $key => $row) {
79 prettyDumpArray($row, $key, $subIndent);
80 if ($n < count($arr)) {
85 print "{$inendspace})";
87 print var_export($arr, true);
91 function getCoreSchema($tableName)
94 include INSTALLDIR . '/db/core.php';
95 return $schema[$tableName];
98 function getCoreTables()
101 include INSTALLDIR . '/db/core.php';
102 return array_keys($schema);
105 function dumpTable($tableName, $live)
108 $schema = Schema::get();
109 $def = $schema->getTableDef($tableName);
112 $def = getCoreSchema($tableName);
114 prettyDumpArray($def, $tableName);
118 function dumpBuildTable($tableName)
121 echo "-- $tableName\n";
124 $schema = Schema::get();
125 $def = getCoreSchema($tableName);
126 $sql = $schema->buildCreateTable($tableName, $def);
129 echo implode(";\n", $sql);
133 function dumpEnsureTable($tableName)
135 $schema = Schema::get();
136 $def = getCoreSchema($tableName);
137 $sql = $schema->buildEnsureTable($tableName, $def);
141 echo "-- $tableName\n";
145 echo implode(";\n", $sql);
150 function dumpDiff($tableName, $filter)
152 $schema = Schema::get();
153 $def = getCoreSchema($tableName);
155 $old = $schema->getTableDef($tableName);
156 } catch (Exception $e) {
157 // @fixme this is a terrible check :D
158 if (preg_match('/no such table/i', $e->getMessage())) {
159 return dumpTable($tableName, false);
166 //$old = $schema->filterDef($old);
167 $def = $schema->filterDef($def);
171 $old = tweakPrimaryKey($old);
172 $def = tweakPrimaryKey($def);
174 $sections = array_unique(array_merge(array_keys($old), array_keys($def)));
176 foreach ($sections as $section) {
177 if ($section == 'fields') {
178 // this shouldn't be needed maybe... wait what?
180 $diff = $schema->diffArrays($old, $def, $section);
181 $chunks = array('del', 'mod', 'add');
182 foreach ($chunks as $chunk) {
184 foreach ($diff[$chunk] as $key) {
185 if ($chunk == 'del') {
186 $final[$section]["DEL $key"] = $old[$section][$key];
187 } else if ($chunk == 'add') {
188 $final[$section]["ADD $key"] = $def[$section][$key];
189 } else if ($chunk == 'mod') {
190 $final[$section]["OLD $key"] = $old[$section][$key];
191 $final[$section]["NEW $key"] = $def[$section][$key];
198 prettyDumpArray($final, $tableName);
202 function tweakPrimaryKey($def)
204 if (isset($def['primary key'])) {
205 $def['primary keys'] = array('primary key' => $def['primary key']);
206 unset($def['primary key']);
208 if (isset($def['description'])) {
209 $def['descriptions'] = array('description' => $def['description']);
210 unset($def['description']);
215 function dumpChecksum($tableName)
217 $schema = Schema::get();
218 $def = getCoreSchema($tableName);
220 $updater = new SchemaUpdater($schema);
221 $checksum = $updater->checksum($def);
222 $old = @$updater->checksums[$tableName];
224 if ($old == $checksum) {
225 echo "OK $checksum $tableName\n";
227 echo "NEW $checksum $tableName\n";
229 echo "MOD $checksum $tableName (was $old)\n";
233 if (have_option('all')) {
234 $args = getCoreTables();
238 foreach ($args as $tableName) {
239 if (have_option('diff')) {
240 dumpDiff($tableName, !have_option('raw'));
241 } else if (have_option('create')) {
242 dumpBuildTable($tableName);
243 } else if (have_option('update')) {
244 dumpEnsureTable($tableName);
245 } else if (have_option('checksum')) {
246 dumpChecksum($tableName);
248 dumpTable($tableName, true);
252 show_help($helptext);