SqlSchemaAdaptor.pm 2.49 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
=pod

=head1 NAME

    Bio::EnsEMBL::Hive::DBSQL::SqlSchemaAdaptor

=head1 SYNOPSIS

    Bio::EnsEMBL::Hive::DBSQL::SqlSchemaAdaptor->get_code_sql_schema_version();

=head1 DESCRIPTION

    This is currently an "objectless" adaptor for finding out the apparent code's SQL schema version

15 16
=head1 LICENSE

17
    Copyright [1999-2015] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute
Matthieu Muffato's avatar
Matthieu Muffato committed
18
    Copyright [2016-2018] EMBL-European Bioinformatics Institute
19 20 21 22 23 24 25 26 27 28

    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.

29 30
=head1 CONTACT

31
    Please subscribe to the Hive mailing list:  http://listserver.ebi.ac.uk/mailman/listinfo/ehive-users  to discuss Hive-related questions or to be notified of our updates
32 33 34 35 36 37 38 39

=cut


package Bio::EnsEMBL::Hive::DBSQL::SqlSchemaAdaptor;

use strict;

40
sub find_all_sql_schema_patches {
41

42
    my %all_patches = ();
43

44
    if(my $hive_root_dir = $ENV{'EHIVE_ROOT_DIR'} ) {
45
        foreach my $patch_path ( split(/\n/, `ls -1 $hive_root_dir/sql/patch_20*.*sql*`) ) {
46
            my ($patch_name, $driver) = ($patch_path=~/^(.+)\.(\w+)$/);
47 48

            $driver = 'mysql' if ($driver eq 'sql');    # for backwards compatibility
49

50 51 52 53 54 55
            $all_patches{$patch_name}{$driver} = $patch_path;
        }
    } # otherwise will sliently return an empty hash

    return \%all_patches;
}
56

57 58 59 60 61 62 63 64 65 66 67 68 69 70

sub get_sql_schema_patches {
    my ($self, $after_version, $driver) = @_;

    my $all_patches         = $self->find_all_sql_schema_patches();
    my $code_schema_version = $self->get_code_sql_schema_version();

    my @ordered_patches = ();
    foreach my $patch_key ( (sort keys %$all_patches)[$after_version..$code_schema_version-1] ) {
        if(my $patch_path = $all_patches->{$patch_key}{$driver}) {
            push @ordered_patches, $patch_path;
        } else {
            return;
        }
71
    }
72 73

    return \@ordered_patches;
74 75 76 77
}


sub get_code_sql_schema_version {
78
    my ($self) = @_;
79

80
    return scalar( keys %{ $self->find_all_sql_schema_patches() } );   # 0 probably means $ENV{'EHIVE_ROOT_DIR'} not set correctly
81 82 83 84
}

1;