3 ## Rough check that the base and postgres "tables.sql" are in sync
4 ## Should be run from maintenance/postgres
5 ## Checks a few other things as well...
14 my @old = ('../tables.sql');
15 my $new = 'tables.sql';
18 ## Read in exceptions and other metadata
21 next unless /^(\w+)\s*:\s*([^#]+)/;
22 my ($name,$val) = ($1,$2);
24 if ($name eq 'RENAME') {
25 die "Invalid rename\n" unless $val =~ /(\w+)\s+(\w+)/;
30 if ($name eq 'XFILE') {
34 for (split /\s+/ => $val) {
39 my $datatype = join '|' => qw(
41 tinyint int bigint real float
42 tinytext mediumtext text char varchar varbinary binary
44 tinyblob mediumblob blob
46 $datatype .= q{|ENUM\([\"\w\', ]+\)};
47 $datatype = qr{($datatype)};
49 my $typeval = qr{(\(\d+\))?};
51 my $typeval2 = qr{ signed| unsigned| binary| NOT NULL| NULL| PRIMARY KEY| AUTO_INCREMENT| default ['\-\d\w"]+| REFERENCES .+CASCADE};
53 my $indextype = join '|' => qw(INDEX KEY FULLTEXT), 'PRIMARY KEY', 'UNIQUE INDEX', 'UNIQUE KEY';
54 $indextype = qr{$indextype};
56 my $engine = qr{TYPE|ENGINE};
58 my $tabletype = qr{InnoDB|MyISAM|HEAP|HEAP MAX_ROWS=\d+|InnoDB MAX_ROWS=\d+ AVG_ROW_LENGTH=\d+};
60 my $charset = qr{utf8|binary};
62 open my $newfh, '<', $new or die qq{Could
not open $new: $!\n};
69 for my $xfile (@xfile) {
70 print "Loading $xfile\n";
71 my $info = parse_sql
($xfile);
73 $xinfo{$_} = $info->{$_};
77 for my $oldfile (@old) {
78 print "Loading $oldfile\n";
79 my $info = parse_sql
($oldfile);
81 $info->{$_} = $xinfo{$_};
83 $old{$oldfile} = $info;
90 open my $oldfh, '<', $oldfile or die qq{Could
not open $oldfile: $!\n};
94 next if /^\s*\-\-/ or /^\s+$/;
98 if (/CREATE\s*TABLE/i) {
99 if (m{^CREATE TABLE /\*_\*/(\w+) \($}) {
102 elsif (m{^CREATE TABLE /\*\$wgDBprefix\*/(\w+) \($}) {
106 die qq{Invalid CREATE TABLE at line
$. of
$oldfile\n};
108 $info{$table}{name
}=$table;
110 elsif (m{^\) /\*\$wgDBTableOptions\*/}) {
111 $info{$table}{engine
} = 'ENGINE';
112 $info{$table}{type
} = 'variable';
114 elsif (/^\) ($engine)=($tabletype);$/) {
115 $info{$table}{engine
}=$1;
116 $info{$table}{type
}=$2;
118 elsif (/^\) ($engine)=($tabletype), DEFAULT CHARSET=($charset);$/) {
119 $info{$table}{engine
}=$1;
120 $info{$table}{type
}=$2;
121 $info{$table}{charset
}=$3;
123 elsif (/^ (\w+) $datatype$typeval$typeval2{0,4},?$/) {
124 $info{$table}{column
}{$1} = $2;
125 my $extra = $3 || '';
126 $info{$table}{columnfull
}{$1} = "$2$extra";
128 elsif (m{^ UNIQUE KEY (\w+) \((.+?)\)}) {
130 elsif (m{^CREATE (?:UNIQUE )?(?:FULLTEXT )?INDEX /\*i\*/(\w+) ON /\*_\*/(\w+) \((.+?)\);}) {
132 elsif (m{^\s*PRIMARY KEY \([\w,]+\)}) {
135 die "Cannot parse line $. of $oldfile:\n$_\n";
139 close $oldfh or die qq{Could
not close "$oldfile": $!\n};
143 } ## end of parse_sql
145 for my $oldfile (@old) {
147 ## Begin non-standard indent
149 ## MySQL sanity checks
150 for my $table (sort keys %{$old{$oldfile}}) {
151 my $t = $old{$oldfile}{$table};
152 if ($t->{engine
} eq 'TYPE') {
153 die "Invalid engine for $oldfile: $t->{engine}\n" unless $t->{name
} eq 'profiling';
155 my $charset = $t->{charset
} || '';
156 if ($oldfile !~ /binary/ and $charset eq 'binary') {
157 die "Invalid charset for $oldfile: $charset\n";
161 my $dtype = join '|' => qw(
162 SMALLINT INTEGER BIGINT NUMERIC SERIAL
168 $dtype = qr{($dtype)};
170 my ($infunction,$inview,$inrule,$lastcomma) = (0,0,0,0);
173 next if /^\s*\-\-/ or /^\s*$/;
174 s/\s*\-\- [\w ']+$//;
175 next if /^BEGIN;/ or /^SET / or /^COMMIT;/;
176 next if /^CREATE SEQUENCE/;
177 next if /^CREATE(?: UNIQUE)? INDEX/;
178 next if /^CREATE FUNCTION/;
179 next if /^CREATE TRIGGER/ or /^ FOR EACH ROW/;
180 next if /^INSERT INTO/ or /^ VALUES \(/;
181 next if /^ALTER TABLE/;
182 next if /^DROP SEQUENCE/;
183 next if /^DROP FUNCTION/;
189 $infunction = $infunction ?
0 : 1;
194 next if /^CREATE VIEW/ and $inview = 1;
196 /;$/ and $inview = 0;
200 next if /^CREATE RULE/ and $inrule = 1;
202 /;$/ and $inrule = 0;
206 if (/^CREATE TABLE "?(\w+)"? \($/) {
208 $new{$table}{name
}=$table;
213 warn "Stray comma before line $.\n";
216 elsif (/^ (\w+) +$dtype.*?(,?)(?: --.*)?$/) {
217 $new{$table}{column
}{$1} = $2;
219 print "Missing comma before line $. of $new\n";
221 $lastcomma = $3 ?
1 : 0;
223 elsif (m{^\s*PRIMARY KEY \([\w,]+\)}) {
227 die "Cannot parse line $. of $new:\n$_\n";
231 ## Which column types are okay to map from mysql to postgres?
251 varbinary(14) TIMESTAMPTZ
252 binary(14) TIMESTAMPTZ
254 timestamp TIMESTAMPTZ
263 ## Allow specific exceptions to the above
265 ## User inputted text strings:
266 ar_comment tinyblob TEXT
267 fa_description tinyblob TEXT
268 img_description tinyblob TEXT
269 ipb_reason tinyblob TEXT
270 log_action varbinary(32) TEXT
271 log_type varbinary(32) TEXT
272 oi_description tinyblob TEXT
273 rev_comment tinyblob TEXT
274 rc_log_action varbinary(255) TEXT
275 rc_log_type varbinary(255) TEXT
277 ## Simple text-only strings:
278 ar_flags tinyblob TEXT
279 cl_collation varbinary(32) TEXT
280 cl_sortkey varbinary(230) TEXT
282 fa_minor_mime varbinary(100) TEXT
283 fa_storage_group varbinary(16) TEXT # Just 'deleted' for now, should stay plain text
284 fa_storage_key varbinary(64) TEXT # sha1 plus text extension
285 ipb_address tinyblob TEXT # IP address or username
286 ipb_range_end tinyblob TEXT # hexadecimal
287 ipb_range_start tinyblob TEXT # hexadecimal
288 img_minor_mime varbinary(100) TEXT
289 lc_lang varbinary(32) TEXT
290 lc_value varbinary(32) TEXT
291 img_sha1 varbinary(32) TEXT
292 iw_wikiid varchar(64) TEXT
293 job_cmd varbinary(60) TEXT # Should we limit to 60 as well?
294 keyname varbinary(255) TEXT # No tablename prefix (objectcache)
295 ll_lang varbinary(20) TEXT # Language code
296 lc_value mediumblob TEXT
297 log_params blob TEXT # LF separated list of args
298 log_type varbinary(10) TEXT
299 ls_field varbinary(32) TEXT
300 md_deps mediumblob TEXT # JSON
301 mr_blob mediumblob TEXT # JSON
302 mr_lang varbinary(32) TEXT
303 oi_minor_mime varbinary(100) TEXT
304 oi_sha1 varbinary(32) TEXT
305 old_flags tinyblob TEXT
306 old_text mediumblob TEXT
307 pp_propname varbinary(60) TEXT
309 page_restrictions tinyblob TEXT # CSV string
310 pf_server varchar(30) TEXT
311 pr_level varbinary(60) TEXT
312 pr_type varbinary(60) TEXT
313 pt_create_perm varbinary(60) TEXT
314 pt_reason tinyblob TEXT
315 qc_type varbinary(32) TEXT
316 qcc_type varbinary(32) TEXT
317 qci_type varbinary(32) TEXT
319 rlc_to_blob blob TEXT
321 ug_group varbinary(16) TEXT
323 up_property varbinary(32) TEXT
325 user_email_token binary(32) TEXT
326 user_ip varbinary(40) TEXT
327 user_newpassword tinyblob TEXT
328 user_options blob TEXT
329 user_password tinyblob TEXT
330 user_token binary(32) TEXT
331 iwl_prefix varbinary(20) TEXT
339 tc_url varbinary(255) TEXT
341 ## Deprecated or not yet used:
342 ar_text mediumblob TEXT
344 log_deleted tinyint INTEGER # Not used yet, but keep it INTEGER for safety
348 fa_bits int SMALLINT # bits per pixel
349 fa_height int SMALLINT
350 fa_width int SMALLINT # Hope we don't see an image this wide...
351 hc_id int BIGINT # Odd that site_stats is all bigint...
352 img_bits int SMALLINT # bits per image should stay sane
355 ## True binary fields, usually due to gzdeflate and/or serialize:
356 math_inputhash varbinary(16) BYTEA
357 math_outputhash varbinary(16) BYTEA
359 ## Namespaces: not need for such a high range
360 ar_namespace int SMALLINT
361 job_namespace int SMALLINT
362 log_namespace int SMALLINT
363 page_namespace int SMALLINT
364 pl_namespace int SMALLINT
365 pt_namespace int SMALLINT
366 qc_namespace int SMALLINT
367 rc_namespace int SMALLINT
368 rd_namespace int SMALLINT
369 rlc_to_namespace int SMALLINT
370 tl_namespace int SMALLINT
371 wl_namespace int SMALLINT
373 ## Easy enough to change if a wiki ever does grow this big:
374 ss_active_users bigint INTEGER
375 ss_good_articles bigint INTEGER
376 ss_total_edits bigint INTEGER
377 ss_total_pages bigint INTEGER
378 ss_total_views bigint INTEGER
379 ss_users bigint INTEGER
381 ## True IP - keep an eye on these, coders tend to make textual assumptions
382 rc_ip varbinary(40) CIDR # Want to keep an eye on this
385 tc_time int TIMESTAMPTZ
391 for (split /\n/ => $COLMAP) {
394 my ($col,@maps) = split / +/, $_;
396 $colmap{$col}{$_} = 1;
401 for (split /\n/ => $COLMAPOK) {
403 my ($col,$old,$new) = split / +/, $_;
404 $colmapok{$col}{$old}{$new} = 1;
408 for my $t (sort keys %{$old{$oldfile}}) {
409 if (!exists $new{$t} and !exists $ok{OLD
}{$t}) {
410 print "Table not in $new: $t\n";
413 next if exists $ok{OLD
}{$t} and !$ok{OLD
}{$t};
414 my $newt = exists $ok{OLD
}{$t} ?
$ok{OLD
}{$t} : $t;
415 my $oldcol = $old{$oldfile}{$t}{column
};
416 my $oldcolfull = $old{$oldfile}{$t}{columnfull
};
417 my $newcol = $new{$newt}{column
};
418 for my $c (keys %$oldcol) {
419 if (!exists $newcol->{$c}) {
420 print "Column $t.$c not in $new\n";
424 for my $c (sort keys %$newcol) {
425 if (!exists $oldcol->{$c}) {
426 print "Column $t.$c not in $oldfile\n";
429 ## Column types (roughly) match up?
430 my $new = $newcol->{$c};
431 my $old = $oldcolfull->{$c};
434 next if exists $colmapok{$c}{$old}{$new};
436 $old =~ s/ENUM.*/ENUM/;
437 if (! exists $colmap{$old}{$new}) {
438 print "Column types for $t.$c do not match: $old does not map to $new\n";
443 for (sort keys %new) {
444 if (!exists $old{$oldfile}{$_} and !exists $ok{NEW
}{$_}) {
445 print "Not in $oldfile: $_\n";
451 } ## end each file to be parsed
454 sub check_valid_sql
{
456 ## Check for a few common problems in most php files
458 my $olddir = getcwd
();
460 for my $basedir (qw
/includes extensions/) {
467 } ## end of check_valid_sql
474 opendir my $dh, $dir or die qq{Could
not opendir $dir: $!\n};
475 #print "Scanning $dir...\n";
476 for my $file (grep { -f
"$dir/$_" and /\
.php
$/ } readdir $dh) {
477 find_problems
("$dir/$file");
480 for my $subdir (grep { -d
"$dir/$_" and ! /\
./ } readdir $dh) {
481 scan_dir
("$dir/$subdir");
483 closedir $dh or die qq{Closedir failed
: $!\n};
491 open my $fh, '<', $file or die qq{Could
not open "$file": $!\n};
495 if (/FORCE INDEX/ and $file !~ /Database\w*\.php/) {
496 warn "Found FORCE INDEX string at line $. of $file\n";
498 if (/REPLACE INTO/ and $file !~ /Database\w*\.php/) {
499 warn "Found REPLACE INTO string at line $. of $file\n";
501 if (/\bIF\s*\(/ and $file !~ /DatabaseMySQL\.php/) {
502 warn "Found IF string at line $. of $file\n";
504 if (/\bCONCAT\b/ and $file !~ /Database\w*\.php/) {
505 warn "Found CONCAT string at line $. of $file\n";
507 if (/\bGROUP\s+BY\s*\d\b/i and $file !~ /Database\w*\.php/) {
508 warn "Found GROUP BY # at line $. of $file\n";
510 if (/wfGetDB\s*\(\s+\)/io) {
511 warn "wfGETDB is missing parameters at line $. of $file\n";
513 if (/=\s*array\s*\(\s*$/) {
522 next if ! /\w/ or /array\(\s*$/ or /^\s*#/ or m{^\s*//};
524 my $nextline = <$fh>;
525 last if ! defined $nextline;
526 if ($nextline =~ /^\s*\)[;,]/) {
530 #warn "Array is missing a comma? Line $. of $file\n";
534 close $fh or die qq{Could
not close "$file": $!\n};
537 } ## end of find_problems
542 OLD
: searchindex
## We use tsearch2 directly on the page table instead
543 RENAME
: user mwuser
## Reserved word causing lots of problems
544 RENAME
: text pagecontent
## Reserved word
545 XFILE
: ../archives/patch
-profiling
.sql