Skip to content

Commit 631885d

Browse files
committed
Handle a plugin not properly namespacing itself and causing migration errors; ref pterodactyl#2291
1 parent b707147 commit 631885d

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

database/migrations/2020_04_03_230614_create_backups_table.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
use Illuminate\Support\Facades\DB;
34
use Illuminate\Support\Facades\Schema;
45
use Illuminate\Database\Schema\Blueprint;
56
use Illuminate\Database\Migrations\Migration;
@@ -13,6 +14,21 @@ class CreateBackupsTable extends Migration
1314
*/
1415
public function up()
1516
{
17+
$db = config('database.default');
18+
// There exists a backups plugin for the 0.7 version of the Panel. However, it didn't properly
19+
// namespace itself so now we have to deal with these tables being in the way of tables we're trying
20+
// to use. For now, just rename them to maintain the data.
21+
$results = DB::select('SELECT TABLE_NAME FROM information_schema.tables WHERE table_schema = ? AND table_name LIKE ? AND table_name NOT LIKE \'%_plugin_bak\'', [
22+
config("database.connections.{$db}.database"),
23+
'backup%'
24+
]);
25+
26+
// Take any of the results, most likely "backups" and "backup_logs" and rename them to have a
27+
// suffix so data isn't completely lost, but they're no longer in the way of this migration...
28+
foreach ($results as $result) {
29+
Schema::rename($result['TABLE_NAME'], $result['TABLE_NAME'] . '_plugin_bak');
30+
}
31+
1632
Schema::create('backups', function (Blueprint $table) {
1733
$table->bigIncrements('id');
1834
$table->unsignedInteger('server_id');

database/migrations/2020_04_26_111208_add_backup_limit_to_servers.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
use Illuminate\Support\Facades\DB;
34
use Illuminate\Support\Facades\Schema;
45
use Illuminate\Database\Schema\Blueprint;
56
use Illuminate\Database\Migrations\Migration;
@@ -13,9 +14,23 @@ class AddBackupLimitToServers extends Migration
1314
*/
1415
public function up()
1516
{
16-
Schema::table('servers', function (Blueprint $table) {
17-
$table->unsignedInteger('backup_limit')->default(0)->after('database_limit');
18-
});
17+
$db = config('database.default');
18+
// Same as in the backups migration, we need to handle that plugin messing with the data structure
19+
// here. If we find a result we'll actually keep the column around since we can maintain that backup
20+
// limit, but we need to correct the column definition a bit.
21+
$results = DB::select('SELECT * FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = ? AND TABLE_NAME = \'servers\' AND COLUMN_NAME = \'backup_limit\'', [
22+
config("database.connections.{$db}.database")
23+
]);
24+
25+
if (count($results) === 1) {
26+
Schema::table('servers', function (Blueprint $table) {
27+
$table->unsignedInteger('backup_limit')->default(0)->change();
28+
});
29+
} else {
30+
Schema::table('servers', function (Blueprint $table) {
31+
$table->unsignedInteger('backup_limit')->default(0)->after('database_limit');
32+
});
33+
}
1934
}
2035

2136
/**

0 commit comments

Comments
 (0)