2006-07-23 01:01:58 +00:00
|
|
|
#!/usr/bin/perl
|
|
|
|
|
|
|
|
|
|
## Rough check that the base and postgres "tables.sql" are in sync
|
|
|
|
|
## Should be run from maintenance/postgres
|
|
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
|
use warnings;
|
|
|
|
|
use Data::Dumper;
|
|
|
|
|
|
2007-04-23 18:33:38 +00:00
|
|
|
my @old = ('../tables.sql');
|
|
|
|
|
my $new = 'tables.sql';
|
2006-11-08 22:54:58 +00:00
|
|
|
my @xfile;
|
2006-07-23 01:01:58 +00:00
|
|
|
|
2006-07-30 23:49:25 +00:00
|
|
|
## Read in exceptions and other metadata
|
|
|
|
|
my %ok;
|
|
|
|
|
while (<DATA>) {
|
|
|
|
|
next unless /^(\w+)\s*:\s*([^#]+)/;
|
|
|
|
|
my ($name,$val) = ($1,$2);
|
|
|
|
|
chomp $val;
|
|
|
|
|
if ($name eq 'RENAME') {
|
|
|
|
|
die "Invalid rename\n" unless $val =~ /(\w+)\s+(\w+)/;
|
|
|
|
|
$ok{OLD}{$1} = $2;
|
|
|
|
|
$ok{NEW}{$2} = $1;
|
|
|
|
|
next;
|
|
|
|
|
}
|
|
|
|
|
if ($name eq 'XFILE') {
|
2006-11-08 22:54:58 +00:00
|
|
|
push @xfile, $val;
|
2006-07-30 23:49:25 +00:00
|
|
|
next;
|
|
|
|
|
}
|
2007-04-23 18:33:38 +00:00
|
|
|
for (split /\s+/ => $val) {
|
2006-07-30 23:49:25 +00:00
|
|
|
$ok{$name}{$_} = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2006-07-23 01:01:58 +00:00
|
|
|
my $datatype = join '|' => qw(
|
|
|
|
|
bool
|
2006-07-30 23:49:25 +00:00
|
|
|
tinyint int bigint real float
|
2007-07-05 13:41:47 +00:00
|
|
|
tinytext mediumtext text char varchar varbinary binary
|
2006-07-23 01:01:58 +00:00
|
|
|
timestamp datetime
|
|
|
|
|
tinyblob mediumblob blob
|
|
|
|
|
);
|
|
|
|
|
$datatype .= q{|ENUM\([\"\w, ]+\)};
|
|
|
|
|
$datatype = qr{($datatype)};
|
|
|
|
|
|
|
|
|
|
my $typeval = qr{(\(\d+\))?};
|
|
|
|
|
|
|
|
|
|
my $typeval2 = qr{ unsigned| binary| NOT NULL| NULL| auto_increment| default ['\-\d\w"]+| REFERENCES .+CASCADE};
|
|
|
|
|
|
2007-04-23 18:33:38 +00:00
|
|
|
my $indextype = join '|' => qw(INDEX KEY FULLTEXT), 'PRIMARY KEY', 'UNIQUE INDEX', 'UNIQUE KEY';
|
2006-07-23 01:01:58 +00:00
|
|
|
$indextype = qr{$indextype};
|
|
|
|
|
|
2006-11-08 22:54:58 +00:00
|
|
|
my $engine = qr{TYPE|ENGINE};
|
|
|
|
|
|
2007-02-17 20:06:37 +00:00
|
|
|
my $tabletype = qr{InnoDB|MyISAM|HEAP|HEAP MAX_ROWS=\d+|InnoDB MAX_ROWS=\d+ AVG_ROW_LENGTH=\d+};
|
2006-07-23 01:01:58 +00:00
|
|
|
|
2006-11-14 03:03:44 +00:00
|
|
|
my $charset = qr{utf8|binary};
|
2006-11-08 22:54:58 +00:00
|
|
|
|
2007-04-23 18:33:38 +00:00
|
|
|
open my $newfh, '<', $new or die qq{Could not open $new: $!\n};
|
2006-11-08 22:54:58 +00:00
|
|
|
|
|
|
|
|
|
2006-07-23 01:01:58 +00:00
|
|
|
my ($table,%old);
|
2006-07-30 23:49:25 +00:00
|
|
|
|
2006-11-08 22:54:58 +00:00
|
|
|
## Read in the xfiles
|
|
|
|
|
my %xinfo;
|
|
|
|
|
for my $xfile (@xfile) {
|
|
|
|
|
print "Loading $xfile\n";
|
|
|
|
|
my $info = &parse_sql($xfile);
|
|
|
|
|
for (keys %$info) {
|
|
|
|
|
$xinfo{$_} = $info->{$_};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for my $oldfile (@old) {
|
|
|
|
|
print "Loading $oldfile\n";
|
|
|
|
|
my $info = &parse_sql($oldfile);
|
|
|
|
|
for (keys %xinfo) {
|
|
|
|
|
$info->{$_} = $xinfo{$_};
|
|
|
|
|
}
|
|
|
|
|
$old{$oldfile} = $info;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sub parse_sql {
|
|
|
|
|
|
|
|
|
|
my $oldfile = shift;
|
|
|
|
|
|
2007-04-23 18:33:38 +00:00
|
|
|
open my $oldfh, '<', $oldfile or die qq{Could not open $oldfile: $!\n};
|
2006-11-08 22:54:58 +00:00
|
|
|
|
|
|
|
|
my %info;
|
2006-07-30 23:49:25 +00:00
|
|
|
while (<$oldfh>) {
|
|
|
|
|
next if /^\s*\-\-/ or /^\s+$/;
|
|
|
|
|
s/\s*\-\- [\w ]+$//;
|
|
|
|
|
chomp;
|
|
|
|
|
|
|
|
|
|
if (/CREATE\s*TABLE/i) {
|
|
|
|
|
m{^CREATE TABLE /\*\$wgDBprefix\*/(\w+) \($}
|
2006-11-08 22:54:58 +00:00
|
|
|
or die qq{Invalid CREATE TABLE at line $. of $oldfile\n};
|
2006-07-30 23:49:25 +00:00
|
|
|
$table = $1;
|
2006-11-08 22:54:58 +00:00
|
|
|
$info{$table}{name}=$table;
|
2006-07-30 23:49:25 +00:00
|
|
|
}
|
2007-04-23 18:33:38 +00:00
|
|
|
elsif (m#^\) /\*\$wgDBTableOptions\*/#) {
|
|
|
|
|
$info{$table}{engine} = 'TYPE';
|
|
|
|
|
$info{$table}{type} = 'variable';
|
|
|
|
|
}
|
2006-11-08 22:54:58 +00:00
|
|
|
elsif (/^\) ($engine)=($tabletype);$/) {
|
|
|
|
|
$info{$table}{engine}=$1;
|
|
|
|
|
$info{$table}{type}=$2;
|
|
|
|
|
}
|
|
|
|
|
elsif (/^\) ($engine)=($tabletype), DEFAULT CHARSET=($charset);$/) {
|
|
|
|
|
$info{$table}{engine}=$1;
|
|
|
|
|
$info{$table}{type}=$2;
|
|
|
|
|
$info{$table}{charset}=$3;
|
2006-07-30 23:49:25 +00:00
|
|
|
}
|
|
|
|
|
elsif (/^ (\w+) $datatype$typeval$typeval2{0,3},?$/) {
|
2006-11-08 22:54:58 +00:00
|
|
|
$info{$table}{column}{$1} = $2;
|
2006-07-30 23:49:25 +00:00
|
|
|
}
|
|
|
|
|
elsif (/^ ($indextype)(?: (\w+))? \(([\w, \(\)]+)\),?$/) {
|
2007-04-23 18:33:38 +00:00
|
|
|
$info{$table}{lc $1.'_name'} = $2 ? $2 : '';
|
|
|
|
|
$info{$table}{lc $1.'pk_target'} = $3;
|
2006-07-30 23:49:25 +00:00
|
|
|
}
|
|
|
|
|
else {
|
2006-11-08 22:54:58 +00:00
|
|
|
die "Cannot parse line $. of $oldfile:\n$_\n";
|
2006-07-30 23:49:25 +00:00
|
|
|
}
|
2006-11-08 22:54:58 +00:00
|
|
|
|
2006-07-23 01:01:58 +00:00
|
|
|
}
|
2006-07-30 23:49:25 +00:00
|
|
|
close $oldfh;
|
2006-11-08 22:54:58 +00:00
|
|
|
|
|
|
|
|
return \%info;
|
|
|
|
|
|
|
|
|
|
} ## end of parse_sql
|
|
|
|
|
|
|
|
|
|
for my $oldfile (@old) {
|
|
|
|
|
|
|
|
|
|
## Begin non-standard indent
|
|
|
|
|
|
|
|
|
|
## MySQL sanity checks
|
|
|
|
|
for my $table (sort keys %{$old{$oldfile}}) {
|
|
|
|
|
my $t = $old{$oldfile}{$table};
|
|
|
|
|
if (($oldfile =~ /5/ and $t->{engine} ne 'ENGINE')
|
|
|
|
|
or
|
|
|
|
|
($oldfile !~ /5/ and $t->{engine} ne 'TYPE')) {
|
|
|
|
|
die "Invalid engine for $oldfile: $t->{engine}\n" unless $t->{name} eq 'profiling';
|
|
|
|
|
}
|
2006-11-14 03:03:44 +00:00
|
|
|
my $charset = $t->{charset} || '';
|
|
|
|
|
if ($oldfile !~ /binary/ and $charset eq 'binary') {
|
|
|
|
|
die "Invalid charset for $oldfile: $charset\n";
|
|
|
|
|
}
|
2006-07-23 01:01:58 +00:00
|
|
|
}
|
|
|
|
|
|
2006-11-08 22:54:58 +00:00
|
|
|
my $dtype = join '|' => qw(
|
2006-07-23 01:01:58 +00:00
|
|
|
SMALLINT INTEGER BIGINT NUMERIC SERIAL
|
|
|
|
|
TEXT CHAR VARCHAR
|
|
|
|
|
BYTEA
|
|
|
|
|
TIMESTAMPTZ
|
|
|
|
|
CIDR
|
|
|
|
|
);
|
2006-11-08 22:54:58 +00:00
|
|
|
$dtype = qr{($dtype)};
|
2006-07-23 01:01:58 +00:00
|
|
|
my %new;
|
2007-06-17 02:09:21 +00:00
|
|
|
my ($infunction,$inview,$inrule,$lastcomma) = (0,0,0,0);
|
2006-11-08 22:54:58 +00:00
|
|
|
seek $newfh, 0, 0;
|
2006-07-23 01:01:58 +00:00
|
|
|
while (<$newfh>) {
|
|
|
|
|
next if /^\s*\-\-/ or /^\s*$/;
|
2006-07-23 02:05:21 +00:00
|
|
|
s/\s*\-\- [\w ']+$//;
|
2006-07-23 01:01:58 +00:00
|
|
|
next if /^BEGIN;/ or /^SET / or /^COMMIT;/;
|
|
|
|
|
next if /^CREATE SEQUENCE/;
|
|
|
|
|
next if /^CREATE(?: UNIQUE)? INDEX/;
|
|
|
|
|
next if /^CREATE FUNCTION/;
|
|
|
|
|
next if /^CREATE TRIGGER/ or /^ FOR EACH ROW/;
|
|
|
|
|
next if /^INSERT INTO/ or /^ VALUES \(/;
|
|
|
|
|
next if /^ALTER TABLE/;
|
|
|
|
|
chomp;
|
|
|
|
|
|
|
|
|
|
if (/^\$mw\$;?$/) {
|
|
|
|
|
$infunction = $infunction ? 0 : 1;
|
|
|
|
|
next;
|
|
|
|
|
}
|
|
|
|
|
next if $infunction;
|
|
|
|
|
|
|
|
|
|
next if /^CREATE VIEW/ and $inview = 1;
|
|
|
|
|
if ($inview) {
|
|
|
|
|
/;$/ and $inview = 0;
|
|
|
|
|
next;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
next if /^CREATE RULE/ and $inrule = 1;
|
|
|
|
|
if ($inrule) {
|
|
|
|
|
/;$/ and $inrule = 0;
|
|
|
|
|
next;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (/^CREATE TABLE "?(\w+)"? \($/) {
|
|
|
|
|
$table = $1;
|
|
|
|
|
$new{$table}{name}=$table;
|
2007-06-17 02:09:21 +00:00
|
|
|
$lastcomma = 1;
|
2006-07-23 01:01:58 +00:00
|
|
|
}
|
|
|
|
|
elsif (/^\);$/) {
|
2007-06-17 02:09:21 +00:00
|
|
|
if ($lastcomma) {
|
|
|
|
|
warn "Stray comma before line $.\n";
|
|
|
|
|
}
|
2006-07-23 01:01:58 +00:00
|
|
|
}
|
2007-06-17 02:09:21 +00:00
|
|
|
elsif (/^ (\w+) +$dtype.*?(,?)(?: --.*)?$/) {
|
2006-07-23 01:01:58 +00:00
|
|
|
$new{$table}{column}{$1} = $2;
|
2007-06-17 02:09:21 +00:00
|
|
|
if (!$lastcomma) {
|
|
|
|
|
print "Missing comma before line $. of $new\n";
|
|
|
|
|
}
|
|
|
|
|
$lastcomma = $3 ? 1 : 0;
|
2006-07-23 01:01:58 +00:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
die "Cannot parse line $. of $new:\n$_\n";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
## Old but not new
|
2006-11-08 22:54:58 +00:00
|
|
|
for my $t (sort keys %{$old{$oldfile}}) {
|
2006-07-23 01:01:58 +00:00
|
|
|
if (!exists $new{$t} and !exists $ok{OLD}{$t}) {
|
|
|
|
|
print "Table not in $new: $t\n";
|
|
|
|
|
next;
|
|
|
|
|
}
|
2006-07-25 00:28:03 +00:00
|
|
|
next if exists $ok{OLD}{$t} and !$ok{OLD}{$t};
|
|
|
|
|
my $newt = exists $ok{OLD}{$t} ? $ok{OLD}{$t} : $t;
|
2006-11-08 22:54:58 +00:00
|
|
|
my $oldcol = $old{$oldfile}{$t}{column};
|
2006-07-25 00:28:03 +00:00
|
|
|
my $newcol = $new{$newt}{column};
|
2006-07-23 01:01:58 +00:00
|
|
|
for my $c (keys %$oldcol) {
|
|
|
|
|
if (!exists $newcol->{$c}) {
|
2007-01-11 15:56:37 +00:00
|
|
|
print "Column $t.$c not in $new\n";
|
2006-07-23 01:01:58 +00:00
|
|
|
next;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for my $c (keys %$newcol) {
|
|
|
|
|
if (!exists $oldcol->{$c}) {
|
2007-01-11 15:56:37 +00:00
|
|
|
print "Column $t.$c not in $oldfile\n";
|
2006-07-23 01:01:58 +00:00
|
|
|
next;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
## New but not old:
|
|
|
|
|
for (sort keys %new) {
|
2006-11-08 22:54:58 +00:00
|
|
|
if (!exists $old{$oldfile}{$_} and !exists $ok{NEW}{$_}) {
|
2007-01-11 15:56:37 +00:00
|
|
|
print "Not in $oldfile: $_\n";
|
2006-07-23 01:01:58 +00:00
|
|
|
next;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2006-11-08 22:54:58 +00:00
|
|
|
|
|
|
|
|
} ## end each file to be parsed
|
|
|
|
|
|
|
|
|
|
|
2006-07-23 01:01:58 +00:00
|
|
|
__DATA__
|
|
|
|
|
## Known exceptions
|
|
|
|
|
OLD: searchindex ## We use tsearch2 directly on the page table instead
|
2006-07-25 00:28:03 +00:00
|
|
|
RENAME: user mwuser ## Reserved word causing lots of problems
|
|
|
|
|
RENAME: text pagecontent ## Reserved word
|
2006-07-23 01:01:58 +00:00
|
|
|
NEW: mediawiki_version ## Just us, for now
|
2006-07-30 23:49:25 +00:00
|
|
|
XFILE: ../archives/patch-profiling.sql
|