4f73137396067d983ee48194c26d3fa45a23617c
[lhc/web/wiklou.git] / maintenance / postgres / compare_schemas.pl
1 #!/usr/bin/perl
2
3 ## Rough check that the base and postgres "tables.sql" are in sync
4 ## Should be run from maintenance/postgres
5
6 use strict;
7 use warnings;
8 use Data::Dumper;
9
10 my @old = ('../tables.sql');
11 my $new = 'tables.sql';
12 my @xfile;
13
14 ## Read in exceptions and other metadata
15 my %ok;
16 while (<DATA>) {
17 next unless /^(\w+)\s*:\s*([^#]+)/;
18 my ($name,$val) = ($1,$2);
19 chomp $val;
20 if ($name eq 'RENAME') {
21 die "Invalid rename\n" unless $val =~ /(\w+)\s+(\w+)/;
22 $ok{OLD}{$1} = $2;
23 $ok{NEW}{$2} = $1;
24 next;
25 }
26 if ($name eq 'XFILE') {
27 push @xfile, $val;
28 next;
29 }
30 for (split /\s+/ => $val) {
31 $ok{$name}{$_} = 0;
32 }
33 }
34
35 my $datatype = join '|' => qw(
36 bool
37 tinyint int bigint real float
38 tinytext mediumtext text char varchar varbinary binary
39 timestamp datetime
40 tinyblob mediumblob blob
41 );
42 $datatype .= q{|ENUM\([\"\w, ]+\)};
43 $datatype = qr{($datatype)};
44
45 my $typeval = qr{(\(\d+\))?};
46
47 my $typeval2 = qr{ unsigned| binary| NOT NULL| NULL| auto_increment| default ['\-\d\w"]+| REFERENCES .+CASCADE};
48
49 my $indextype = join '|' => qw(INDEX KEY FULLTEXT), 'PRIMARY KEY', 'UNIQUE INDEX', 'UNIQUE KEY';
50 $indextype = qr{$indextype};
51
52 my $engine = qr{TYPE|ENGINE};
53
54 my $tabletype = qr{InnoDB|MyISAM|HEAP|HEAP MAX_ROWS=\d+|InnoDB MAX_ROWS=\d+ AVG_ROW_LENGTH=\d+};
55
56 my $charset = qr{utf8|binary};
57
58 open my $newfh, '<', $new or die qq{Could not open $new: $!\n};
59
60
61 my ($table,%old);
62
63 ## Read in the xfiles
64 my %xinfo;
65 for my $xfile (@xfile) {
66 print "Loading $xfile\n";
67 my $info = &parse_sql($xfile);
68 for (keys %$info) {
69 $xinfo{$_} = $info->{$_};
70 }
71 }
72
73 for my $oldfile (@old) {
74 print "Loading $oldfile\n";
75 my $info = &parse_sql($oldfile);
76 for (keys %xinfo) {
77 $info->{$_} = $xinfo{$_};
78 }
79 $old{$oldfile} = $info;
80 }
81
82 sub parse_sql {
83
84 my $oldfile = shift;
85
86 open my $oldfh, '<', $oldfile or die qq{Could not open $oldfile: $!\n};
87
88 my %info;
89 while (<$oldfh>) {
90 next if /^\s*\-\-/ or /^\s+$/;
91 s/\s*\-\- [\w ]+$//;
92 chomp;
93
94 if (/CREATE\s*TABLE/i) {
95 m{^CREATE TABLE /\*\$wgDBprefix\*/(\w+) \($}
96 or die qq{Invalid CREATE TABLE at line $. of $oldfile\n};
97 $table = $1;
98 $info{$table}{name}=$table;
99 }
100 elsif (m#^\) /\*\$wgDBTableOptions\*/#) {
101 $info{$table}{engine} = 'TYPE';
102 $info{$table}{type} = 'variable';
103 }
104 elsif (/^\) ($engine)=($tabletype);$/) {
105 $info{$table}{engine}=$1;
106 $info{$table}{type}=$2;
107 }
108 elsif (/^\) ($engine)=($tabletype), DEFAULT CHARSET=($charset);$/) {
109 $info{$table}{engine}=$1;
110 $info{$table}{type}=$2;
111 $info{$table}{charset}=$3;
112 }
113 elsif (/^ (\w+) $datatype$typeval$typeval2{0,3},?$/) {
114 $info{$table}{column}{$1} = $2;
115 my $extra = $3 || '';
116 $info{$table}{columnfull}{$1} = "$2$extra";
117 }
118 elsif (/^ ($indextype)(?: (\w+))? \(([\w, \(\)]+)\),?$/) {
119 $info{$table}{lc $1.'_name'} = $2 ? $2 : '';
120 $info{$table}{lc $1.'pk_target'} = $3;
121 }
122 else {
123 die "Cannot parse line $. of $oldfile:\n$_\n";
124 }
125
126 }
127 close $oldfh;
128
129 return \%info;
130
131 } ## end of parse_sql
132
133 for my $oldfile (@old) {
134
135 ## Begin non-standard indent
136
137 ## MySQL sanity checks
138 for my $table (sort keys %{$old{$oldfile}}) {
139 my $t = $old{$oldfile}{$table};
140 if (($oldfile =~ /5/ and $t->{engine} ne 'ENGINE')
141 or
142 ($oldfile !~ /5/ and $t->{engine} ne 'TYPE')) {
143 die "Invalid engine for $oldfile: $t->{engine}\n" unless $t->{name} eq 'profiling';
144 }
145 my $charset = $t->{charset} || '';
146 if ($oldfile !~ /binary/ and $charset eq 'binary') {
147 die "Invalid charset for $oldfile: $charset\n";
148 }
149 }
150
151 my $dtype = join '|' => qw(
152 SMALLINT INTEGER BIGINT NUMERIC SERIAL
153 TEXT CHAR VARCHAR
154 BYTEA
155 TIMESTAMPTZ
156 CIDR
157 );
158 $dtype = qr{($dtype)};
159 my %new;
160 my ($infunction,$inview,$inrule,$lastcomma) = (0,0,0,0);
161 seek $newfh, 0, 0;
162 while (<$newfh>) {
163 next if /^\s*\-\-/ or /^\s*$/;
164 s/\s*\-\- [\w ']+$//;
165 next if /^BEGIN;/ or /^SET / or /^COMMIT;/;
166 next if /^CREATE SEQUENCE/;
167 next if /^CREATE(?: UNIQUE)? INDEX/;
168 next if /^CREATE FUNCTION/;
169 next if /^CREATE TRIGGER/ or /^ FOR EACH ROW/;
170 next if /^INSERT INTO/ or /^ VALUES \(/;
171 next if /^ALTER TABLE/;
172 chomp;
173
174 if (/^\$mw\$;?$/) {
175 $infunction = $infunction ? 0 : 1;
176 next;
177 }
178 next if $infunction;
179
180 next if /^CREATE VIEW/ and $inview = 1;
181 if ($inview) {
182 /;$/ and $inview = 0;
183 next;
184 }
185
186 next if /^CREATE RULE/ and $inrule = 1;
187 if ($inrule) {
188 /;$/ and $inrule = 0;
189 next;
190 }
191
192 if (/^CREATE TABLE "?(\w+)"? \($/) {
193 $table = $1;
194 $new{$table}{name}=$table;
195 $lastcomma = 1;
196 }
197 elsif (/^\);$/) {
198 if ($lastcomma) {
199 warn "Stray comma before line $.\n";
200 }
201 }
202 elsif (/^ (\w+) +$dtype.*?(,?)(?: --.*)?$/) {
203 $new{$table}{column}{$1} = $2;
204 if (!$lastcomma) {
205 print "Missing comma before line $. of $new\n";
206 }
207 $lastcomma = $3 ? 1 : 0;
208 }
209 else {
210 die "Cannot parse line $. of $new:\n$_\n";
211 }
212 }
213
214 ## Which column types are okay to map from mysql to postgres?
215 my $COLMAP = q{
216 ## INTS:
217 tinyint SMALLINT
218 int INTEGER SERIAL
219 bigint BIGINT
220 real NUMERIC
221 float NUMERIC
222
223 ## TEXT:
224 varchar(32) TEXT
225 varchar(70) TEXT
226 varchar(255) TEXT
227 varchar TEXT
228 text TEXT
229 tinytext TEXT
230 ENUM TEXT
231
232 ## TIMESTAMPS:
233 varbinary(14) TIMESTAMPTZ
234 binary(14) TIMESTAMPTZ
235 datetime TIMESTAMPTZ
236 timestamp TIMESTAMPTZ
237
238 ## BYTEA:
239 mediumblob BYTEA
240
241 ## OTHER:
242 bool CHAR # Sigh
243
244 };
245 ## Allow specific exceptions to the above
246 my $COLMAPOK = q{
247 ## User inputted text strings:
248 ar_comment tinyblob TEXT
249 fa_description tinyblob TEXT
250 img_description tinyblob TEXT
251 ipb_reason tinyblob TEXT
252 log_action varbinary(10) TEXT
253 oi_description tinyblob TEXT
254 rev_comment tinyblob TEXT
255 rc_log_action varbinary(255) TEXT
256 rc_log_type varbinary(255) TEXT
257
258 ## Simple text-only strings:
259 ar_flags tinyblob TEXT
260 fa_minor_mime varbinary(32) TEXT
261 fa_storage_group varbinary(16) TEXT # Just 'deleted' for now, should stay plain text
262 fa_storage_key varbinary(64) TEXT # sha1 plus text extension
263 ipb_address tinyblob TEXT # IP address or username
264 ipb_range_end tinyblob TEXT # hexadecimal
265 ipb_range_start tinyblob TEXT # hexadecimal
266 img_minor_mime varbinary(32) TEXT
267 img_sha1 varbinary(32) TEXT
268 job_cmd varbinary(60) TEXT # Should we limit to 60 as well?
269 keyname varbinary(255) TEXT # No tablename prefix (objectcache)
270 ll_lang varbinary(20) TEXT # Language code
271 log_params blob TEXT # LF separated list of args
272 log_type varbinary(10) TEXT
273 oi_minor_mime varbinary(32) TEXT
274 oi_sha1 varbinary(32) TEXT
275 old_flags tinyblob TEXT
276 old_text mediumblob TEXT
277 page_restrictions tinyblob TEXT # CSV string
278 pf_server varchar(30) TEXT
279 pr_level varbinary(60) TEXT
280 pr_type varbinary(60) TEXT
281 pt_create_perm varbinary(60) TEXT
282 pt_reason tinyblob TEXT
283 qc_type varbinary(32) TEXT
284 qcc_type varbinary(32) TEXT
285 qci_type varbinary(32) TEXT
286 rc_params blob TEXT
287 ug_group varbinary(16) TEXT
288 user_email_token binary(32) TEXT
289 user_ip varbinary(40) TEXT
290 user_newpassword tinyblob TEXT
291 user_options blob TEXT
292 user_password tinyblob TEXT
293 user_token binary(32) TEXT
294
295 ## Text URLs:
296 el_index blob TEXT
297 el_to blob TEXT
298 iw_url blob TEXT
299 tb_url blob TEXT
300 tc_url varbinary(255) TEXT
301
302 ## Deprecated or not yet used:
303 ar_text mediumblob TEXT
304 job_params blob TEXT
305 log_deleted tinyint INTEGER # Not used yet, but keep it INTEGER for safety
306 rc_type tinyint CHAR
307
308 ## Number tweaking:
309 fa_bits int SMALLINT # bits per pixel
310 fa_height int SMALLINT
311 fa_width int SMALLINT # Hope we don't see an image this wide...
312 hc_id int BIGINT # Odd that site_stats is all bigint...
313 img_bits int SMALLINT # bits per image should stay sane
314 oi_bits int SMALLINT
315
316 ## True binary fields, usually due to gzdeflate and/or serialize:
317 math_inputhash varbinary(16) BYTEA
318 math_outputhash varbinary(16) BYTEA
319
320 ## Namespaces: not need for such a high range
321 ar_namespace int SMALLINT
322 job_namespace int SMALLINT
323 log_namespace int SMALLINT
324 page_namespace int SMALLINT
325 pl_namespace int SMALLINT
326 pt_namespace int SMALLINT
327 qc_namespace int SMALLINT
328 rc_namespace int SMALLINT
329 rd_namespace int SMALLINT
330 tl_namespace int SMALLINT
331 wl_namespace int SMALLINT
332
333 ## "Bools"
334 ar_minor_edit tinyint CHAR
335 iw_trans tinyint CHAR
336 page_is_new tinyint CHAR
337 page_is_redirect tinyint CHAR
338 rc_bot tinyint CHAR
339 rc_deleted tinyint CHAR
340 rc_minor tinyint CHAR
341 rc_new tinyint CHAR
342 rc_patrolled tinyint CHAR
343 rev_deleted tinyint CHAR
344 rev_minor_edit tinyint CHAR
345
346 ## Easy enough to change if a wiki ever does grow this big:
347 ss_good_articles bigint INTEGER
348 ss_total_edits bigint INTEGER
349 ss_total_pages bigint INTEGER
350 ss_total_views bigint INTEGER
351 ss_users bigint INTEGER
352
353 ## True IP - keep an eye on these, coders tend to make textual assumptions
354 rc_ip varbinary(40) CIDR # Want to keep an eye on this
355
356 ## Others:
357 tc_time int TIMESTAMPTZ
358
359
360 };
361
362 my %colmap;
363 for (split /\n/ => $COLMAP) {
364 next unless /^\w/;
365 s/(.*?)#.*/$1/;
366 my ($col,@maps) = split / +/, $_;
367 for (@maps) {
368 $colmap{$col}{$_} = 1;
369 }
370 }
371
372 my %colmapok;
373 for (split /\n/ => $COLMAPOK) {
374 next unless /^\w/;
375 my ($col,$old,$new) = split / +/, $_;
376 $colmapok{$col}{$old}{$new} = 1;
377 }
378
379 ## Old but not new
380 for my $t (sort keys %{$old{$oldfile}}) {
381 if (!exists $new{$t} and !exists $ok{OLD}{$t}) {
382 print "Table not in $new: $t\n";
383 next;
384 }
385 next if exists $ok{OLD}{$t} and !$ok{OLD}{$t};
386 my $newt = exists $ok{OLD}{$t} ? $ok{OLD}{$t} : $t;
387 my $oldcol = $old{$oldfile}{$t}{column};
388 my $oldcolfull = $old{$oldfile}{$t}{columnfull};
389 my $newcol = $new{$newt}{column};
390 for my $c (keys %$oldcol) {
391 if (!exists $newcol->{$c}) {
392 print "Column $t.$c not in $new\n";
393 next;
394 }
395 }
396 for my $c (sort keys %$newcol) {
397 if (!exists $oldcol->{$c}) {
398 print "Column $t.$c not in $oldfile\n";
399 next;
400 }
401 ## Column types (roughly) match up?
402 my $new = $newcol->{$c};
403 my $old = $oldcolfull->{$c};
404
405 ## Known exceptions:
406 next if exists $colmapok{$c}{$old}{$new};
407
408 $old =~ s/ENUM.*/ENUM/;
409 if (! exists $colmap{$old}{$new}) {
410 print "Column types for $t.$c do not match: $old does not map to $new\n";
411 }
412 }
413 }
414 ## New but not old:
415 for (sort keys %new) {
416 if (!exists $old{$oldfile}{$_} and !exists $ok{NEW}{$_}) {
417 print "Not in $oldfile: $_\n";
418 next;
419 }
420 }
421
422
423 } ## end each file to be parsed
424
425
426 __DATA__
427 ## Known exceptions
428 OLD: searchindex ## We use tsearch2 directly on the page table instead
429 RENAME: user mwuser ## Reserved word causing lots of problems
430 RENAME: text pagecontent ## Reserved word
431 NEW: mediawiki_version ## Just us, for now
432 XFILE: ../archives/patch-profiling.sql