]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - scripts/dumpschema.php
Filter table definitions to scrub out unsupported features before trying to alter...
[quix0rs-gnu-social.git] / scripts / dumpschema.php
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
21 define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
22
23 $helptext = <<<END_OF_CHECKSCHEMA_HELP
24 Attempt to pull a schema definition for a given table.
25
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
30
31
32 END_OF_CHECKSCHEMA_HELP;
33
34 $longoptions = array('diff', 'all', 'build', 'update');
35 require_once INSTALLDIR.'/scripts/commandline.inc';
36
37 function indentOptions($indent)
38 {
39     $cutoff = 3;
40     if ($indent < $cutoff) {
41         $space = $indent ? str_repeat(' ', $indent * 4) : '';
42         $sep = ",";
43         $lf = "\n";
44         $endspace = "$lf" . ($indent ? str_repeat(' ', ($indent - 1) * 4) : '');
45     } else {
46         $space = '';
47         $sep = ", ";
48         $lf = '';
49         $endspace = '';
50     }
51     if ($indent - 1 < $cutoff) {
52     }
53     return array($space, $sep, $lf, $endspace);
54 }
55
56 function prettyDumpArray($arr, $key=null, $indent=0)
57 {
58     // hack
59     if ($key == 'primary key') {
60         $subIndent = $indent + 2;
61     } else {
62         $subIndent = $indent + 1;
63     }
64
65     list($space, $sep, $lf, $endspace) = indentOptions($indent);
66     list($inspace, $insep, $inlf, $inendspace) = indentOptions($subIndent);
67
68     print "{$space}";
69     if (!is_numeric($key)) {
70         print "'$key' => ";
71     }
72     if (is_array($arr)) {
73         print "array({$inlf}";
74         $n = 0;
75         foreach ($arr as $key => $row) {
76             $n++;
77             prettyDumpArray($row, $key, $subIndent);
78             if ($n < count($arr)) {
79                 print "$insep$inlf";
80             }
81         }
82         // hack!
83         print "{$inendspace})";
84     } else {
85         print var_export($arr, true);
86     }
87 }
88
89 function getCoreSchema($tableName)
90 {
91     $schema = array();
92     include INSTALLDIR . '/db/core.php';
93     return $schema[$tableName];
94 }
95
96 function getCoreTables()
97 {
98     $schema = array();
99     include INSTALLDIR . '/db/core.php';
100     return array_keys($schema);
101 }
102
103 function dumpTable($tableName, $live)
104 {
105     if ($live) {
106         $schema = Schema::get();
107         $def = $schema->getTableDef($tableName);
108     } else {
109         // hack
110         $def = getCoreSchema($tableName);
111     }
112     prettyDumpArray($def, $tableName);
113 }
114
115 function dumpBuildTable($tableName)
116 {
117     echo "-- \n";
118     echo "-- $tableName\n";
119     echo "-- \n";
120
121     $schema = Schema::get();
122     $def = getCoreSchema($tableName);
123     $sql = $schema->buildCreateTable($tableName, $def);
124     $sql[] = '';
125
126     echo implode(";\n", $sql);
127     echo "\n";
128 }
129
130 function dumpEnsureTable($tableName)
131 {
132     $schema = Schema::get();
133     $def = getCoreSchema($tableName);
134     $sql = $schema->buildEnsureTable($tableName, $def);
135
136     if ($sql) {
137         echo "-- \n";
138         echo "-- $tableName\n";
139         echo "-- \n";
140
141         $sql[] = '';
142         echo implode(";\n", $sql);
143         echo "\n";
144     }
145 }
146
147 function showDiff($a, $b)
148 {
149     $fnameA = tempnam(sys_get_temp_dir(), 'defined-diff-a');
150     file_put_contents($fnameA, $a);
151
152     $fnameB = tempnam(sys_get_temp_dir(), 'detected-diff-b');
153     file_put_contents($fnameB, $b);
154
155     $cmd = sprintf('diff -U 100 %s %s',
156             escapeshellarg($fnameA),
157             escapeshellarg($fnameB));
158     passthru($cmd);
159
160     unlink($fnameA);
161     unlink($fnameB);
162 }
163
164 if (have_option('all')) {
165     $args = getCoreTables();
166 }
167
168 if (count($args)) {
169     foreach ($args as $tableName) {
170         if (have_option('diff')) {
171             ob_start();
172             dumpTable($tableName, false);
173             $defined = ob_get_clean();
174
175             ob_start();
176             dumpTable($tableName, true);
177             $detected = ob_get_clean();
178
179             showDiff($defined, $detected);
180         } else if (have_option('build')) {
181             dumpBuildTable($tableName);
182         } else if (have_option('update')) {
183             dumpEnsureTable($tableName);
184         } else {
185             dumpTable($tableName, true);
186         }
187     }
188 } else {
189     show_help($helptext);
190 }