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 for my $oldfile (@old) {
135 ## Begin non-standard indent
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')
142 ($oldfile !~ /5/ and $t->{engine
} ne 'TYPE')) {
143 die "Invalid engine for $oldfile: $t->{engine}\n" unless $t->{name
} eq 'profiling';
145 my $charset = $t->{charset
} || '';
146 if ($oldfile !~ /binary/ and $charset eq 'binary') {
147 die "Invalid charset for $oldfile: $charset\n";
151 my $dtype = join '|' => qw(
152 SMALLINT INTEGER BIGINT NUMERIC SERIAL
158 $dtype = qr{($dtype)};
160 my ($infunction,$inview,$inrule,$lastcomma) = (0,0,0,0);
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/;
175 $infunction = $infunction ?
0 : 1;
180 next if /^CREATE VIEW/ and $inview = 1;
182 /;$/ and $inview = 0;
186 next if /^CREATE RULE/ and $inrule = 1;
188 /;$/ and $inrule = 0;
192 if (/^CREATE TABLE "?(\w+)"? \($/) {
194 $new{$table}{name
}=$table;
199 warn "Stray comma before line $.\n";
202 elsif (/^ (\w+) +$dtype.*?(,?)(?: --.*)?$/) {
203 $new{$table}{column
}{$1} = $2;
205 print "Missing comma before line $. of $new\n";
207 $lastcomma = $3 ?
1 : 0;
210 die "Cannot parse line $. of $new:\n$_\n";
214 ## Which column types are okay to map from mysql to postgres?
233 varbinary(14) TIMESTAMPTZ
234 binary(14) TIMESTAMPTZ
236 timestamp TIMESTAMPTZ
245 ## Allow specific exceptions to the above
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
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 qc_type varbinary(32) TEXT
282 qcc_type varbinary(32) TEXT
283 qci_type varbinary(32) TEXT
285 ug_group varbinary(16) TEXT
286 user_email_token binary(32) TEXT
287 user_ip varbinary(40) TEXT
288 user_newpassword tinyblob TEXT
289 user_options blob TEXT
290 user_password tinyblob TEXT
291 user_token binary(32) TEXT
298 tc_url varbinary(255) TEXT
300 ## Deprecated or not yet used:
301 ar_text mediumblob TEXT
303 log_deleted tinyint INTEGER # Not used yet, but keep it INTEGER for safety
307 fa_bits int SMALLINT # bits per pixel
308 fa_height int SMALLINT
309 fa_width int SMALLINT # Hope we don't see an image this wide...
310 hc_id int BIGINT # Odd that site_stats is all bigint...
311 img_bits int SMALLINT # bits per image should stay sane
314 ## True binary fields, usually due to gzdeflate and/or serialize:
315 math_inputhash varbinary(16) BYTEA
316 math_outputhash varbinary(16) BYTEA
318 ## Namespaces: not need for such a high range
319 ar_namespace int SMALLINT
320 job_namespace int SMALLINT
321 log_namespace int SMALLINT
322 page_namespace int SMALLINT
323 pl_namespace int SMALLINT
324 qc_namespace int SMALLINT
325 rc_namespace int SMALLINT
326 rd_namespace int SMALLINT
327 tl_namespace int SMALLINT
328 wl_namespace int SMALLINT
331 ar_minor_edit tinyint CHAR
332 iw_trans tinyint CHAR
333 page_is_new tinyint CHAR
334 page_is_redirect tinyint CHAR
336 rc_deleted tinyint CHAR
337 rc_minor tinyint CHAR
339 rc_patrolled tinyint CHAR
340 rev_deleted tinyint CHAR
341 rev_minor_edit tinyint CHAR
343 ## Easy enough to change if a wiki ever does grow this big:
344 ss_good_articles bigint INTEGER
345 ss_total_edits bigint INTEGER
346 ss_total_pages bigint INTEGER
347 ss_total_views bigint INTEGER
348 ss_users bigint INTEGER
350 ## True IP - keep an eye on these, coders tend to make textual assumptions
351 rc_ip varbinary(40) CIDR # Want to keep an eye on this
354 tc_time int TIMESTAMPTZ
360 for (split /\n/ => $COLMAP) {
363 my ($col,@maps) = split / +/, $_;
365 $colmap{$col}{$_} = 1;
370 for (split /\n/ => $COLMAPOK) {
372 my ($col,$old,$new) = split / +/, $_;
373 $colmapok{$col}{$old}{$new} = 1;
377 for my $t (sort keys %{$old{$oldfile}}) {
378 if (!exists $new{$t} and !exists $ok{OLD
}{$t}) {
379 print "Table not in $new: $t\n";
382 next if exists $ok{OLD
}{$t} and !$ok{OLD
}{$t};
383 my $newt = exists $ok{OLD
}{$t} ?
$ok{OLD
}{$t} : $t;
384 my $oldcol = $old{$oldfile}{$t}{column
};
385 my $oldcolfull = $old{$oldfile}{$t}{columnfull
};
386 my $newcol = $new{$newt}{column
};
387 for my $c (keys %$oldcol) {
388 if (!exists $newcol->{$c}) {
389 print "Column $t.$c not in $new\n";
393 for my $c (sort keys %$newcol) {
394 if (!exists $oldcol->{$c}) {
395 print "Column $t.$c not in $oldfile\n";
398 ## Column types (roughly) match up?
399 my $new = $newcol->{$c};
400 my $old = $oldcolfull->{$c};
403 next if exists $colmapok{$c}{$old}{$new};
405 $old =~ s/ENUM.*/ENUM/;
406 if (! exists $colmap{$old}{$new}) {
407 print "Column types for $t.$c do not match: $old does not map to $new\n";
412 for (sort keys %new) {
413 if (!exists $old{$oldfile}{$_} and !exists $ok{NEW
}{$_}) {
414 print "Not in $oldfile: $_\n";
420 } ## end each file to be parsed
425 OLD
: searchindex
## We use tsearch2 directly on the page table instead
426 RENAME
: user mwuser
## Reserved word causing lots of problems
427 RENAME
: text pagecontent
## Reserved word
428 NEW
: mediawiki_version
## Just us, for now
429 XFILE
: ../archives/patch
-profiling
.sql