3 * Copyright (C) 2003-2004 Polytechnique.org
4 * http://opensource.polytechnique.org/
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 require_once dirname(__FILE__
).'/diogenes.database.inc.php';
25 /** This class is used to create, update and remove databases
27 class DiogenesDatabaseCreator
{
28 /** Do we show information messages? */
31 /** Do we show debugging info? */
32 var $opt_debug = false
;
34 /** table containing options */
37 /** database versions history */
38 var $versions = array();
45 function DiogenesDatabaseCreator($opt_table)
47 $this->opt_table
= $opt_table;
52 * Connect to the database
57 $this->debug("host : ".$this->dbhost
);
58 $this->debug("user : ".$this->dbuser
);
59 $this->debug("pass : ".(($this->dbpass
!= "") ?
"true" : "false"));
60 $this->debug("database : ".$this->dbdb
);
62 $this->dbh
= new DiogenesDatabase($this->dbdb
, $this->dbhost
, $this->dbuser
, $this->dbpass
);
64 if (!$this->dbh
->connect_id
) {
65 $this->error("Unable to connect to the database!");
68 return $this->dbh
->connect_id
;
73 * Displays a debugging message.
85 * Displays an info message.
97 * Displays an error message.
108 * Upgrade the database from one version to the next
112 function upgradeDb($newversion)
114 $this->info("updrade to $newversion");
119 * Retrieve the current database version
121 function getVersion()
123 $res = $this->dbh
->query("SELECT value FROM {$this->opt_table} WHERE name='dbversion'");
124 if (list($dbversion) = mysql_fetch_row($res)) {
125 mysql_free_result($res);
127 $dbversion = $this->versions
[0];
134 * Set the current database version
138 function setVersion($newversion)
140 $this->dbh
->query("REPLACE INTO {$this->opt_table} SET name='dbversion',value='$newversion'");
145 * Parse command line options
153 function parseOptions($argv, $dbdb, $dbhost, $dbuser, $dbpass)
155 // set default options
157 $this->dbhost
= $dbhost;
158 $this->dbuser
= $dbuser;
159 $this->dbpass
= $dbpass;
162 $script = basename($argv[0]);
163 $opts = Console_GetOpt
::getopt($argv, "d:hp:qs:u:v");
165 if ( PEAR
::isError($opts) ) {
166 echo $opts->getMessage();
167 $this->syntax($script);
171 foreach ( $opts as $opt) {
174 $this->syntax($script);
177 $this->opt_info
= false
;
178 $this->opt_debug
= false
;
181 $this->dbdb
= $opt[1];
184 $this->dbuser
= $opt[1];
187 $this->opt_info
= true
;
188 $this->opt_debug
= true
;
191 $this->dbhost
= $opt[1];
194 $this->dbpass
= $opt[1];
204 * Displays program usage.
206 function syntax($script)
210 " $script [options]\n\n".
212 " -h display this help message\n".
214 " -v verbose mode\n\n".
227 if (!$this->connect()) {
231 $versions = $this->versions
;
232 $dbversion = $this->getVersion();
233 $this->info("Current database version is $dbversion");
235 // check we know the current database version
236 if (!in_array($dbversion, $versions)) {
237 $this->error("Unknown database format version '$dbversion'");
241 // runs the successive updates
242 $from = array_search($dbversion, $versions);
243 $to = sizeof($versions)-1;
245 for($pos = $from; $pos < $to; $pos++
) {
246 $oldversion = $versions[$pos];
247 $newversion = $versions[$pos+
1];
248 $this->info("Upgrading from DB format '$oldversion' to '$newversion'");
250 $this->upgradeDb($newversion);
251 $this->setVersion($newversion);