File: gmod_update_chado.pl

package info (click to toggle)
libchado-perl 1.31-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 44,716 kB
  • sloc: sql: 282,721; xml: 192,553; perl: 25,524; sh: 102; python: 73; makefile: 57
file content (102 lines) | stat: -rwxr-xr-x 2,451 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
#!/usr/bin/env perl
use strict;
use warnings;

use Getopt::Long;
#use lib '/home/cain/cvs_stuff/schema/chado/lib';
#use lib '/home/scott/cvs_stuff/schema/chado/lib';
use Bio::GMOD::Config;
use Bio::GMOD::DB::Config;
use ExtUtils::MakeMaker;  #to get prompt
use Cwd;

=head1 NAME

$0 - updates the schema of a Chado database if necessary

=head1 SYNOPSIS

  % gmod_update_chado.pl [options] 

=head1 COMMAND-LINE OPTIONS

  --force          Update the schema without prompt
  --dbprofile      Which database profile to use for updating

=head1 DESCRIPTION

=head1 AUTHOR

Scott Cain E<lt>scain@cpan.orgE<gt>

Copyright (c) 2011

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.

=cut

my ($FORCE, $DBPROFILE, );

GetOptions(
    'force'         => \$FORCE,
    'dbprofile=s'   => \$DBPROFILE,
) or ( system( 'pod2text', $0 ), exit -1 );

$DBPROFILE ||= 'default';

my $gmod_conf = Bio::GMOD::Config->new();
my $version   = $gmod_conf->version();
my $gmod_root = $gmod_conf->gmod_root();
my $db_conf   = Bio::GMOD::DB::Config->new($gmod_conf, $DBPROFILE);
my $dbh       = $db_conf->dbh();

my $current_version = `gmod_chado_properties.pl --version --dbprof $DBPROFILE`;
chomp $current_version;

if ($current_version >= $version) {
    print "This instance of the Chado schema does not need updating.\n";
    exit(0);
}

if (!defined $FORCE) {
    print <<END

The schema needs updating; this will add columns and/or tables, and should
not delete any data, but we still advise you to back up your database
before continuing.  

END
;
    my $YN = prompt ("Continue with update?", "y");
    if ($YN =~ /^n/i) {
        print  "OK, exiting...\n";
        exit(0);
    }
}

#build path to get updates from
my $path = "$gmod_root/src/chado/schemas/$current_version-$version/diff.sql";
my $cwd  = getcwd;

my $dbuser = $db_conf->user;
my $dbport = $db_conf->port;
my $dbhost = $db_conf->host;
my $dbname = $db_conf->name;
my $schema = $db_conf->schema;

system("cp $path $cwd");
unless ($schema eq 'public') {
    system("perl -pi -e 's/public/$schema/g' diff.sql");
}

my $syscommand = "cat diff.sql | psql -U $dbuser -p $dbport -h $dbhost $dbname";
system($syscommand) == 0 or die "failed updating database";

#now update the schema version in the chadoprop table
system("gmod_chado_properties.pl --version $version --force --dbprof $DBPROFILE");

print "Updating $dbname complete.\n";

exit(0);