开发者

Multi database schema migration (php/mysql)

I have an application where each user has their own database and share the same schema. (I have another thread discussing this, so I don't need any information on this)

When migrating the databases, I wrote a script shown below:

<?php
$sql = <<<SQL
ALTER TABLE xyz....;
ALTER TABLE abc.....;
SQL;
$sql_queries = explode(";", $sql);
$exclude_dbs = array();

$conn = mysql_connect("localhost", "USER", "PASSWORD");
$show_db_query = mysql_query('SHOW databases');
$databases = array();
while ($row = mysql_fetch_assoc($show_db_query))
{
        if (!in_array($row['Database'], $exclude_dbs))
        {
                $databases[] = $row['Database'];
        }
}

foreach($databases as $database)
{
        mysql_select_db($database, $conn);
        echo "Running queries on $database\n***********************************\n";
        foreach($sql_queries as $query)
        {
                if (!empty($query))
                {
                        echo "$query;";
                        if (!mysql_query($query))
                        {
                                echo "\n\nERROR: ".mysql_error()."\n\n";
                        }
                }
        }
        echo "\n\n";
}
?>

It has been working fine without any problems. I then use stepancheg / mysql-diff to make sure the database schem开发者_JAVA技巧as are good.

Are there any things I should be doing when migrating databases? (I test on a staging server before hand also)


Your approach seems to be working, and the use of mysql-diff is good. Have you considered using mysqldump to back up actual data? It's a more conventional approach than a custom script.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜