f8305a00d4636bca1a6793fe3b1d951524ace696
[lhc/web/wiklou.git] / maintenance / postgres / compare_schemas.pl
1 #!/usr/bin/perl
2
3 ## Rough check that the base and postgres "tables.sql" are in sync
4 ## Should be run from maintenance/postgres
5
6 use strict;
7 use warnings;
8 use Data::Dumper;
9
10 my @old = ("../tables.sql", "../mysql5/tables.sql");
11 my $new = "tables.sql";
12 my @xfile;
13
14 ## Read in exceptions and other metadata
15 my %ok;
16 while (<DATA>) {
17 next unless /^(\w+)\s*:\s*([^#]+)/;
18 my ($name,$val) = ($1,$2);
19 chomp $val;
20 if ($name eq 'RENAME') {
21 die "Invalid rename\n" unless $val =~ /(\w+)\s+(\w+)/;
22 $ok{OLD}{$1} = $2;
23 $ok{NEW}{$2} = $1;
24 next;
25 }
26 if ($name eq 'XFILE') {
27 push @xfile, $val;
28 next;
29 }
30 for (split(/\s+/ => $val)) {
31 $ok{$name}{$_} = 0;
32 }
33 }
34
35 my $datatype = join '|' => qw(
36 bool
37 tinyint int bigint real float
38 tinytext mediumtext text char varchar varbinary
39 timestamp datetime
40 tinyblob mediumblob blob
41 );
42 $datatype .= q{|ENUM\([\"\w, ]+\)};
43 $datatype = qr{($datatype)};
44
45 my $typeval = qr{(\(\d+\))?};
46
47 my $typeval2 = qr{ unsigned| binary| NOT NULL| NULL| auto_increment| default ['\-\d\w"]+| REFERENCES .+CASCADE};
48
49 my $indextype = join '|' => qw(INDEX KEY FULLTEXT), "PRIMARY KEY", "UNIQUE INDEX", "UNIQUE KEY";
50 $indextype = qr{$indextype};
51
52 my $engine = qr{TYPE|ENGINE};
53
54 my $tabletype = qr{InnoDB|MyISAM|HEAP|HEAP MAX_ROWS=\d+};
55
56 my $charset = qr{utf8};
57
58
59 open my $newfh, "<", $new or die qq{Could not open $new: $!\n};
60
61
62 my ($table,%old);
63
64 ## Read in the xfiles
65 my %xinfo;
66 for my $xfile (@xfile) {
67 print "Loading $xfile\n";
68 my $info = &parse_sql($xfile);
69 for (keys %$info) {
70 $xinfo{$_} = $info->{$_};
71 }
72 }
73
74 for my $oldfile (@old) {
75 print "Loading $oldfile\n";
76 my $info = &parse_sql($oldfile);
77 for (keys %xinfo) {
78 $info->{$_} = $xinfo{$_};
79 }
80 $old{$oldfile} = $info;
81 }
82
83 sub parse_sql {
84
85 my $oldfile = shift;
86
87 open my $oldfh, "<", $oldfile or die qq{Could not open $oldfile: $!\n};
88
89 my %info;
90 while (<$oldfh>) {
91 next if /^\s*\-\-/ or /^\s+$/;
92 s/\s*\-\- [\w ]+$//;
93 chomp;
94
95 if (/CREATE\s*TABLE/i) {
96 m{^CREATE TABLE /\*\$wgDBprefix\*/(\w+) \($}
97 or die qq{Invalid CREATE TABLE at line $. of $oldfile\n};
98 $table = $1;
99 $info{$table}{name}=$table;
100 }
101 elsif (/^\) ($engine)=($tabletype);$/) {
102 $info{$table}{engine}=$1;
103 $info{$table}{type}=$2;
104 }
105 elsif (/^\) ($engine)=($tabletype), DEFAULT CHARSET=($charset);$/) {
106 $info{$table}{engine}=$1;
107 $info{$table}{type}=$2;
108 $info{$table}{charset}=$3;
109 }
110 elsif (/^ (\w+) $datatype$typeval$typeval2{0,3},?$/) {
111 $info{$table}{column}{$1} = $2;
112 }
113 elsif (/^ ($indextype)(?: (\w+))? \(([\w, \(\)]+)\),?$/) {
114 $info{$table}{lc $1."_name"} = $2 ? $2 : "";
115 $info{$table}{lc $1."pk_target"} = $3;
116 }
117 else {
118 die "Cannot parse line $. of $oldfile:\n$_\n";
119 }
120
121 }
122 close $oldfh;
123
124 return \%info;
125
126 } ## end of parse_sql
127
128 for my $oldfile (@old) {
129
130 ## Begin non-standard indent
131
132 ## MySQL sanity checks
133 for my $table (sort keys %{$old{$oldfile}}) {
134 my $t = $old{$oldfile}{$table};
135 if (($oldfile =~ /5/ and $t->{engine} ne 'ENGINE')
136 or
137 ($oldfile !~ /5/ and $t->{engine} ne 'TYPE')) {
138 die "Invalid engine for $oldfile: $t->{engine}\n" unless $t->{name} eq 'profiling';
139 }
140 }
141
142 my $dtype = join '|' => qw(
143 SMALLINT INTEGER BIGINT NUMERIC SERIAL
144 TEXT CHAR VARCHAR
145 BYTEA
146 TIMESTAMPTZ
147 CIDR
148 );
149 $dtype = qr{($dtype)};
150 my %new;
151 my ($infunction,$inview,$inrule) = (0,0,0);
152 seek $newfh, 0, 0;
153 while (<$newfh>) {
154 next if /^\s*\-\-/ or /^\s*$/;
155 s/\s*\-\- [\w ']+$//;
156 next if /^BEGIN;/ or /^SET / or /^COMMIT;/;
157 next if /^CREATE SEQUENCE/;
158 next if /^CREATE(?: UNIQUE)? INDEX/;
159 next if /^CREATE FUNCTION/;
160 next if /^CREATE TRIGGER/ or /^ FOR EACH ROW/;
161 next if /^INSERT INTO/ or /^ VALUES \(/;
162 next if /^ALTER TABLE/;
163 chomp;
164
165 if (/^\$mw\$;?$/) {
166 $infunction = $infunction ? 0 : 1;
167 next;
168 }
169 next if $infunction;
170
171 next if /^CREATE VIEW/ and $inview = 1;
172 if ($inview) {
173 /;$/ and $inview = 0;
174 next;
175 }
176
177 next if /^CREATE RULE/ and $inrule = 1;
178 if ($inrule) {
179 /;$/ and $inrule = 0;
180 next;
181 }
182
183 if (/^CREATE TABLE "?(\w+)"? \($/) {
184 $table = $1;
185 $new{$table}{name}=$table;
186 }
187 elsif (/^\);$/) {
188 }
189 elsif (/^ (\w+) +$dtype/) {
190 $new{$table}{column}{$1} = $2;
191 }
192 else {
193 die "Cannot parse line $. of $new:\n$_\n";
194 }
195 }
196
197 ## Old but not new
198 for my $t (sort keys %{$old{$oldfile}}) {
199 if (!exists $new{$t} and !exists $ok{OLD}{$t}) {
200 print "Table not in $new: $t\n";
201 next;
202 }
203 next if exists $ok{OLD}{$t} and !$ok{OLD}{$t};
204 my $newt = exists $ok{OLD}{$t} ? $ok{OLD}{$t} : $t;
205 my $oldcol = $old{$oldfile}{$t}{column};
206 my $newcol = $new{$newt}{column};
207 for my $c (keys %$oldcol) {
208 if (!exists $newcol->{$c}) {
209 print "Column $t.$c not in new\n";
210 next;
211 }
212 }
213 for my $c (keys %$newcol) {
214 if (!exists $oldcol->{$c}) {
215 print "Column $t.$c not in old\n";
216 next;
217 }
218 }
219 }
220 ## New but not old:
221 for (sort keys %new) {
222 if (!exists $old{$oldfile}{$_} and !exists $ok{NEW}{$_}) {
223 print "Not in old: $_\n";
224 next;
225 }
226 }
227
228
229 } ## end each file to be parsed
230
231
232 __DATA__
233 ## Known exceptions
234 OLD: searchindex ## We use tsearch2 directly on the page table instead
235 OLD: archive ## This is a view due to the char(14) timestamp hack
236 RENAME: user mwuser ## Reserved word causing lots of problems
237 RENAME: text pagecontent ## Reserved word
238 NEW: archive2 ## The real archive table
239 NEW: mediawiki_version ## Just us, for now
240 XFILE: ../archives/patch-profiling.sql