[英]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 schemas are good.然后我使用stepancheg / mysql-diff来确保数据库模式是好的。
Are there any things I should be doing when migrating databases?迁移数据库时我应该做些什么? (I test on a staging server before hand also) (我也事先在登台服务器上进行了测试)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.