push_master_tables.pl 10.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#!/usr/bin/env perl

use strict;
use warnings;

use Data::Dumper;
use DBI qw( :sql_types );
use Getopt::Long qw( :config no_ignore_case );
use IO::File;
use POSIX qw( floor ceil );

sub usage {
  my $padding = ' ' x length($0);

  print <<USAGE_END;
Usage:
  $0 --release NN --master master_server \\
  $padding --server server1 --server server2 [...] \\
19
  $padding --dbport 3306 --dbuser user --dbpass passwd
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127

or
  $0 --help

or
  $0 --about


where

  --release/-r  The current release (required).
  --master/-m   The master server where the production database lives
                (optional, default is 'ens-staging1').
  --server/-s   A database server (optional, may occur several times,
                default is 'ens-staging1' and 'ens-staging2').
  --dbport/-P   The port to connect to (optional, default is '3306').
  --dbuser/-u   The use to connect as (optional, default is 'ensro').
  --dbpass/-p   The password to connect with (optional, no default).

  --help/-h     Displays this help text.

  --about/-a    Display a text about this program (what it does etc.).
USAGE_END
} ## end sub usage

sub about {
  print <<ABOUT_END;
Run the program with --help to get information about available command
line switches.

This program takes the master tables from the production database
and compares it to the corresponding tables on the given servers (by
default, the staging servers).

The program will display any discrepancies on the display
while writing SQL to files in the current directory that will
correct the discrepancies.

Each SQL patch file will have the generic name "fix-DBNAME.sql"
where "DBNAME" is the name of the database, e.g.,
"fix-oryctolagus_cuniculus_otherfeatures_60_3.sql".

A discrepancy is patched by

1)  Insertion into the master table in the production database in the
    case where a new entry has been added to a database without being
    added to the master table.

2)  Insertion into the database table in the case where a new master
    entry is missing in the database.

3)  Updating the database entry in the case where an entry (identified by
    its primary key only) differs in any of its fields.

The SQL patch files may then be used to patch the databases:

  \$ mysql -h server -u user -ppass < fix-DBNAME.sql




                    BE SURE TO REVIEW THESE SQL PATCH FILES
                        (along with the program output)

                            WITH YOUR EYE AND BRAIN

                              BEFORE APPLYING THEM


ABOUT_END
} ## end sub about

sub fetch_table {
  my ( $dbh, $dbname, $table ) = @_;

  my $sth = $dbh->prepare(
             sprintf( 'SELECT * FROM %s',
                      $dbh->quote_identifier( undef, $dbname, $table ) )
  );

  $sth->execute();

  my %table_hash;

  $table =~ s/^master_//;

  my $pk = sprintf( '%s_id', $table );
  while ( my $row = $sth->fetchrow_hashref() ) {
    if ( !exists( $row->{$pk} ) ) {
      die( sprintf( "Can not find expected primary key '%s'", $pk ) );
    }

    $table_hash{ $row->{$pk} } = $row;
  }

  return \%table_hash;
} ## end sub fetch_table

sub display_banner {
  my ( $char, $text ) = @_;

  printf( "%s %s %s\n",
          $char x ( 39 - floor( length($text)/2 ) ),
          $text, $char x ( 39 - ceil( length($text)/2 ) ) );
}

my $release;
my @servers = ( 'ens-staging1', 'ens-staging2' );
128
my $master = 'ens-staging1';
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354

my $dbport = '3306';
my $dbuser = 'ensro';
my $dbpass;

my $opt_help  = 0;
my $opt_about = 0;

if ( !GetOptions( 'release|r=i' => \$release,
                  'master|m=s'  => \$master,
                  'server|s=s@' => \@servers,
                  'dbuser|u=s'  => \$dbuser,
                  'dbpass|p=s'  => \$dbpass,
                  'dbport|P=s'  => \$dbport,
                  'help|h!'     => \$opt_help,
                  'about!'      => \$opt_about )
     || $opt_help )
{
  usage();
  exit();
} elsif ($opt_about) {
  about();
  exit();
} elsif ( !defined($release) ) {
  print("ERROR: Release was not specified! (use -r or --release)\n");
  usage();
  exit();
}

my @tables = ( 'attrib_type', 'external_db', 'misc_set' );
my @dbtypes = ( 'core', 'otherfeatures', 'cdna', 'vega' );

my @db_handles;

my %master;
{
  my $dsn = sprintf( "DBI:mysql:host=%s;port=%d", $master, $dbport );
  my $dbh =
    DBI->connect( $dsn, $dbuser, $dbpass, { 'PrintError' => 1 } );

  foreach my $table (@tables) {
    my $master_table = sprintf( 'master_%s', $table );
    $master{$table} = fetch_table( $dbh,
                                   sprintf( 'ensembl_production_%d',
                                            $release ),
                                   $master_table );
  }
}

foreach my $server (@servers) {
  my $dsn = sprintf( "DBI:mysql:host=%s;port=%d", $server, $dbport );
  my $dbh =
    DBI->connect( $dsn, $dbuser, $dbpass, { 'PrintError' => 1 } );

  push( @db_handles, $dbh );
}

my %sql;
foreach my $dbh (@db_handles) {
  my $sth = $dbh->prepare('SHOW DATABASES LIKE ?');

  foreach my $dbtype (@dbtypes) {
    $sth->bind_param( 1,
                      sprintf( '%%\\_%s\\_%d\\_%%', $dbtype, $release ),
                      SQL_VARCHAR );

    $sth->execute();

    my $dbname;
    $sth->bind_col( 1, \$dbname );

    while ( $sth->fetch() ) {
      foreach my $table (@tables) {
        my $csth = $dbh->column_info( undef, $dbname, $table, '%' );
        my $colinfo = $csth->fetchall_hashref( ['COLUMN_NAME'] );

        my %table = %{ fetch_table( $dbh, $dbname, $table ) };

        foreach
          my $pk ( sort { $a <=> $b } keys( %{ $master{$table} } ) )
        {
          if ( !exists( $table{$pk} ) ) {
            my $row    = $master{$table}{$pk};
            my @fields = sort( keys( %{$row} ) );

            push(
              @{ $sql{$dbname} },
              sprintf( "-- insert %s_id=%d in %s\n",
                       $table, $pk, $table ),
              sprintf(
                "INSERT INTO %s (%s) VALUES (%s);\n",
                $dbh->quote_identifier( undef, $dbname, $table ),
                join( ',', map { $dbh->quote_identifier($_) } @fields ),
                join(
                  ',',
                  map {
                    $dbh->quote( $row->{$_},
                                 $colinfo->{$_}{'DATA_TYPE'} )
                    } @fields ) ) );

          }
        }

        foreach my $pk ( sort { $a <=> $b } keys(%table) ) {
          my $master_row = $master{$table}{$pk};
          my $row        = $table{$pk};

          if ( $pk == 0 ) {
            display_banner( '-', sprintf( "%s.%s", $dbname, $table ) );

            print( "==> Primary key is ZERO "
                     . "for the following row in DATABASE:\n",
                   Dumper($row),
                   "\n" );
          } else {
            my @fields = sort( keys( %{$row} ) );

            if ( !defined($master_row) ) {
              display_banner( '=',
                              sprintf( "%s.%s", $dbname, $table ) );

              print( "==> The following row is MISSING IN MASTER:\n",
                     Dumper($row) );

              push(
                @{ $sql{$dbname} },
                sprintf( "-- MASTER: insert from %s.%s\n",
                         $dbname, $table ),
                sprintf(
                  "INSERT INTO %s (%s) VALUES (%s);\n",
                  $dbh->quote_identifier(
                           undef,
                           sprintf( 'ensembl_production_%d', $release ),
                           sprintf( 'master_%s',             $table ) ),
                  join( ',',
                        map { $dbh->quote_identifier($_) } @fields ),
                  join(
                    ',',
                    map {
                      $dbh->quote( $row->{$_},
                                   $colinfo->{$_}{'DATA_TYPE'} )
                      } @fields ) ) );

              print("\n");
            } else {
              my %diff_fields;

              foreach my $field (@fields) {
                if (    defined( $master_row->{$field} )
                     || defined( $row->{$field} ) )
                {
                  if ( (   !defined( $master_row->{$field} )
                         && defined( $row->{$field} ) )
                       || ( defined( $master_row->{$field} )
                            && !defined( $row->{$field} ) )
                       || ( $master_row->{$field} ne $row->{$field} ) )
                  {
                    if ( !(    $table eq 'external_db'
                            && $field eq 'db_release' ) )
                    {
                      $diff_fields{$field} = $master_row->{$field};
                    }
                  }
                }
              }

              if ( scalar( keys(%diff_fields) ) > 0 ) {
                display_banner( '=',
                                sprintf( "%s.%s", $dbname, $table ) );

                printf( "==> The following row differs in %s.\n",
                        join( ', ', keys(%diff_fields) ) );
                print( "==> MASTER row:\n",   Dumper($master_row),
                       "==> DATABASE row:\n", Dumper($row) );

                push(
                  @{ $sql{$dbname} },
                  sprintf( "-- update %s in %s\n",
                           join( ', ', keys(%diff_fields) ), $table ),
                  sprintf(
                    "UPDATE %s SET %s WHERE %s_id = %d;\n",
                    $dbh->quote_identifier( undef, $dbname, $table ),
                    join(
                      ', ',
                      map {
                        sprintf( "%s = %s",
                                 $_,
                                 $dbh->quote( $diff_fields{$_} ),
                                 $colinfo->{$_}{'DATA_TYPE'} )
                        }
                        keys(%diff_fields) ),
                    $table,
                    $pk ) );

                print("\n");

              } ## end if ( scalar( keys(%diff_fields...)))
            } ## end else [ if ( !defined($master_row...))]

          } ## end else [ if ( $pk == 0 ) ]

        } ## end foreach my $pk ( sort { $a ...})
      } ## end foreach my $table (@tables)

    } ## end while ( $sth->fetch() )
  } ## end foreach my $dbtype (@dbtypes)

} ## end foreach my $dbh (@db_handles)

if ( scalar( keys(%sql) ) > 0 ) {
  foreach my $db_name ( keys(%sql) ) {
    my $filename = sprintf( "fix-%s.sql", $db_name );
    printf( "==> Writing SQL to '%s'\n", $filename );
    my $out = IO::File->new( $filename, 'w' );
    $out->print( @{ $sql{$db_name} } );
    $out->close();
  }
} else {
  print("Nothing to do, all seems ok\n");
}

END {
  foreach my $dbh (@db_handles) {
    $dbh->disconnect();
  }
}