Commit 775c5e0b authored by Andreas Kusalananda Kähäri's avatar Andreas Kusalananda Kähäri
Browse files

Reformat.

parent 0eefc9fe
......@@ -12,26 +12,29 @@ use DBI;
use IO::File;
use FindBin;
my ( $host, $user, $pass, $port, @dbnames, $file, $release_num,
$nobackup,$forceTableWrite );
GetOptions( "dbhost|host=s", \$host,
"dbuser|user=s", \$user,
"dbpass|pass=s", \$pass,
"dbport|port=i", \$port,
"file=s", \$file,
"nobackup", \$nobackup,
"dbname|dbnames=s@", \@dbnames,
"release_num=i", \$release_num,
"force_table_write", \$forceTableWrite);
my (
$host, $user, $pass,
$port, @dbnames, $file,
$release_num, $nobackup, $forceTableWrite
);
GetOptions(
"dbhost|host=s", \$host,
"dbuser|user=s", \$user,
"dbpass|pass=s", \$pass,
"dbport|port=i", \$port,
"file=s", \$file,
"nobackup", \$nobackup,
"dbname|dbnames=s@", \@dbnames,
"release_num=i", \$release_num,
"force_table_write", \$forceTableWrite
);
$file ||= $FindBin::Bin . "/attrib_type.txt";
usage() if ( !$host );
usage()
if ( ( $release_num && @dbnames ) || ( !$release_num && !@dbnames ) );
#release num XOR dbname are required
$port ||= 3306;
......@@ -45,10 +48,11 @@ if ($release_num) {
#
# filter out all non-core databases
#
@dbnames = grep {/^[a-zA-Z]+\_[a-zA-Z]+\_(core|otherfeatures|cdna|vega)\_${release_num}\_\d+[A-Za-z]?$/} @dbnames;
@dbnames = grep {
/^[a-zA-Z]+\_[a-zA-Z]+\_(core|otherfeatures|cdna|vega)\_${release_num}\_\d+[A-Za-z]?$/
} @dbnames;
}
#
# make sure the user wishes to continue
#
......@@ -57,7 +61,6 @@ print STDERR "The following databases "
print join( "\n ", @dbnames );
print "\ncontinue with update (yes/no)> ";
my $input = lc(<STDIN>);
chomp($input);
if ( $input ne 'yes' ) {
......@@ -65,20 +68,24 @@ if ( $input ne 'yes' ) {
exit();
}
my $attribs = read_attrib_file( $file );
my $attribs = read_attrib_file($file);
my $table_consistent;
# if any attrib_types are loaded that are different from the file, a
# consistency problem is reported and the upload is not done.
for my $database (@dbnames) {
backup_attribute_types( $host, $user, $pass, $port, $database )
if ( !$nobackup );
if ( !$nobackup ) {
backup_attribute_types( $host, $user, $pass, $port, $database );
}
$table_consistent = check_consistency( $attribs, $database, $db );
# This has been introduced in e54: when we are sure that the data in
# the file is consistent (e.g. some attributes have been removed from
# the file since they are not longer are needed) you should force the
# writing option.
$table_consistent = check_consistency($attribs,$database,$db);
#this has been introduced in e54: when we are sure that the data in the file is consistent
#(e.g. some attributes have been removed from the file since they are not longer are needed)
#you should force the writing option
if ( $table_consistent || $forceTableWrite ) {
# consistent
$db->do("use $database");
......@@ -88,23 +95,24 @@ for my $database (@dbnames) {
} else {
print STDERR "Repairing $database, not consistent!\n";
repair( $attribs, $database, $db );
print STDERR "If you are sure the file is up to date, you can use the --force_table_write".
"to overwrite the information in the attrib_table, check RelCoord for more information\n";
print STDERR "If you are sure the file is up to date, "
. "you can use the --force_table_write"
. "to overwrite the information in the attrib_table, "
. "check RelCoord for more information\n";
}
}
} ## end for my $database (@dbnames)
# move attrib types wih the same code to the common attrib_type table
# Move attrib types wih the same code to the common attrib_type table
# ones that are not in the table move to an attrib_type_id that is not
# used
# used.
sub repair {
my ( $attribs, $database, $db ) = @_;
$db->do("use $database");
my @tables =
qw( seq_region_attrib misc_attrib translation_attrib transcript_attrib gene_attrib);
my @tables = qw( seq_region_attrib misc_attrib translation_attrib
transcript_attrib gene_attrib);
my $ref = $db->selectall_arrayref("show create table attrib_type");
my $create_table = $ref->[0]->[1];
......@@ -113,29 +121,29 @@ sub repair {
load_attribs( $db, $attribs );
$db->do( "delete oat "
. "from old_attrib_type oat, attrib_type at "
. "where oat.attrib_type_id = at.attrib_type_id "
. "and oat.code = at.code" );
$db->do( "delete oat "
. "from old_attrib_type oat, attrib_type at "
. "where oat.attrib_type_id = at.attrib_type_id "
. "and oat.code = at.code" );
# what remains in old attrib type ?
# Entries with a code that is unknown in general file
# and that shouldn't really happen. If it happens, the code
# needs to be appended to attrib_type table and the attrib
# type_ids will be updated in the feature tables.
# Entries with a code that is unknown in general file and that
# shouldn't really happen. If it happens, the code needs to be
# appended to attrib_type table and the attrib type_ids will be
# updated in the feature tables.
# Entries with a code that is known, but has different
# attrib_type_id. Feature tables will be updated.
$db->do( "create table tmp_attrib_types "
. "select oat.attrib_type_id, oat.code, oat.name, oat.description "
. "from old_attrib_type oat "
. "left join attrib_type at "
. "on oat.code = at.code "
. "where at.code is null" );
$db->do( "insert into attrib_type( code, name, description) "
. "select code, name, description "
. "from tmp_attrib_types" );
. "select oat.attrib_type_id, oat.code, oat.name, oat.description "
. "from old_attrib_type oat "
. "left join attrib_type at "
. "on oat.code = at.code "
. "where at.code is null" );
$db->do( "insert into attrib_type( code, name, description) "
. "select code, name, description "
. "from tmp_attrib_types" );
$ref = $db->selectall_arrayref("select code from tmp_attrib_types");
$db->do("drop table tmp_attrib_types");
......@@ -163,30 +171,27 @@ sub repair {
# now do multi table updates on all tables
for my $up_table (@tables) {
$db->do( "update $up_table tb, attrib_type at, old_attrib_type oat "
. "set tb.attrib_type_id = at.attrib_type_id "
. "where tb.attrib_type_id = oat.attrib_type_id "
. "and oat.code = at.code " );
. "set tb.attrib_type_id = at.attrib_type_id "
. "where tb.attrib_type_id = oat.attrib_type_id "
. "and oat.code = at.code " );
}
$db->do("drop table old_attrib_type");
} ## end sub repair
sub load_attribs {
my ( $db, $attribs ) = @_;
my $sth;
$sth =
$db->prepare(
$sth = $db->prepare(
"insert into attrib_type( attrib_type_id, code, name, description) "
. "values(?,?,?,?)" );
for my $attrib (@$attribs) {
$sth->execute( $attrib->{'attrib_type_id'},
$attrib->{'code'}, $attrib->{'name'},
$attrib->{'description'} );
$sth->execute(
$attrib->{'attrib_type_id'}, $attrib->{'code'},
$attrib->{'name'}, $attrib->{'description'} );
}
}
# alternatively consistency can be enforced to a certain degree
sub check_consistency {
my $attribs = shift;
......@@ -199,8 +204,8 @@ sub check_consistency {
$db->do("use $database");
my $sth =
$db->prepare( "SELECT attrib_type_id, code, name, description "
. "FROM attrib_type" );
$db->prepare( "SELECT attrib_type_id, code, name, description "
. "FROM attrib_type" );
$sth->execute();
while ( my $arr = $sth->fetchrow_arrayref() ) {
$db_codes{ $arr->[0] } = $arr->[1];
......@@ -211,12 +216,12 @@ sub check_consistency {
for my $dbid ( keys %db_codes ) {
if ( !exists $file_codes{$dbid} ) {
printf( "Not consistent: code '%d' ('%s') not in file.\n",
$dbid, $db_codes{$dbid} );
$dbid, $db_codes{$dbid} );
$consistent = 0;
} elsif ( $file_codes{$dbid} ne $db_codes{$dbid} ) {
printf( "Not consistent: code '%d' is '%s' in file "
. "but '%s' in database.\n",
$dbid, $file_codes{$dbid}, $db_codes{$dbid} );
. "but '%s' in database.\n",
$dbid, $file_codes{$dbid}, $db_codes{$dbid} );
$consistent = 0;
}
}
......@@ -241,26 +246,28 @@ sub read_attrib_file {
my @a = split( /\t/, $row );
push @rows, {
'attrib_type_id' => $a[0],
'code' => $a[1],
'name' => $a[2],
'description' => $a[3] };
push @rows,
{
'attrib_type_id' => $a[0],
'code' => $a[1],
'name' => $a[2],
'description' => $a[3] };
}
$fh->close();
return \@rows;
} ## end sub read_attrib_file
sub backup_attribute_types {
my ( $host, $user, $pass, $port, $dbname ) = @_;
unless (
system( "mysql -h$host -P$port -u$user -p'$pass' -N "
. "-e 'select * from attrib_type' $dbname "
. "> $dbname.attrib_type.backup; "
. "gzip -9 -f $dbname.attrib_type.backup"
) == 0 )
system(
"mysql -h$host -P$port -u$user -p'$pass' -N "
. "-e 'select * from attrib_type' $dbname "
. "> $dbname.attrib_type.backup; "
. "gzip -9 -f $dbname.attrib_type.backup"
) == 0
)
{
print STDERR "Can't dump the original attrib_type table "
. "from $dbname for backup\n";
......@@ -271,17 +278,18 @@ sub backup_attribute_types {
}
}
sub usage {
GetOptions( "host=s", \$host,
"user=s", \$user,
"pass=s", \$pass,
"port=i", \$port,
"file=s", \$file,
"dbnames=s@", \@dbnames,
"release_num=i", \$release_num,
"nobackup", \$nobackup,
"force_table_write", \$forceTableWrite);
GetOptions(
"host=s", \$host,
"user=s", \$user,
"pass=s", \$pass,
"port=i", \$port,
"file=s", \$file,
"dbnames=s@", \@dbnames,
"release_num=i", \$release_num,
"nobackup", \$nobackup,
"force_table_write", \$forceTableWrite
);
print STDERR <<EOC;
Usage:
......@@ -297,14 +305,17 @@ Usage:
Use --file ... if not using the default 'attrib_type.txt'.
The new option force_table_write has been introduced: when you run the script and there
is inconsistency between the database and the file (there is the "not consistent!!" in
the output), you should first ensure if the consistency is expected (some attributes were
removed from the file because they are not longer needed), or not expected (someone introduced
a new attribute_type in the database but forgot to update the attrib_type.txt file). In the first
situation, you should use the --force_table_write when running the script. In the second situation,
the new attrib_type must be added to the file and run it again without the force flag
The new option force_table_write has been introduced: when you run
the script and there is inconsistency between the database and the
file (there is the "not consistent!!" in the output), you should first
ensure if the consistency is expected (some attributes were removed
from the file because they are not longer needed), or not expected
(someone introduced a new attribute_type in the database but forgot to
update the attrib_type.txt file). In the first situation, you should
use the --force_table_write when running the script. In the second
situation, the new attrib_type must be added to the file and run it
again without the force flag
EOC
exit;
}
} ## end sub usage
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment