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| 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 m{^CREATE TABLE /\*\$wgDBprefix\*/(\w+) \($}
100 or die qq{Invalid CREATE TABLE at line
$. of
$oldfile\n};
102 $info{$table}{name
}=$table;
104 elsif (m{^\) /\*\$wgDBTableOptions\*/}) {
105 $info{$table}{engine
} = 'ENGINE';
106 $info{$table}{type
} = 'variable';
108 elsif (/^\) ($engine)=($tabletype);$/) {
109 $info{$table}{engine
}=$1;
110 $info{$table}{type
}=$2;
112 elsif (/^\) ($engine)=($tabletype), DEFAULT CHARSET=($charset);$/) {
113 $info{$table}{engine
}=$1;
114 $info{$table}{type
}=$2;
115 $info{$table}{charset
}=$3;
117 elsif (/^ (\w+) $datatype$typeval$typeval2{0,3},?$/) {
118 $info{$table}{column
}{$1} = $2;
119 my $extra = $3 || '';
120 $info{$table}{columnfull
}{$1} = "$2$extra";
122 elsif (/^ ($indextype)(?: (\w+))? \(([\w, \(\)]+)\),?$/) {
123 $info{$table}{lc $1.'_name'} = $2 ?
$2 : '';
124 $info{$table}{lc $1.'pk_target'} = $3;
127 die "Cannot parse line $. of $oldfile:\n$_\n";
131 close $oldfh or die qq{Could
not close "$oldfile": $!\n};
135 } ## end of parse_sql
137 ## Read in the parser test information
138 my $parsefile = '../parserTests.inc';
139 open my $pfh, '<', $parsefile or die qq{Could
not open "$parsefile": $!\n};
144 if (/function listTables/) {
149 $ptable{$1}=2 while m{'(\w+)'}g;
152 close $pfh or die qq{Could
not close "$parsefile": $!\n};
154 my $OK_NOT_IN_PTABLE = '
166 ## Make sure all tables in main tables.sql are accounted for in the parsertest.
167 for my $table (sort keys %{$old{'../tables.sql'}}) {
169 next if $ptable{$table} > 2;
170 next if $OK_NOT_IN_PTABLE =~ /\b$table\b/;
171 print qq{Table
"$table" is
in the schema
, but
not used inside of parserTest
.inc
\n};
173 ## Any that are used in ptables but no longer exist in the schema?
174 for my $table (sort grep { $ptable{$_} == 2 } keys %ptable) {
175 print qq{Table
"$table" ($ptable{$table}) used
in parserTest
.inc
, but
not found
in schema
\n};
178 for my $oldfile (@old) {
180 ## Begin non-standard indent
182 ## MySQL sanity checks
183 for my $table (sort keys %{$old{$oldfile}}) {
184 my $t = $old{$oldfile}{$table};
185 if ($t->{engine
} eq 'TYPE') {
186 die "Invalid engine for $oldfile: $t->{engine}\n" unless $t->{name
} eq 'profiling';
188 my $charset = $t->{charset
} || '';
189 if ($oldfile !~ /binary/ and $charset eq 'binary') {
190 die "Invalid charset for $oldfile: $charset\n";
194 my $dtype = join '|' => qw(
195 SMALLINT INTEGER BIGINT NUMERIC SERIAL
201 $dtype = qr{($dtype)};
203 my ($infunction,$inview,$inrule,$lastcomma) = (0,0,0,0);
206 next if /^\s*\-\-/ or /^\s*$/;
207 s/\s*\-\- [\w ']+$//;
208 next if /^BEGIN;/ or /^SET / or /^COMMIT;/;
209 next if /^CREATE SEQUENCE/;
210 next if /^CREATE(?: UNIQUE)? INDEX/;
211 next if /^CREATE FUNCTION/;
212 next if /^CREATE TRIGGER/ or /^ FOR EACH ROW/;
213 next if /^INSERT INTO/ or /^ VALUES \(/;
214 next if /^ALTER TABLE/;
218 $infunction = $infunction ?
0 : 1;
223 next if /^CREATE VIEW/ and $inview = 1;
225 /;$/ and $inview = 0;
229 next if /^CREATE RULE/ and $inrule = 1;
231 /;$/ and $inrule = 0;
235 if (/^CREATE TABLE "?(\w+)"? \($/) {
237 $new{$table}{name
}=$table;
242 warn "Stray comma before line $.\n";
245 elsif (/^ (\w+) +$dtype.*?(,?)(?: --.*)?$/) {
246 $new{$table}{column
}{$1} = $2;
248 print "Missing comma before line $. of $new\n";
250 $lastcomma = $3 ?
1 : 0;
253 die "Cannot parse line $. of $new:\n$_\n";
257 ## Which column types are okay to map from mysql to postgres?
277 varbinary(14) TIMESTAMPTZ
278 binary(14) TIMESTAMPTZ
280 timestamp TIMESTAMPTZ
289 ## Allow specific exceptions to the above
291 ## User inputted text strings:
292 ar_comment tinyblob TEXT
293 fa_description tinyblob TEXT
294 img_description tinyblob TEXT
295 ipb_reason tinyblob TEXT
296 log_action varbinary(10) TEXT
297 oi_description tinyblob TEXT
298 rev_comment tinyblob TEXT
299 rc_log_action varbinary(255) TEXT
300 rc_log_type varbinary(255) TEXT
302 ## Simple text-only strings:
303 ar_flags tinyblob TEXT
304 fa_minor_mime varbinary(32) TEXT
305 fa_storage_group varbinary(16) TEXT # Just 'deleted' for now, should stay plain text
306 fa_storage_key varbinary(64) TEXT # sha1 plus text extension
307 ipb_address tinyblob TEXT # IP address or username
308 ipb_range_end tinyblob TEXT # hexadecimal
309 ipb_range_start tinyblob TEXT # hexadecimal
310 img_minor_mime varbinary(32) TEXT
311 img_sha1 varbinary(32) TEXT
312 job_cmd varbinary(60) TEXT # Should we limit to 60 as well?
313 keyname varbinary(255) TEXT # No tablename prefix (objectcache)
314 ll_lang varbinary(20) TEXT # Language code
315 log_params blob TEXT # LF separated list of args
316 log_type varbinary(10) TEXT
317 oi_minor_mime varbinary(32) TEXT
318 oi_sha1 varbinary(32) TEXT
319 old_flags tinyblob TEXT
320 old_text mediumblob TEXT
321 pp_propname varbinary(60) TEXT
323 page_restrictions tinyblob TEXT # CSV string
324 pf_server varchar(30) TEXT
325 pr_level varbinary(60) TEXT
326 pr_type varbinary(60) TEXT
327 pt_create_perm varbinary(60) TEXT
328 pt_reason tinyblob TEXT
329 qc_type varbinary(32) TEXT
330 qcc_type varbinary(32) TEXT
331 qci_type varbinary(32) TEXT
333 rlc_to_blob blob TEXT
334 ug_group varbinary(16) TEXT
335 user_email_token binary(32) TEXT
336 user_ip varbinary(40) TEXT
337 user_newpassword tinyblob TEXT
338 user_options blob TEXT
339 user_password tinyblob TEXT
340 user_token binary(32) TEXT
347 tc_url varbinary(255) TEXT
349 ## Deprecated or not yet used:
350 ar_text mediumblob TEXT
352 log_deleted tinyint INTEGER # Not used yet, but keep it INTEGER for safety
356 fa_bits int SMALLINT # bits per pixel
357 fa_height int SMALLINT
358 fa_width int SMALLINT # Hope we don't see an image this wide...
359 hc_id int BIGINT # Odd that site_stats is all bigint...
360 img_bits int SMALLINT # bits per image should stay sane
363 ## True binary fields, usually due to gzdeflate and/or serialize:
364 math_inputhash varbinary(16) BYTEA
365 math_outputhash varbinary(16) BYTEA
367 ## Namespaces: not need for such a high range
368 ar_namespace int SMALLINT
369 job_namespace int SMALLINT
370 log_namespace int SMALLINT
371 page_namespace int SMALLINT
372 pl_namespace int SMALLINT
373 pt_namespace int SMALLINT
374 qc_namespace int SMALLINT
375 rc_namespace int SMALLINT
376 rd_namespace int SMALLINT
377 rlc_to_namespace int SMALLINT
378 tl_namespace int SMALLINT
379 wl_namespace int SMALLINT
381 ## Easy enough to change if a wiki ever does grow this big:
382 ss_active_users bigint INTEGER
383 ss_good_articles bigint INTEGER
384 ss_total_edits bigint INTEGER
385 ss_total_pages bigint INTEGER
386 ss_total_views bigint INTEGER
387 ss_users bigint INTEGER
389 ## True IP - keep an eye on these, coders tend to make textual assumptions
390 rc_ip varbinary(40) CIDR # Want to keep an eye on this
393 tc_time int TIMESTAMPTZ
399 for (split /\n/ => $COLMAP) {
402 my ($col,@maps) = split / +/, $_;
404 $colmap{$col}{$_} = 1;
409 for (split /\n/ => $COLMAPOK) {
411 my ($col,$old,$new) = split / +/, $_;
412 $colmapok{$col}{$old}{$new} = 1;
416 for my $t (sort keys %{$old{$oldfile}}) {
417 if (!exists $new{$t} and !exists $ok{OLD
}{$t}) {
418 print "Table not in $new: $t\n";
421 next if exists $ok{OLD
}{$t} and !$ok{OLD
}{$t};
422 my $newt = exists $ok{OLD
}{$t} ?
$ok{OLD
}{$t} : $t;
423 my $oldcol = $old{$oldfile}{$t}{column
};
424 my $oldcolfull = $old{$oldfile}{$t}{columnfull
};
425 my $newcol = $new{$newt}{column
};
426 for my $c (keys %$oldcol) {
427 if (!exists $newcol->{$c}) {
428 print "Column $t.$c not in $new\n";
432 for my $c (sort keys %$newcol) {
433 if (!exists $oldcol->{$c}) {
434 print "Column $t.$c not in $oldfile\n";
437 ## Column types (roughly) match up?
438 my $new = $newcol->{$c};
439 my $old = $oldcolfull->{$c};
442 next if exists $colmapok{$c}{$old}{$new};
444 $old =~ s/ENUM.*/ENUM/;
445 if (! exists $colmap{$old}{$new}) {
446 print "Column types for $t.$c do not match: $old does not map to $new\n";
451 for (sort keys %new) {
452 if (!exists $old{$oldfile}{$_} and !exists $ok{NEW
}{$_}) {
453 print "Not in $oldfile: $_\n";
459 } ## end each file to be parsed
462 sub check_valid_sql
{
464 ## Check for a few common problems in most php files
466 my $olddir = getcwd
();
468 for my $basedir (qw
/includes extensions/) {
475 } ## end of check_valid_sql
482 opendir my $dh, $dir or die qq{Could
not opendir $dir: $!\n};
483 #print "Scanning $dir...\n";
484 for my $file (grep { -f
"$dir/$_" and /\
.php
$/ } readdir $dh) {
485 find_problems
("$dir/$file");
488 for my $subdir (grep { -d
"$dir/$_" and ! /\
./ } readdir $dh) {
489 scan_dir
("$dir/$subdir");
491 closedir $dh or die qq{Closedir failed
: $!\n};
499 open my $fh, '<', $file or die qq{Could
not open "$file": $!\n};
501 if (/FORCE INDEX/ and $file !~ /Database\w*\.php/) {
502 warn "Found FORCE INDEX string at line $. of $file\n";
504 if (/REPLACE INTO/ and $file !~ /Database\w*\.php/) {
505 warn "Found REPLACE INTO string at line $. of $file\n";
507 if (/\bIF\s*\(/ and $file !~ /DatabaseMySQL\.php/) {
508 warn "Found IF string at line $. of $file\n";
510 if (/\bCONCAT\b/ and $file !~ /Database\w*\.php/) {
511 warn "Found CONCAT string at line $. of $file\n";
513 if (/\bGROUP\s+BY\s*\d\b/i and $file !~ /Database\w*\.php/) {
514 warn "Found GROUP BY # at line $. of $file\n";
517 close $fh or die qq{Could
not close "$file": $!\n};
520 } ## end of find_problems
525 OLD
: searchindex
## We use tsearch2 directly on the page table instead
526 RENAME
: user mwuser
## Reserved word causing lots of problems
527 RENAME
: text pagecontent
## Reserved word
528 NEW
: mediawiki_version
## Just us, for now
529 XFILE
: ../archives/patch
-profiling
.sql