r17497 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r17496‎ | r17497 | r17498 >
Date:22:54, 8 November 2006
Author:greg
Status:old
Tags:
Comment:
Compare both schemas, be a little more verbose, add some other checks.
Modified paths:
  • /trunk/phase3/maintenance/postgres/compare_schemas.pl (modified) (history)

Diff [purge]

Index: trunk/phase3/maintenance/postgres/compare_schemas.pl
@@ -7,8 +7,9 @@
88 use warnings;
99 use Data::Dumper;
1010
11 -my @old = ("../tables.sql");
 11+my @old = ("../tables.sql", "../mysql5/tables.sql");
1212 my $new = "tables.sql";
 13+my @xfile;
1314
1415 ## Read in exceptions and other metadata
1516 my %ok;
@@ -23,7 +24,7 @@
2425 next;
2526 }
2627 if ($name eq 'XFILE') {
27 - push @old, $val;
 28+ push @xfile, $val;
2829 next;
2930 }
3031 for (split(/\s+/ => $val)) {
@@ -31,12 +32,10 @@
3233 }
3334 }
3435
35 -open my $newfh, "<", $new or die qq{Could not open $new: $!\n};
36 -
3736 my $datatype = join '|' => qw(
3837 bool
3938 tinyint int bigint real float
40 -tinytext mediumtext text char varchar
 39+tinytext mediumtext text char varchar varbinary
4140 timestamp datetime
4241 tinyblob mediumblob blob
4342 );
@@ -50,12 +49,44 @@
5150 my $indextype = join '|' => qw(INDEX KEY FULLTEXT), "PRIMARY KEY", "UNIQUE INDEX", "UNIQUE KEY";
5251 $indextype = qr{$indextype};
5352
 53+my $engine = qr{TYPE|ENGINE};
 54+
5455 my $tabletype = qr{InnoDB|MyISAM|HEAP|HEAP MAX_ROWS=\d+};
5556
 57+my $charset = qr{utf8};
 58+
 59+
 60+open my $newfh, "<", $new or die qq{Could not open $new: $!\n};
 61+
 62+
5663 my ($table,%old);
57 -for my $old (@old) {
58 - open my $oldfh, "<", $old or die qq{Could not open $old: $!\n};
5964
 65+## Read in the xfiles
 66+my %xinfo;
 67+for my $xfile (@xfile) {
 68+ print "Loading $xfile\n";
 69+ my $info = &parse_sql($xfile);
 70+ for (keys %$info) {
 71+ $xinfo{$_} = $info->{$_};
 72+ }
 73+}
 74+
 75+for my $oldfile (@old) {
 76+ print "Loading $oldfile\n";
 77+ my $info = &parse_sql($oldfile);
 78+ for (keys %xinfo) {
 79+ $info->{$_} = $xinfo{$_};
 80+ }
 81+ $old{$oldfile} = $info;
 82+}
 83+
 84+sub parse_sql {
 85+
 86+ my $oldfile = shift;
 87+
 88+ open my $oldfh, "<", $oldfile or die qq{Could not open $oldfile: $!\n};
 89+
 90+ my %info;
6091 while (<$oldfh>) {
6192 next if /^\s*\-\-/ or /^\s+$/;
6293 s/\s*\-\- [\w ]+$//;
@@ -63,37 +94,62 @@
6495
6596 if (/CREATE\s*TABLE/i) {
6697 m{^CREATE TABLE /\*\$wgDBprefix\*/(\w+) \($}
67 - or die qq{Invalid CREATE TABLE at line $. of $old\n};
 98+ or die qq{Invalid CREATE TABLE at line $. of $oldfile\n};
6899 $table = $1;
69 - $old{$table}{name}=$table;
 100+ $info{$table}{name}=$table;
70101 }
71 - elsif (/^\) TYPE=($tabletype);$/) {
72 - $old{$table}{type}=$1;
 102+ elsif (/^\) ($engine)=($tabletype);$/) {
 103+ $info{$table}{engine}=$1;
 104+ $info{$table}{type}=$2;
73105 }
 106+ elsif (/^\) ($engine)=($tabletype), DEFAULT CHARSET=($charset);$/) {
 107+ $info{$table}{engine}=$1;
 108+ $info{$table}{type}=$2;
 109+ $info{$table}{charset}=$3;
 110+ }
74111 elsif (/^ (\w+) $datatype$typeval$typeval2{0,3},?$/) {
75 - $old{$table}{column}{$1} = $2;
 112+ $info{$table}{column}{$1} = $2;
76113 }
77114 elsif (/^ ($indextype)(?: (\w+))? \(([\w, \(\)]+)\),?$/) {
78 - $old{$table}{lc $1."_name"} = $2 ? $2 : "";
79 - $old{$table}{lc $1."pk_target"} = $3;
 115+ $info{$table}{lc $1."_name"} = $2 ? $2 : "";
 116+ $info{$table}{lc $1."pk_target"} = $3;
80117 }
81118 else {
82 - die "Cannot parse line $. of $old:\n$_\n";
 119+ die "Cannot parse line $. of $oldfile:\n$_\n";
83120 }
 121+
84122 }
85123 close $oldfh;
 124+
 125+ return \%info;
 126+
 127+} ## end of parse_sql
 128+
 129+for my $oldfile (@old) {
 130+
 131+## Begin non-standard indent
 132+
 133+## MySQL sanity checks
 134+for my $table (sort keys %{$old{$oldfile}}) {
 135+ my $t = $old{$oldfile}{$table};
 136+ if (($oldfile =~ /5/ and $t->{engine} ne 'ENGINE')
 137+ or
 138+ ($oldfile !~ /5/ and $t->{engine} ne 'TYPE')) {
 139+ die "Invalid engine for $oldfile: $t->{engine}\n" unless $t->{name} eq 'profiling';
 140+ }
86141 }
87142
88 -$datatype = join '|' => qw(
 143+my $dtype = join '|' => qw(
89144 SMALLINT INTEGER BIGINT NUMERIC SERIAL
90145 TEXT CHAR VARCHAR
91146 BYTEA
92147 TIMESTAMPTZ
93148 CIDR
94149 );
95 -$datatype = qr{($datatype)};
 150+$dtype = qr{($dtype)};
96151 my %new;
97152 my ($infunction,$inview,$inrule) = (0,0,0);
 153+seek $newfh, 0, 0;
98154 while (<$newfh>) {
99155 next if /^\s*\-\-/ or /^\s*$/;
100156 s/\s*\-\- [\w ']+$//;
@@ -130,24 +186,23 @@
131187 }
132188 elsif (/^\);$/) {
133189 }
134 - elsif (/^ (\w+) +$datatype/) {
 190+ elsif (/^ (\w+) +$dtype/) {
135191 $new{$table}{column}{$1} = $2;
136192 }
137193 else {
138194 die "Cannot parse line $. of $new:\n$_\n";
139195 }
140196 }
141 -close $newfh;
142197
143198 ## Old but not new
144 -for my $t (sort keys %old) {
 199+for my $t (sort keys %{$old{$oldfile}}) {
145200 if (!exists $new{$t} and !exists $ok{OLD}{$t}) {
146201 print "Table not in $new: $t\n";
147202 next;
148203 }
149204 next if exists $ok{OLD}{$t} and !$ok{OLD}{$t};
150205 my $newt = exists $ok{OLD}{$t} ? $ok{OLD}{$t} : $t;
151 - my $oldcol = $old{$t}{column};
 206+ my $oldcol = $old{$oldfile}{$t}{column};
152207 my $newcol = $new{$newt}{column};
153208 for my $c (keys %$oldcol) {
154209 if (!exists $newcol->{$c}) {
@@ -164,12 +219,16 @@
165220 }
166221 ## New but not old:
167222 for (sort keys %new) {
168 - if (!exists $old{$_} and !exists $ok{NEW}{$_}) {
 223+ if (!exists $old{$oldfile}{$_} and !exists $ok{NEW}{$_}) {
169224 print "Not in old: $_\n";
170225 next;
171226 }
172227 }
173228
 229+
 230+} ## end each file to be parsed
 231+
 232+
174233 __DATA__
175234 ## Known exceptions
176235 OLD: searchindex ## We use tsearch2 directly on the page table instead