misc_pterodactyl-panel/database/migrations/2017_11_19_122708_MigratePubPrivFormatToSingleKey.php
Lance Pioch 3bf5a71802
PostgreSQL Support (#4486)
Co-authored-by: Matthew Penner <matthew@pterodactyl.io>
2022-11-25 13:29:04 -07:00

74 lines
2.2 KiB
PHP

<?php
use Illuminate\Support\Str;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Contracts\Encryption\DecryptException;
class MigratePubPrivFormatToSingleKey extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
DB::transaction(function () {
DB::table('api_keys')->get()->each(function ($item) {
try {
$decrypted = Crypt::decrypt($item->secret);
} catch (DecryptException) {
$decrypted = str_random(32);
} finally {
DB::table('api_keys')->where('id', $item->id)->update([
'secret' => $decrypted,
]);
}
});
});
Schema::table('api_keys', function (Blueprint $table) {
$table->dropColumn('public');
$table->renameColumn('secret', 'token');
});
Schema::table('api_keys', function (Blueprint $table) {
$table->char('token', 32)->change();
$table->unique('token');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('api_keys', function (Blueprint $table) {
$table->dropUnique(['token']);
$table->renameColumn('token', 'secret');
});
Schema::table('api_keys', function (Blueprint $table) {
$table->dropUnique('token');
$table->text('token')->change();
});
Schema::table('api_keys', function (Blueprint $table) {
$table->renameColumn('token', 'secret');
$table->text('secret')->nullable()->change();
$table->char('public', 16)->after('user_id');
});
DB::transaction(function () {
DB::table('api_keys')->get()->each(function ($item) {
DB::table('api_keys')->where('id', $item->id)->update([
'public' => Str::random(16),
'secret' => Crypt::encrypt($item->secret),
]);
});
});
}
}