DBAdaptor.pm 7.1 KB
Newer Older
1
=pod
2 3 4

=head1 NAME

5
    Bio::EnsEMBL::Hive::DBSQL::DBAdaptor
6 7 8 9 10 11 12 13

=head1 SYNOPSIS

    $db = Bio::EnsEMBL::Hive::DBSQL::DBAdaptor->new(
        -user   => 'root',
        -dbname => 'pog',
        -host   => 'caldy',
        -driver => 'mysql',
14
    );
15 16

=head1 DESCRIPTION
17

18 19 20 21
    This object represents the handle for a Hive system enabled database

=head1 LICENSE

22
    Copyright [1999-2014] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute
23 24 25 26 27 28 29 30 31

    Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

         http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software distributed under the License
    is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and limitations under the License.
32 33 34

=head1 CONTACT

35
    Please contact ehive-users@ebi.ac.uk mailing list with questions/suggestions.
36 37 38 39 40 41 42

=cut


package Bio::EnsEMBL::Hive::DBSQL::DBAdaptor;

use strict;
43

44
use Bio::EnsEMBL::Registry;
45 46
use Bio::EnsEMBL::Utils::Argument ('rearrange');

47
use Bio::EnsEMBL::Hive::URLFactory;
48
use Bio::EnsEMBL::Hive::DBSQL::DBConnection;
49
use Bio::EnsEMBL::Hive::DBSQL::SqlSchemaAdaptor;
50

Leo Gordon's avatar
Leo Gordon committed
51
use base ('Bio::EnsEMBL::DBSQL::DBAdaptor');
52

53

54 55 56
sub new {
    my ($class, @args) = @_;

57 58
    my ($url, $reg_conf, $reg_type, $reg_alias, $no_sql_schema_version_check)
        = rearrange(['URL', 'REG_CONF', 'REG_TYPE', 'REG_ALIAS', 'NO_SQL_SCHEMA_VERSION_CHECK'], @args);
59

60 61
    $url .= ';nosqlvc=1' if($url && $no_sql_schema_version_check);

62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
    my $self;

    if($url) {
        $self = Bio::EnsEMBL::Hive::URLFactory->fetch($url)
            or die "Unable to connect to DBA using url='$url'\n";
    } elsif($reg_alias) {
        Bio::EnsEMBL::Registry->load_all($reg_conf) if($reg_conf);

        $reg_type ||= 'hive';

        $self = Bio::EnsEMBL::Registry->get_DBAdaptor($reg_alias, $reg_type)
            or die "Unable to connect to DBA using reg_conf='$reg_conf', reg_type='$reg_type', reg_alias='$reg_alias'\n";

        if($reg_type ne 'hive') {   # ensure we are getting a Hive adaptor even from a non-Hive Registry entry:
            $self = Bio::EnsEMBL::Hive::DBSQL::DBAdaptor->new( -dbconn => $self->dbc(), -no_sql_schema_version_check => $no_sql_schema_version_check );
        }
    } else {
        $self = $class->SUPER::new(@args)
            or die "Unable to connect to DBA using parameters (".join(', ', @args).")\n"
    }
82 83

    unless($no_sql_schema_version_check) {
84 85 86 87 88 89

        my $dbc = $self->dbc();
        $url ||= $dbc->url();

        my $code_sql_schema_version = Bio::EnsEMBL::Hive::DBSQL::SqlSchemaAdaptor->get_code_sql_schema_version()
            || die "DB($url) Could not establish code_sql_schema_version, please check that 'EHIVE_ROOT_DIR' environment variable is set correctly";
90

91 92
        my $db_sql_schema_version   = eval { $self->get_MetaAdaptor->fetch_value_by_key( 'hive_sql_schema_version' ); };
        if($@) {
93
            if($@ =~ /hive_meta.*doesn't exist/) {
94

95
                die "\nDB($url) The 'hive_meta' table does not seem to exist in the database yet.\nPlease patch the database up to sql_schema_version '$code_sql_schema_version' and try again.\n";
96 97 98

            } else {

99
                die "DB($url) $@";
100
            }
101 102 103

        } elsif(!$db_sql_schema_version) {

104
            die "\nDB($url) The 'hive_meta' table does not contain 'hive_sql_schema_version' entry.\nPlease investigate.\n";
105 106 107 108

        } elsif($db_sql_schema_version < $code_sql_schema_version) {


109 110 111 112
            my $new_patches = Bio::EnsEMBL::Hive::DBSQL::SqlSchemaAdaptor->get_sql_schema_patches( $db_sql_schema_version, $dbc->driver )
                || die "DB($url) sql_schema_version mismatch: the database's version is '$db_sql_schema_version' but the code is already '$code_sql_schema_version'.\n"
                      ."Unfortunately we cannot patch the database; you may have to create a new database or agree to run older code\n";

113
            my $patcher_command = "$ENV{'EHIVE_ROOT_DIR'}/scripts/db_cmd.pl -url $url";
114 115 116

            die "DB($url) sql_schema_version mismatch: the database's version is '$db_sql_schema_version' but the code is already '$code_sql_schema_version'.\n"
               ."Please upgrade the database by applying the following patches:\n\n".join("\n", map { "\t$patcher_command < $_" } @$new_patches)."\n\nand try again.\n";
117 118 119

        } elsif($code_sql_schema_version < $db_sql_schema_version) {

120
            die "DB($url) sql_schema_version mismatch: the database's version is '$db_sql_schema_version', but your code is still '$code_sql_schema_version'.\n"
121
               ."Please update the code and try again.\n";
122
        }
123
    }
124 125

    return $self;
126 127 128
}


129 130 131 132 133 134 135 136 137 138
sub dbc {
    my $self = shift @_;

    my $dbc = $self->SUPER::dbc( @_ );
    bless $dbc, 'Bio::EnsEMBL::Hive::DBSQL::DBConnection' if( $dbc );

    return $dbc;
}


139 140 141 142
sub hive_use_triggers {  # getter only, not setter
    my $self = shift @_;

    unless( defined($self->{'_hive_use_triggers'}) ) {
143
        my $hive_use_triggers = $self->get_MetaAdaptor->fetch_value_by_key( 'hive_use_triggers' );
144
        $self->{'_hive_use_triggers'} = $hive_use_triggers || 0;
145 146 147 148 149
    } 
    return $self->{'_hive_use_triggers'};
}


150 151 152 153 154 155 156 157 158 159 160
sub hive_use_param_stack {  # getter only, not setter
    my $self = shift @_;

    unless( defined($self->{'_hive_use_param_stack'}) ) {
        my $hive_use_param_stack = $self->get_MetaAdaptor->fetch_value_by_key( 'hive_use_param_stack' );
        $self->{'_hive_use_param_stack'} = $hive_use_param_stack || 0;
    } 
    return $self->{'_hive_use_param_stack'};
}


Leo Gordon's avatar
Leo Gordon committed
161
sub get_available_adaptors {
162
 
Leo Gordon's avatar
Leo Gordon committed
163
    my %pairs =  (
164
            # Core adaptors extended with Hive stuff:
165
        'MetaContainer'         => 'Bio::EnsEMBL::Hive::DBSQL::MetaContainer',
166 167

            # "new" Hive adaptors (sharing the same fetching/storing code inherited from the BaseAdaptor class) :
168 169 170 171 172 173 174 175 176 177 178
        'AnalysisCtrlRule'      => 'Bio::EnsEMBL::Hive::DBSQL::AnalysisCtrlRuleAdaptor',
        'DataflowRule'          => 'Bio::EnsEMBL::Hive::DBSQL::DataflowRuleAdaptor',
        'ResourceDescription'   => 'Bio::EnsEMBL::Hive::DBSQL::ResourceDescriptionAdaptor',
        'ResourceClass'         => 'Bio::EnsEMBL::Hive::DBSQL::ResourceClassAdaptor',
        'LogMessage'            => 'Bio::EnsEMBL::Hive::DBSQL::LogMessageAdaptor',
        'NakedTable'            => 'Bio::EnsEMBL::Hive::DBSQL::NakedTableAdaptor',
        'Analysis'              => 'Bio::EnsEMBL::Hive::DBSQL::AnalysisAdaptor',
        'Queen'                 => 'Bio::EnsEMBL::Hive::Queen',
        'AnalysisData'          => 'Bio::EnsEMBL::Hive::DBSQL::AnalysisDataAdaptor',
        'Accumulator'           => 'Bio::EnsEMBL::Hive::DBSQL::AccumulatorAdaptor',
        'Meta'                  => 'Bio::EnsEMBL::Hive::DBSQL::MetaAdaptor',
179 180

            # "old" Hive adaptors (having their own fetching/storing code) :
181 182
        'AnalysisJob'           => 'Bio::EnsEMBL::Hive::DBSQL::AnalysisJobAdaptor',
        'AnalysisStats'         => 'Bio::EnsEMBL::Hive::DBSQL::AnalysisStatsAdaptor',
Leo Gordon's avatar
Leo Gordon committed
183
    );
184
    return \%pairs;
185
}
186

187
1;