Better output of filenames.
[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", "../mysql5/tables-binary.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|binary};
57
58 open my $newfh, "<", $new or die qq{Could not open $new: $!\n};
59
60
61 my ($table,%old);
62
63 ## Read in the xfiles
64 my %xinfo;
65 for my $xfile (@xfile) {
66 print "Loading $xfile\n";
67 my $info = &parse_sql($xfile);
68 for (keys %$info) {
69 $xinfo{$_} = $info->{$_};
70 }
71 }
72
73 for my $oldfile (@old) {
74 print "Loading $oldfile\n";
75 my $info = &parse_sql($oldfile);
76 for (keys %xinfo) {
77 $info->{$_} = $xinfo{$_};
78 }
79 $old{$oldfile} = $info;
80 }
81
82 sub parse_sql {
83
84 my $oldfile = shift;
85
86 open my $oldfh, "<", $oldfile or die qq{Could not open $oldfile: $!\n};
87
88 my %info;
89 while (<$oldfh>) {
90 next if /^\s*\-\-/ or /^\s+$/;
91 s/\s*\-\- [\w ]+$//;
92 chomp;
93
94 if (/CREATE\s*TABLE/i) {
95 m{^CREATE TABLE /\*\$wgDBprefix\*/(\w+) \($}
96 or die qq{Invalid CREATE TABLE at line $. of $oldfile\n};
97 $table = $1;
98 $info{$table}{name}=$table;
99 }
100 elsif (/^\) ($engine)=($tabletype);$/) {
101 $info{$table}{engine}=$1;
102 $info{$table}{type}=$2;
103 }
104 elsif (/^\) ($engine)=($tabletype), DEFAULT CHARSET=($charset);$/) {
105 $info{$table}{engine}=$1;
106 $info{$table}{type}=$2;
107 $info{$table}{charset}=$3;
108 }
109 elsif (/^ (\w+) $datatype$typeval$typeval2{0,3},?$/) {
110 $info{$table}{column}{$1} = $2;
111 }
112 elsif (/^ ($indextype)(?: (\w+))? \(([\w, \(\)]+)\),?$/) {
113 $info{$table}{lc $1."_name"} = $2 ? $2 : "";
114 $info{$table}{lc $1."pk_target"} = $3;
115 }
116 else {
117 die "Cannot parse line $. of $oldfile:\n$_\n";
118 }
119
120 }
121 close $oldfh;
122
123 return \%info;
124
125 } ## end of parse_sql
126
127 for my $oldfile (@old) {
128
129 ## Begin non-standard indent
130
131 ## MySQL sanity checks
132 for my $table (sort keys %{$old{$oldfile}}) {
133 my $t = $old{$oldfile}{$table};
134 if (($oldfile =~ /5/ and $t->{engine} ne 'ENGINE')
135 or
136 ($oldfile !~ /5/ and $t->{engine} ne 'TYPE')) {
137 die "Invalid engine for $oldfile: $t->{engine}\n" unless $t->{name} eq 'profiling';
138 }
139 my $charset = $t->{charset} || '';
140 if ($oldfile !~ /binary/ and $charset eq 'binary') {
141 die "Invalid charset for $oldfile: $charset\n";
142 }
143 }
144
145 my $dtype = join '|' => qw(
146 SMALLINT INTEGER BIGINT NUMERIC SERIAL
147 TEXT CHAR VARCHAR
148 BYTEA
149 TIMESTAMPTZ
150 CIDR
151 );
152 $dtype = qr{($dtype)};
153 my %new;
154 my ($infunction,$inview,$inrule) = (0,0,0);
155 seek $newfh, 0, 0;
156 while (<$newfh>) {
157 next if /^\s*\-\-/ or /^\s*$/;
158 s/\s*\-\- [\w ']+$//;
159 next if /^BEGIN;/ or /^SET / or /^COMMIT;/;
160 next if /^CREATE SEQUENCE/;
161 next if /^CREATE(?: UNIQUE)? INDEX/;
162 next if /^CREATE FUNCTION/;
163 next if /^CREATE TRIGGER/ or /^ FOR EACH ROW/;
164 next if /^INSERT INTO/ or /^ VALUES \(/;
165 next if /^ALTER TABLE/;
166 chomp;
167
168 if (/^\$mw\$;?$/) {
169 $infunction = $infunction ? 0 : 1;
170 next;
171 }
172 next if $infunction;
173
174 next if /^CREATE VIEW/ and $inview = 1;
175 if ($inview) {
176 /;$/ and $inview = 0;
177 next;
178 }
179
180 next if /^CREATE RULE/ and $inrule = 1;
181 if ($inrule) {
182 /;$/ and $inrule = 0;
183 next;
184 }
185
186 if (/^CREATE TABLE "?(\w+)"? \($/) {
187 $table = $1;
188 $new{$table}{name}=$table;
189 }
190 elsif (/^\);$/) {
191 }
192 elsif (/^ (\w+) +$dtype/) {
193 $new{$table}{column}{$1} = $2;
194 }
195 else {
196 die "Cannot parse line $. of $new:\n$_\n";
197 }
198 }
199
200 ## Old but not new
201 for my $t (sort keys %{$old{$oldfile}}) {
202 if (!exists $new{$t} and !exists $ok{OLD}{$t}) {
203 print "Table not in $new: $t\n";
204 next;
205 }
206 next if exists $ok{OLD}{$t} and !$ok{OLD}{$t};
207 my $newt = exists $ok{OLD}{$t} ? $ok{OLD}{$t} : $t;
208 my $oldcol = $old{$oldfile}{$t}{column};
209 my $newcol = $new{$newt}{column};
210 for my $c (keys %$oldcol) {
211 if (!exists $newcol->{$c}) {
212 print "Column $t.$c not in $new\n";
213 next;
214 }
215 }
216 for my $c (keys %$newcol) {
217 if (!exists $oldcol->{$c}) {
218 print "Column $t.$c not in $oldfile\n";
219 next;
220 }
221 }
222 }
223 ## New but not old:
224 for (sort keys %new) {
225 if (!exists $old{$oldfile}{$_} and !exists $ok{NEW}{$_}) {
226 print "Not in $oldfile: $_\n";
227 next;
228 }
229 }
230
231
232 } ## end each file to be parsed
233
234
235 __DATA__
236 ## Known exceptions
237 OLD: searchindex ## We use tsearch2 directly on the page table instead
238 OLD: archive ## This is a view due to the char(14) timestamp hack
239 RENAME: user mwuser ## Reserved word causing lots of problems
240 RENAME: text pagecontent ## Reserved word
241 NEW: archive2 ## The real archive table
242 NEW: mediawiki_version ## Just us, for now
243 XFILE: ../archives/patch-profiling.sql