I haven't looked into mysqldiff very close but something tells me it's not the right tool
As i said earlier i'd be glad to have a highly specialized OO library for this task, please have a look at the following imaginary script using such kind of a library.
PHP Code:
<?php
$local_db = new DSN('mysq://root:secret@localhost/dev');
$shared_db = new DSN('mysq://root:secret@shared.db/dev');
$c = new MySQLDbComparator($local_db, $shared_db);
if($c->isIdentical())
{
echo 'Databases are identical';
exit(0);
}
if($c->isSchemaConflict())
{
//here we check if shared db schema needs to be updated:
if($diff = $c->getLeftSchemaDiff())
{
//it's almost 100% safe to apply changes if we simply
//modified indexes, added new tables or fields,
//the most dangerous changes concern table and fields drops,
//Below we check if changes are critical and require manual
//control otherwise we backup the shared db and apply diff to it
//automatically
if($diff->isDangerous())
{
echo 'Dangerous changes in new schema:';
echo $diff->toString();
exit(1);
}
$dumper = new MySQLDumper($shared_db);
$dumper->dumpAll('/path/to/shared/db/backup/sql');
echo 'Applying schema changes';
echo $diff->toString();
$diff->applyTo($shared_db);
}
}
if($c->isDataConflict())
{
//and here we check if local db data needs to be updated:
if($diff = $c->getRightDataDiff())
{
//we believe that data changes on the shared server can
//be safely applied to the local server...
$dumper = new MySQLDumper($local_db);
$dumper->dumpAll('/path/to/local/db/backup/sql');
echo 'Applying data changes';
echo $diff->toString();
$diff->applyTo($local_db);
}
}
exit(0);
?>
The following list of classes would be very nice to see in such a library:
1) A whole bunch of db meta data classes: DbInfo, TableInfo, FieldInfo, IndexInfo. I think we could use and extend them from Propel(if Hans doesn't mind
).
2) DbDumper which is capable of dumping concrete db into sql using different settings(dump only data, dump only schema, dump only certain tables, etc). It may also be capable of loading dumps into the database but that can be another class as well.
3) DbComparator which would accept 2 db meta data objects in the constructor and analyze and return differences between them(DbSchemaDiff and DbDataDiff).
4) DbSchemaDiff, DbDataDiff - these two would encapsulate actual changes between 2 databases and would be able to apply the diff to the "right" or "left" part.
5) DbSandBox - a wrapper around some db object which creates a temporary physical copy of the database transparently to the user. Upon its destruction the temporary db gets dropped silently. It can be very useful if one has to change some db data before, say, dumping it without messing up with the original db.
Please feel free to post any kind of feedback on this pre-alpha list of classes!
Bookmarks