Pterodactyl-Panel/database/migrations/2017_02_09_174834_SetupPermissionsPivotTable.php

72 lines
2.4 KiB
PHP
Raw Normal View History

2017-02-10 00:44:07 +01:00
<?php
2017-02-12 21:10:39 +01:00
use Pterodactyl\Models\Subuser;
use Pterodactyl\Models\Permission;
2017-02-10 00:44:07 +01:00
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class SetupPermissionsPivotTable extends Migration
{
/**
* Run the migrations.
*/
public function up()
{
Schema::table('permissions', function (Blueprint $table) {
$table->unsignedInteger('subuser_id')->after('id');
});
DB::transaction(function () {
2017-02-12 21:10:39 +01:00
foreach (Subuser::all() as &$subuser) {
2017-02-10 00:44:07 +01:00
Permission::where('user_id', $subuser->user_id)->where('server_id', $subuser->server_id)->update([
'subuser_id' => $subuser->id,
]);
}
});
Schema::table('permissions', function (Blueprint $table) {
$table->dropForeign('permissions_server_id_foreign');
$table->dropIndex('permissions_server_id_foreign');
$table->dropForeign('permissions_user_id_foreign');
$table->dropIndex('permissions_user_id_foreign');
$table->dropColumn('server_id');
$table->dropColumn('user_id');
2017-02-10 01:38:54 +01:00
$table->dropColumn('created_at');
$table->dropColumn('updated_at');
2017-02-10 00:44:07 +01:00
$table->foreign('subuser_id')->references('id')->on('subusers');
});
}
/**
* Reverse the migrations.
*/
public function down()
{
Schema::table('permissions', function (Blueprint $table) {
$table->unsignedInteger('server_id')->after('subuser_id');
$table->unsignedInteger('user_id')->after('server_id');
2017-02-10 01:38:54 +01:00
$table->timestamps();
2017-02-10 00:44:07 +01:00
});
DB::transaction(function () {
2017-02-12 21:10:39 +01:00
foreach (Subuser::all() as &$subuser) {
2017-02-10 00:44:07 +01:00
Permission::where('subuser_id', $subuser->id)->update([
'user_id' => $subuser->user_id,
'server_id' => $subuser->server_id,
]);
}
});
Schema::table('permissions', function (Blueprint $table) {
$table->dropForeign('permissions_subuser_id_foreign');
$table->dropIndex('permissions_subuser_id_foreign');
$table->dropColumn('subuser_id');
$table->foreign('server_id')->references('id')->on('servers');
$table->foreign('user_id')->references('id')->on('users');
});
}
}