3 ## Rough check that the base and postgres "tables.sql" are in sync
4 ## Should be run from maintenance/postgres
10 my @old = ('../tables.sql');
11 my $new = 'tables.sql';
14 ## Read in exceptions and other metadata
17 next unless /^(\w+)\s*:\s*([^#]+)/;
18 my ($name,$val) = ($1,$2);
20 if ($name eq 'RENAME') {
21 die "Invalid rename\n" unless $val =~ /(\w+)\s+(\w+)/;
26 if ($name eq 'XFILE') {
30 for (split /\s+/ => $val) {
35 my $datatype = join '|' => qw(
37 tinyint int bigint real float
38 tinytext mediumtext text char varchar varbinary binary
40 tinyblob mediumblob blob
42 $datatype .= q{|ENUM\([\"\w, ]+\)};
43 $datatype = qr{($datatype)};
45 my $typeval = qr{(\(\d+\))?};
47 my $typeval2 = qr{ unsigned| binary| NOT NULL| NULL| auto_increment| default ['\-\d\w"]+| REFERENCES .+CASCADE};
49 my $indextype = join '|' => qw(INDEX KEY FULLTEXT), 'PRIMARY KEY', 'UNIQUE INDEX', 'UNIQUE KEY';
50 $indextype = qr{$indextype};
52 my $engine = qr{TYPE|ENGINE};
54 my $tabletype = qr{InnoDB|MyISAM|HEAP|HEAP MAX_ROWS=\d+|InnoDB MAX_ROWS=\d+ AVG_ROW_LENGTH=\d+};
56 my $charset = qr{utf8|binary};
58 open my $newfh, '<', $new or die qq{Could
not open $new: $!\n};
65 for my $xfile (@xfile) {
66 print "Loading $xfile\n";
67 my $info = &parse_sql
($xfile);
69 $xinfo{$_} = $info->{$_};
73 for my $oldfile (@old) {
74 print "Loading $oldfile\n";
75 my $info = &parse_sql
($oldfile);
77 $info->{$_} = $xinfo{$_};
79 $old{$oldfile} = $info;
86 open my $oldfh, '<', $oldfile or die qq{Could
not open $oldfile: $!\n};
90 next if /^\s*\-\-/ or /^\s+$/;
94 if (/CREATE\s*TABLE/i) {
95 m{^CREATE TABLE /\*\$wgDBprefix\*/(\w+) \($}
96 or die qq{Invalid CREATE TABLE at line
$. of
$oldfile\n};
98 $info{$table}{name
}=$table;
100 elsif (m
#^\) /\*\$wgDBTableOptions\*/#) {
101 $info{$table}{engine
} = 'TYPE';
102 $info{$table}{type
} = 'variable';
104 elsif (/^\) ($engine)=($tabletype);$/) {
105 $info{$table}{engine
}=$1;
106 $info{$table}{type
}=$2;
108 elsif (/^\) ($engine)=($tabletype), DEFAULT CHARSET=($charset);$/) {
109 $info{$table}{engine
}=$1;
110 $info{$table}{type
}=$2;
111 $info{$table}{charset
}=$3;
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";
118 elsif (/^ ($indextype)(?: (\w+))? \(([\w, \(\)]+)\),?$/) {
119 $info{$table}{lc $1.'_name'} = $2 ?
$2 : '';
120 $info{$table}{lc $1.'pk_target'} = $3;
123 die "Cannot parse line $. of $oldfile:\n$_\n";
131 } ## end of parse_sql
133 ## Read in the parser test information
134 my $parsefile = '../parserTests.inc';
135 open my $pfh, '<', $parsefile or die qq{Could
not open "$parsefile": $!\n};
140 if (/function listTables/) {
145 $ptable{$1}=2 while /'(\w+)'/g;
150 my $OK_NOT_IN_PTABLE = '
161 ## Make sure all tables in main tables.sql are accounted for int the parsertest.
162 for my $table (sort keys %{$old{'../tables.sql'}}) {
164 next if $ptable{$table} > 2;
165 next if $OK_NOT_IN_PTABLE =~ /\b$table\b/;
166 print qq{Table
"$table" is
in the schema
, but
not used inside of parserTest
.inc
\n};
168 ## Any that are used in ptables but no longer exist in the schema?
169 for my $table (sort grep { $ptable{$_} == 2 } keys %ptable) {
170 print qq{Table
"$table" ($ptable{$table}) used
in parserTest
.inc
, but
not found
in schema
\n};
173 for my $oldfile (@old) {
175 ## Begin non-standard indent
177 ## MySQL sanity checks
178 for my $table (sort keys %{$old{$oldfile}}) {
179 my $t = $old{$oldfile}{$table};
180 if (($oldfile =~ /5/ and $t->{engine
} ne 'ENGINE')
182 ($oldfile !~ /5/ and $t->{engine
} ne 'TYPE')) {
183 die "Invalid engine for $oldfile: $t->{engine}\n" unless $t->{name
} eq 'profiling';
185 my $charset = $t->{charset
} || '';
186 if ($oldfile !~ /binary/ and $charset eq 'binary') {
187 die "Invalid charset for $oldfile: $charset\n";
191 my $dtype = join '|' => qw(
192 SMALLINT INTEGER BIGINT NUMERIC SERIAL
198 $dtype = qr{($dtype)};
200 my ($infunction,$inview,$inrule,$lastcomma) = (0,0,0,0);
203 next if /^\s*\-\-/ or /^\s*$/;
204 s/\s*\-\- [\w ']+$//;
205 next if /^BEGIN;/ or /^SET / or /^COMMIT;/;
206 next if /^CREATE SEQUENCE/;
207 next if /^CREATE(?: UNIQUE)? INDEX/;
208 next if /^CREATE FUNCTION/;
209 next if /^CREATE TRIGGER/ or /^ FOR EACH ROW/;
210 next if /^INSERT INTO/ or /^ VALUES \(/;
211 next if /^ALTER TABLE/;
215 $infunction = $infunction ?
0 : 1;
220 next if /^CREATE VIEW/ and $inview = 1;
222 /;$/ and $inview = 0;
226 next if /^CREATE RULE/ and $inrule = 1;
228 /;$/ and $inrule = 0;
232 if (/^CREATE TABLE "?(\w+)"? \($/) {
234 $new{$table}{name
}=$table;
239 warn "Stray comma before line $.\n";
242 elsif (/^ (\w+) +$dtype.*?(,?)(?: --.*)?$/) {
243 $new{$table}{column
}{$1} = $2;
245 print "Missing comma before line $. of $new\n";
247 $lastcomma = $3 ?
1 : 0;
250 die "Cannot parse line $. of $new:\n$_\n";
254 ## Which column types are okay to map from mysql to postgres?
273 varbinary(14) TIMESTAMPTZ
274 binary(14) TIMESTAMPTZ
276 timestamp TIMESTAMPTZ
285 ## Allow specific exceptions to the above
287 ## User inputted text strings:
288 ar_comment tinyblob TEXT
289 fa_description tinyblob TEXT
290 img_description tinyblob TEXT
291 ipb_reason tinyblob TEXT
292 log_action varbinary(10) TEXT
293 oi_description tinyblob TEXT
294 rev_comment tinyblob TEXT
295 rc_log_action varbinary(255) TEXT
296 rc_log_type varbinary(255) TEXT
298 ## Simple text-only strings:
299 ar_flags tinyblob TEXT
300 fa_minor_mime varbinary(32) TEXT
301 fa_storage_group varbinary(16) TEXT # Just 'deleted' for now, should stay plain text
302 fa_storage_key varbinary(64) TEXT # sha1 plus text extension
303 ipb_address tinyblob TEXT # IP address or username
304 ipb_range_end tinyblob TEXT # hexadecimal
305 ipb_range_start tinyblob TEXT # hexadecimal
306 img_minor_mime varbinary(32) TEXT
307 img_sha1 varbinary(32) TEXT
308 job_cmd varbinary(60) TEXT # Should we limit to 60 as well?
309 keyname varbinary(255) TEXT # No tablename prefix (objectcache)
310 ll_lang varbinary(20) TEXT # Language code
311 log_params blob TEXT # LF separated list of args
312 log_type varbinary(10) TEXT
313 oi_minor_mime varbinary(32) TEXT
314 oi_sha1 varbinary(32) TEXT
315 old_flags tinyblob TEXT
316 old_text mediumblob TEXT
317 pp_propname varbinary(60) TEXT
319 page_restrictions tinyblob TEXT # CSV string
320 pf_server varchar(30) TEXT
321 pr_level varbinary(60) TEXT
322 pr_type varbinary(60) TEXT
323 pt_create_perm varbinary(60) TEXT
324 pt_reason tinyblob TEXT
325 qc_type varbinary(32) TEXT
326 qcc_type varbinary(32) TEXT
327 qci_type varbinary(32) TEXT
329 ug_group varbinary(16) TEXT
330 user_email_token binary(32) TEXT
331 user_ip varbinary(40) TEXT
332 user_newpassword tinyblob TEXT
333 user_options blob TEXT
334 user_password tinyblob TEXT
335 user_token binary(32) TEXT
342 tc_url varbinary(255) TEXT
344 ## Deprecated or not yet used:
345 ar_text mediumblob TEXT
347 log_deleted tinyint INTEGER # Not used yet, but keep it INTEGER for safety
351 fa_bits int SMALLINT # bits per pixel
352 fa_height int SMALLINT
353 fa_width int SMALLINT # Hope we don't see an image this wide...
354 hc_id int BIGINT # Odd that site_stats is all bigint...
355 img_bits int SMALLINT # bits per image should stay sane
358 ## True binary fields, usually due to gzdeflate and/or serialize:
359 math_inputhash varbinary(16) BYTEA
360 math_outputhash varbinary(16) BYTEA
362 ## Namespaces: not need for such a high range
363 ar_namespace int SMALLINT
364 job_namespace int SMALLINT
365 log_namespace int SMALLINT
366 page_namespace int SMALLINT
367 pl_namespace int SMALLINT
368 pt_namespace int SMALLINT
369 qc_namespace int SMALLINT
370 rc_namespace int SMALLINT
371 rd_namespace int SMALLINT
372 tl_namespace int SMALLINT
373 wl_namespace int SMALLINT
376 ar_minor_edit tinyint CHAR
377 iw_trans tinyint CHAR
378 page_is_new tinyint CHAR
379 page_is_redirect tinyint CHAR
381 rc_deleted tinyint CHAR
382 rc_minor tinyint CHAR
384 rc_patrolled tinyint CHAR
385 rev_deleted tinyint CHAR
386 rev_minor_edit tinyint CHAR
388 ## Easy enough to change if a wiki ever does grow this big:
389 ss_good_articles bigint INTEGER
390 ss_total_edits bigint INTEGER
391 ss_total_pages bigint INTEGER
392 ss_total_views bigint INTEGER
393 ss_users bigint INTEGER
395 ## True IP - keep an eye on these, coders tend to make textual assumptions
396 rc_ip varbinary(40) CIDR # Want to keep an eye on this
399 tc_time int TIMESTAMPTZ
405 for (split /\n/ => $COLMAP) {
408 my ($col,@maps) = split / +/, $_;
410 $colmap{$col}{$_} = 1;
415 for (split /\n/ => $COLMAPOK) {
417 my ($col,$old,$new) = split / +/, $_;
418 $colmapok{$col}{$old}{$new} = 1;
422 for my $t (sort keys %{$old{$oldfile}}) {
423 if (!exists $new{$t} and !exists $ok{OLD
}{$t}) {
424 print "Table not in $new: $t\n";
427 next if exists $ok{OLD
}{$t} and !$ok{OLD
}{$t};
428 my $newt = exists $ok{OLD
}{$t} ?
$ok{OLD
}{$t} : $t;
429 my $oldcol = $old{$oldfile}{$t}{column
};
430 my $oldcolfull = $old{$oldfile}{$t}{columnfull
};
431 my $newcol = $new{$newt}{column
};
432 for my $c (keys %$oldcol) {
433 if (!exists $newcol->{$c}) {
434 print "Column $t.$c not in $new\n";
438 for my $c (sort keys %$newcol) {
439 if (!exists $oldcol->{$c}) {
440 print "Column $t.$c not in $oldfile\n";
443 ## Column types (roughly) match up?
444 my $new = $newcol->{$c};
445 my $old = $oldcolfull->{$c};
448 next if exists $colmapok{$c}{$old}{$new};
450 $old =~ s/ENUM.*/ENUM/;
451 if (! exists $colmap{$old}{$new}) {
452 print "Column types for $t.$c do not match: $old does not map to $new\n";
457 for (sort keys %new) {
458 if (!exists $old{$oldfile}{$_} and !exists $ok{NEW
}{$_}) {
459 print "Not in $oldfile: $_\n";
465 } ## end each file to be parsed
470 OLD
: searchindex
## We use tsearch2 directly on the page table instead
471 RENAME
: user mwuser
## Reserved word causing lots of problems
472 RENAME
: text pagecontent
## Reserved word
473 NEW
: mediawiki_version
## Just us, for now
474 XFILE
: ../archives/patch
-profiling
.sql