Check existing tables before running database migrations

This commit is contained in:
printempw 2018-02-22 22:29:25 +08:00
parent 6a977b6de4
commit 3725ec26f7
3 changed files with 23 additions and 12 deletions

View File

@ -32,6 +32,19 @@ class SetupController extends Controller
public function info()
{
$existingTables = static::checkTablesExist([], true);
// Not installed completely
if (count($existingTables) > 0) {
Log::info('Remaining tables detected, exit setup wizard now', [$existingTables]);
$existingTables = array_map(function ($item) {
return get_db_config()['prefix'].$item;
}, $existingTables);
throw new PrettyPageException(trans('setup.database.table-already-exists', ['tables' => json_encode($existingTables)]), 1);
}
return view('setup.wizard.info');
}
@ -60,7 +73,7 @@ class SetupController extends Controller
}
}
// create tables
// Create tables
Artisan::call('migrate', ['--force' => true]);
Log::info("[SetupWizard] Tables migrated.");
@ -169,28 +182,24 @@ class SetupController extends Controller
* Check if the given tables exist in current database.
*
* @param array $tables
* @return bool
* @param bool $returnExisting
* @return bool|array
*/
public static function checkTablesExist($tables = null) {
$totalTables = 0;
public static function checkTablesExist($tables = [], $returnExistingTables = false) {
$existingTables = [];
$tables = $tables ?: ['users', 'closets', 'players', 'textures', 'options'];
foreach ($tables as $tableName) {
// Table prefix will be added automatically
if (Schema::hasTable($tableName)) {
$totalTables++;
$existingTables[] = $tableName;
}
}
if ($totalTables == count($tables)) {
if (count($existingTables) == count($tables)) {
return true;
} else {
// Not installed completely
foreach (array_merge($tables, ['migrations']) as $tableName) {
Schema::dropIfExists($tableName);
}
return false;
return $returnExistingTables ? $existingTables : false;
}
}

View File

@ -1,6 +1,7 @@
database:
connection-error: "Unable to connect to the target :type database, please check your configuration. The server replied with: :msg"
connection-success: Connect to the target :type database [:server] successfully, just click NEXT to start installation.
table-already-exists: There are some tables already exist in target database, which names conflict with ones we are going to create. To avoid data loss, please manually delete these tables :tables, or set a different table prefix.
file:
permission-error: Unable to create textures folder, please check the directory permissions or place one manually.

View File

@ -1,6 +1,7 @@
database:
connection-error: 无法连接至 :type 服务器,请检查你的配置。服务器返回的信息:「:msg」
connection-success: 成功连接至 :type 服务器 [:server] ,点击下一步以开始安装。
table-already-exists: 检测到目标数据库中已存在如下数据表 :tables它们与本程序即将创建的数据表名称冲突为了避免原有数据被覆盖请手动删除它们或者为本程序选择一个不同的数据表前缀。
file:
permission-error: textures 文件夹创建失败,请确认目录权限是否正确,或者手动放置一个。