Files
thetool/db/migrations/20250709125000_asset_management_add_image_ids.php
2025-07-09 12:54:01 +02:00

53 lines
1.8 KiB
PHP

<?php
declare(strict_types=1);
use Phinx\Migration\AbstractMigration;
final class AssetManagementAddImageIds extends AbstractMigration
{
public function up(): void
{
if ($this->getEnvironment() == "thetool") {
$table = $this->table("AssetManagement");
$table->renameColumn('imageId', 'mainImageId');
$table->changeColumn('mainImageId', 'integer', [
'null' => true,
'default' => null,
'comment' => 'Foreign key to the File table for the asset\'s main image',
]);
$table->addColumn('imageIds', 'json', [
'null' => true,
'default' => null,
'after' => 'mainImageId',
'comment' => 'An array of file IDs for all asset images',
]);
$table->update();
$this->execute("
UPDATE `AssetManagement`
SET `imageIds` = JSON_ARRAY(`mainImageId`)
WHERE `mainImageId` IS NOT NULL;
");
}
}
public function down(): void
{
if ($this->getEnvironment() == "thetool") {
$table = $this->table("AssetManagement");
$this->execute("
UPDATE `AssetManagement`
SET `mainImageId` = JSON_UNQUOTE(JSON_EXTRACT(`imageIds`, '$[0]'))
WHERE JSON_TYPE(`imageIds`) = 'ARRAY' AND JSON_LENGTH(`imageIds`) > 0;
");
$table->removeColumn('imageIds');
$table->renameColumn('mainImageId', 'imageId');
$table->changeColumn('imageId', 'integer', [
'null' => true,
'default' => null,
'comment' => 'Foreign key to the File table for the asset\'s image',
]);
$table->update();
}
}
}