Having MDB2_Schema to update your database,
when its schema changes is also really easy.
You can use the
getDefinitionFromDatabase()
method to determine the current database schema,
and then just use
updateDatabase()
to do the actual update.
However, you have to make sure, that
getDefinitionFromDatabase()
returns what you expect before you rely on it.
See the respective chapter for more info.
<?php
require_once 'MDB2/Schema.php';
$options = array(
'log_line_break' => '<br>',
'idxname_format' => '%s',
'debug' => true,
'quote_identifier' => true,
'force_defaults' => false,
'portability' => false
);
$dsn = 'mysql://root:@localhost/MDB2Example';
$schema =& MDB2_Schema::factory($dsn, $options);
if (PEAR::isError($schema)) {
$error = $schema->getMessage();
} else {
// first run with queries disabled to make sure everything is allright
$disable_query = true;
$previous_schema = $schema->getDefinitionFromDatabase();
$op = $schema->updateDatabase('schema.xml', $previous_schema, array(), $disable_query);
if (PEAR::isError($op)) {
$error = $op->getMessage();
}
}
if (isset($error)) {
var_dump($error);
}
$schema->disconnect();
?>
The method accepts both,
a filename or a definition array,
as the first two parameters.
Note how we mixed them in the example above.
You may want to backup the current schema using
dumpDatabase()
for the case something goes wrong.
When updating database schemas we can run into data persistence issues. This can be addressed with data manipulation ability, that will be documented later in this manual.