pelican-panel-mirror/database/migrations/2017_11_19_122708_MigratePubPrivFormatToSingleKey.php
Lance Pioch 8261184b57
Officially support PostgreSQL database (#1066)
* Just skip this table because it no longer exists

* Add postgresql

* This no longer needs to be there

* These are the same output in mysql, but different in postgresql

* Fix these migrations for postgresql

* This table no longer exists

* This is expected to be a json column for json operations, required for postgresql

* Shoot for the stars

* Fix pint

* Why was this missing

* Updates

* Restore this

* This needs to be explicit

* Don’t like strings

* Fix these classes

* Use different method to compare dates

* Apparently postgresql doesn’t like case insensitivity

* Postgresql orders it backwards

* Ordered different by postgresql

* Unnecessary and breaking

* Make sure the order is correct for postresql

* Fix this with the order too

* Remove this

* Force email to be lowercased

* Update app/Models/User.php
2025-03-30 14:44:03 -04:00

76 lines
2.7 KiB
PHP

<?php
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;
return new class 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 $exception) {
$decrypted = str_random(32);
} finally {
DB::table('api_keys')->where('id', $item->id)->update([
'secret' => $decrypted,
]);
}
});
});
if (Schema::getConnection()->getDriverName() === 'sqlite') {
Schema::table('api_keys', function (Blueprint $table) {
$table->dropColumn('public');
$table->string('secret', 32)->change();
$table->renameColumn('secret', 'token');
$table->string('token', 32)->unique()->change();
});
} elseif (Schema::getConnection()->getDriverName() === 'pgsql') {
// Rename column 'secret' to 'token'
DB::statement('ALTER TABLE api_keys RENAME COLUMN secret TO token');
// Change data type of 'token' to CHAR(32) and set NOT NULL constraint
DB::statement('ALTER TABLE api_keys ALTER COLUMN token TYPE CHAR(32)');
DB::statement('ALTER TABLE api_keys ALTER COLUMN token SET NOT NULL');
// Add unique constraint on 'token'
DB::statement('ALTER TABLE api_keys ADD CONSTRAINT api_keys_token_unique UNIQUE (token)');
} else {
DB::statement('ALTER TABLE `api_keys` CHANGE `secret` `token` CHAR(32) NOT NULL, ADD UNIQUE INDEX `api_keys_token_unique` (`token`(32))');
}
}
/**
* Reverse the migrations.
*/
public function down(): void
{
DB::statement('ALTER TABLE `api_keys` CHANGE `token` `secret` TEXT, DROP INDEX `api_keys_token_unique`');
Schema::table('api_keys', function (Blueprint $table) {
$table->string('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),
]);
});
});
}
};