dumpschema.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. #!/usr/bin/env php
  2. <?php
  3. /*
  4. * StatusNet - a distributed open-source microblogging tool
  5. * Copyright (C) 2008, 2009, StatusNet, Inc.
  6. *
  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.
  11. *
  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.
  16. *
  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/>.
  19. */
  20. define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
  21. $helptext = <<<END_OF_CHECKSCHEMA_HELP
  22. Attempt to pull a schema definition for a given table.
  23. --all run over all defined core tables
  24. --diff show differences between the expected and live table defs
  25. --raw skip compatibility filtering for diffs
  26. --create dump SQL that would be run to update or create this table
  27. --build dump SQL that would be run to create this table fresh
  28. --checksum just output checksums from the source schema defs
  29. END_OF_CHECKSCHEMA_HELP;
  30. $longoptions = array('diff', 'all', 'create', 'update', 'raw', 'checksum');
  31. require_once INSTALLDIR.'/scripts/commandline.inc';
  32. function indentOptions($indent)
  33. {
  34. $cutoff = 3;
  35. if ($indent < $cutoff) {
  36. $space = $indent ? str_repeat(' ', $indent * 4) : '';
  37. $sep = ",";
  38. $lf = "\n";
  39. $endspace = "$lf" . ($indent ? str_repeat(' ', ($indent - 1) * 4) : '');
  40. } else {
  41. $space = '';
  42. $sep = ", ";
  43. $lf = '';
  44. $endspace = '';
  45. }
  46. if ($indent - 1 < $cutoff) {
  47. }
  48. return array($space, $sep, $lf, $endspace);
  49. }
  50. function prettyDumpArray($arr, $key=null, $indent=0)
  51. {
  52. // hack
  53. if ($key == 'primary key') {
  54. $subIndent = $indent + 2;
  55. } else {
  56. $subIndent = $indent + 1;
  57. }
  58. list($space, $sep, $lf, $endspace) = indentOptions($indent);
  59. list($inspace, $insep, $inlf, $inendspace) = indentOptions($subIndent);
  60. print "{$space}";
  61. if (!is_numeric($key)) {
  62. print "'$key' => ";
  63. }
  64. if (is_array($arr)) {
  65. print "array({$inlf}";
  66. $n = 0;
  67. foreach ($arr as $key => $row) {
  68. $n++;
  69. prettyDumpArray($row, $key, $subIndent);
  70. if ($n < count($arr)) {
  71. print "$insep$inlf";
  72. }
  73. }
  74. // hack!
  75. print "{$inendspace})";
  76. } else {
  77. print var_export($arr, true);
  78. }
  79. }
  80. function getCoreSchema($tableName)
  81. {
  82. $schema = array();
  83. include INSTALLDIR . '/db/core.php';
  84. return $schema[$tableName];
  85. }
  86. function getCoreTables()
  87. {
  88. $schema = array();
  89. include INSTALLDIR . '/db/core.php';
  90. return array_keys($schema);
  91. }
  92. function dumpTable($tableName, $live)
  93. {
  94. if ($live) {
  95. $schema = Schema::get();
  96. $def = $schema->getTableDef($tableName);
  97. } else {
  98. // hack
  99. $def = getCoreSchema($tableName);
  100. }
  101. prettyDumpArray($def, $tableName);
  102. print "\n";
  103. }
  104. function dumpBuildTable($tableName)
  105. {
  106. echo "-- \n";
  107. echo "-- $tableName\n";
  108. echo "-- \n";
  109. $schema = Schema::get();
  110. $def = getCoreSchema($tableName);
  111. $sql = $schema->buildCreateTable($tableName, $def);
  112. $sql[] = '';
  113. echo implode(";\n", $sql);
  114. echo "\n";
  115. }
  116. function dumpEnsureTable($tableName)
  117. {
  118. $schema = Schema::get();
  119. $def = getCoreSchema($tableName);
  120. $sql = $schema->buildEnsureTable($tableName, $def);
  121. if ($sql) {
  122. echo "-- \n";
  123. echo "-- $tableName\n";
  124. echo "-- \n";
  125. $sql[] = '';
  126. echo implode(";\n", $sql);
  127. echo "\n";
  128. }
  129. }
  130. function dumpDiff($tableName, $filter)
  131. {
  132. $schema = Schema::get();
  133. $def = getCoreSchema($tableName);
  134. try {
  135. $old = $schema->getTableDef($tableName);
  136. } catch (Exception $e) {
  137. // @fixme this is a terrible check :D
  138. if (preg_match('/no such table/i', $e->getMessage())) {
  139. return dumpTable($tableName, false);
  140. } else {
  141. throw $e;
  142. }
  143. }
  144. if ($filter) {
  145. //$old = $schema->filterDef($old);
  146. $def = $schema->filterDef($def);
  147. }
  148. // @hack
  149. $old = tweakPrimaryKey($old);
  150. $def = tweakPrimaryKey($def);
  151. $sections = array_unique(array_merge(array_keys($old), array_keys($def)));
  152. $final = array();
  153. foreach ($sections as $section) {
  154. if ($section == 'fields') {
  155. // this shouldn't be needed maybe... wait what?
  156. }
  157. $diff = $schema->diffArrays($old, $def, $section);
  158. $chunks = array('del', 'mod', 'add');
  159. foreach ($chunks as $chunk) {
  160. if ($diff[$chunk]) {
  161. foreach ($diff[$chunk] as $key) {
  162. if ($chunk == 'del') {
  163. $final[$section]["DEL $key"] = $old[$section][$key];
  164. } else if ($chunk == 'add') {
  165. $final[$section]["ADD $key"] = $def[$section][$key];
  166. } else if ($chunk == 'mod') {
  167. $final[$section]["OLD $key"] = $old[$section][$key];
  168. $final[$section]["NEW $key"] = $def[$section][$key];
  169. }
  170. }
  171. }
  172. }
  173. }
  174. prettyDumpArray($final, $tableName);
  175. print "\n";
  176. }
  177. function tweakPrimaryKey($def)
  178. {
  179. if (isset($def['primary key'])) {
  180. $def['primary keys'] = array('primary key' => $def['primary key']);
  181. unset($def['primary key']);
  182. }
  183. if (isset($def['description'])) {
  184. $def['descriptions'] = array('description' => $def['description']);
  185. unset($def['description']);
  186. }
  187. return $def;
  188. }
  189. function dumpChecksum($tableName)
  190. {
  191. $schema = Schema::get();
  192. $def = getCoreSchema($tableName);
  193. $updater = new SchemaUpdater($schema);
  194. $checksum = $updater->checksum($def);
  195. $old = @$updater->checksums[$tableName];
  196. if ($old == $checksum) {
  197. echo "OK $checksum $tableName\n";
  198. } else if (!$old) {
  199. echo "NEW $checksum $tableName\n";
  200. } else {
  201. echo "MOD $checksum $tableName (was $old)\n";
  202. }
  203. }
  204. if (have_option('all')) {
  205. $args = getCoreTables();
  206. }
  207. if (count($args)) {
  208. foreach ($args as $tableName) {
  209. if (have_option('diff')) {
  210. dumpDiff($tableName, !have_option('raw'));
  211. } else if (have_option('create')) {
  212. dumpBuildTable($tableName);
  213. } else if (have_option('update')) {
  214. dumpEnsureTable($tableName);
  215. } else if (have_option('checksum')) {
  216. dumpChecksum($tableName);
  217. } else {
  218. dumpTable($tableName, true);
  219. }
  220. }
  221. } else {
  222. show_help($helptext);
  223. }