[Bio] / FigKernelScripts / init_FIG.pl Repository:
ViewVC logotype

Annotation of /FigKernelScripts/init_FIG.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.13 - (view) (download) (as text)

1 : olson 1.8 #
2 :     # Copyright (c) 2003-2006 University of Chicago and Fellowship
3 :     # for Interpretations of Genomes. All Rights Reserved.
4 :     #
5 :     # This file is part of the SEED Toolkit.
6 :     #
7 :     # The SEED Toolkit is free software. You can redistribute
8 :     # it and/or modify it under the terms of the SEED Toolkit
9 :     # Public License.
10 :     #
11 :     # You should have received a copy of the SEED Toolkit Public License
12 :     # along with this program; if not write to the University of Chicago
13 :     # at info@ci.uchicago.edu or the Fellowship for Interpretation of
14 :     # Genomes at veronika@thefig.info or download a copy from
15 :     # http://www.theseed.org/LICENSE.TXT.
16 :     #
17 :    
18 : efrank 1.1 use FIG;
19 : olson 1.4 use strict;
20 : efrank 1.1
21 : efrank 1.2
22 :     #
23 :     # Initialize FIG database.
24 :     #
25 :     # This is database-system specific.
26 :     #
27 : olson 1.5 # We assume the database system has been configured and the database
28 :     # server is up and running. Responsibility for this lies with the
29 :     # SEED configure process.
30 :     #
31 : efrank 1.2 # We use system below because it's okay for the database drop to fail; if we used
32 :     # $FIG::run it would cause the script to terminate.
33 :     #
34 :    
35 : olson 1.4 my $table_init = "CREATE TABLE file_table ( file varchar(200) UNIQUE NOT NULL, fileno INTEGER, PRIMARY KEY (file));";
36 : efrank 1.2
37 :     if ($FIG_Config::dbms eq "Pg")
38 : efrank 1.1 {
39 : efrank 1.2
40 : olson 1.3 my $dbport = $FIG_Config::dbport;
41 :     my $dbuser = $FIG_Config::dbuser;
42 : olson 1.4
43 :     #
44 :     # Try to do some due diligence on ensuring we don't delete an active
45 :     # database. Do this by creating a $fig and querying against the
46 :     # genome table (in an eval in case it fails, like it really should).
47 :     #
48 :    
49 :     my $genomes;
50 :     eval {
51 : olson 1.7 #
52 :     # Consume the warnings that db init may emit.
53 :     #
54 :     my ($fig, $dbh);
55 :     do {
56 :     local $SIG{__WARN__} = sub { die @_; };
57 :    
58 :     $fig = new FIG;
59 :     $dbh = $fig->db_handle();
60 :     } while(0);
61 : olson 1.4
62 : olson 1.7 local $dbh->{_dbh}->{PrintError} = 0;
63 :     local $dbh->{_dbh}->{RaiseError} = 1;
64 : parrello 1.6
65 : olson 1.4 my $resp = $dbh->SQL("select count(*) from genome");
66 :     if ($resp && @$resp > 0)
67 :     {
68 :     $genomes = $resp->[0]->[0];
69 :     }
70 : parrello 1.6
71 : olson 1.4 };
72 :    
73 : olson 1.7 my $need_dropdb = 1;
74 :    
75 : olson 1.4 if ($@ eq "")
76 :     {
77 :     #
78 :     # We didn't get an error - that means we were able to connect. Be wary.
79 :     #
80 :    
81 :     if ($genomes > 0)
82 :     {
83 :     #
84 :     # Yah, there's stuff in here.
85 :     #
86 :    
87 :     print "You are initializing a SEED database named $FIG_Config::Db that appears to contain live data\n";
88 :     print "(it has $genomes genomes loaded). If you continue, this data will be\n";
89 :     print "wiped and a reload required.\n";
90 :     print "\nDo you wish to continue? (y/n) ";
91 : parrello 1.6
92 : olson 1.4 my $ans = <STDIN>;
93 :     if ($ans !~ /^y/i)
94 :     {
95 :     exit;
96 :     }
97 :     }
98 :     else
99 :     {
100 :     #
101 :     # Yah, there might be.
102 :     #
103 :    
104 :     print "You are initializing a SEED database named $FIG_Config::db that might contain live data\n";
105 :     print "(the database exists, but does not appear to have data)\n";
106 :     print "\nDo you wish to continue? (y/n) ";
107 : parrello 1.6
108 : olson 1.4 my $ans = <STDIN>;
109 :     if ($ans !~ /^y/i)
110 :     {
111 :     exit;
112 :     }
113 :     }
114 :     }
115 : olson 1.7 else
116 :     {
117 :     if ($@ =~ /FATAL:\s+database\s+"$FIG_Config::db"\s+does\s+not\s+exist/)
118 :     {
119 :     $need_dropdb = 0;
120 :     }
121 :     }
122 : olson 1.4
123 :     print "\nInitializing new SEED database $FIG_Config::db\n\n";
124 :    
125 : olson 1.7 if ($need_dropdb)
126 :     {
127 :     system("dropdb -p $dbport -U $dbuser $FIG_Config::db");
128 :     }
129 : olson 1.3 &FIG::run("createdb -p $dbport -U $dbuser $FIG_Config::db");
130 :     open(PSQL,"| psql -p $dbport -U $dbuser $FIG_Config::db") || die "could not initialize DB";
131 : efrank 1.2
132 : olson 1.3 print PSQL $table_init;
133 :    
134 :     close(PSQL);
135 : olson 1.4
136 :     print "\nComplete. You will need to run \"fig load_all\" to load the data.\n";
137 : parrello 1.6 } elsif ($FIG_Config::dbms eq "mysql" && !$FIG_Config::win_mode) {
138 : olson 1.11
139 :     my @args;
140 :     if ($FIG_Config::dbsock ne "")
141 :     {
142 : olson 1.12 push(@args, -S => $FIG_Config::dbsock);
143 : olson 1.11 }
144 :     push(@args, -u => $FIG_Config::dbuser);
145 :     if ($FIG_Config::dbpass)
146 :     {
147 :     push(@args, "-p$FIG_Config::dbpass");
148 :     }
149 : olson 1.13 if ($FIG_Config::dbhost)
150 :     {
151 :     push(@args, "-h $FIG_Config::dbhost");
152 :     }
153 : olson 1.11
154 :     system("mysqladmin @args drop $FIG_Config::db");
155 : olson 1.13 system("mysqladmin @args create $FIG_Config::db");
156 : olson 1.11 open(MYSQL, "| mysql -D $FIG_Config::db @args");
157 : parrello 1.6 print MYSQL "$table_init\n";
158 :     close(MYSQL);
159 :     } elsif ($FIG_Config::dbms eq "mysql" && $FIG_Config::win_mode) {
160 :     # Here we're in Windows, and we can't create the database on the
161 :     # command line because of password glitchiness.
162 :     # First, we connect to the database.
163 :     my $fig = new FIG;
164 :     my $dbh = $fig->db_handle();
165 :     # Drop all the tables.
166 :     my @tables = $dbh->get_tables();
167 :     for my $table (@tables) {
168 :     print "Dropping $table.\n";
169 :     $dbh->drop_table(tbl => $table);
170 :     }
171 :     # Create the file table.
172 :     print "Creating file table.\n";
173 :     $dbh->SQL($table_init);
174 :     # Tell the user we're done.
175 :     print "Run load_all to load the data.\n";
176 : olson 1.9 }
177 :     elsif ($FIG_Config::dbms eq 'SQLite')
178 :     {
179 :     # don't need to really do anything but create the table..
180 :     my $fig = new FIG;
181 :     my $dbh = $fig->db_handle();
182 :     print "Creating file table.\n";
183 :     $dbh->SQL($table_init);
184 : parrello 1.6 } else {
185 :     print "Invalid database configuration. Check the \"dbms\" variable in FIG_Config.\n";
186 : efrank 1.1 }

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3