<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
final class Version20260810120000 extends AbstractMigration
{
public function getDescription(): string
{
return 'Create dtb_display_content table and seed featured_products section';
}
public function up(Schema $schema): void
{
if (!$schema->hasTable('dtb_display_content')) {
$table = $schema->createTable('dtb_display_content');
$table->addColumn('id', 'integer', [
'autoincrement' => true,
'notnull' => true,
'unsigned' => true,
]);
$table->setPrimaryKey(['id']);
$table->addColumn('config_key', 'string', [
'length' => 64,
'notnull' => true,
]);
$table->addUniqueIndex(['config_key'], 'dtb_display_content_config_key_idx');
$table->addColumn('label', 'string', [
'length' => 255,
'notnull' => true,
]);
$table->addColumn('product_ids', 'text', [
'notnull' => false,
]);
$table->addColumn('max_items', 'integer', [
'notnull' => true,
'unsigned' => true,
'default' => 5,
]);
$table->addColumn('help_text', 'string', [
'length' => 500,
'notnull' => false,
]);
$table->addColumn('preview_path', 'string', [
'length' => 255,
'notnull' => false,
]);
$table->addColumn('sort_no', 'integer', [
'notnull' => true,
'unsigned' => true,
'default' => 0,
]);
$table->addIndex(['sort_no'], 'dtb_display_content_sort_no_idx');
$table->addColumn('create_date', 'datetimetz', [
'notnull' => true,
]);
$table->addColumn('update_date', 'datetimetz', [
'notnull' => true,
]);
}
}
public function postUp(Schema $schema): void
{
$exists = $this->connection->fetchOne(
"SELECT COUNT(*) FROM dtb_display_content WHERE config_key = 'featured_products'"
);
if ((int) $exists > 0) {
return;
}
$ids = [];
try {
$rows = $this->connection->fetchAllAssociative(
'SELECT id FROM dtb_product WHERE ranking IS NOT NULL ORDER BY ranking ASC, id DESC LIMIT 5'
);
foreach ($rows as $row) {
$ids[] = (int) $row['id'];
}
} catch (\Throwable $e) {
// ranking column may not exist on some environments
$ids = [];
}
$productIds = empty($ids) ? null : '['.implode(',', $ids).']';
$now = (new \DateTime())->format('Y-m-d H:i:s');
$this->connection->insert('dtb_display_content', [
'config_key' => 'featured_products',
'label' => '注目の商品',
'product_ids' => $productIds,
'max_items' => 5,
'help_text' => 'トップページの「注目の商品」に表示する公開商品のID。最大5件。',
'preview_path' => '/#ranking-section',
'sort_no' => 1,
'create_date' => $now,
'update_date' => $now,
]);
}
public function down(Schema $schema): void
{
if ($schema->hasTable('dtb_display_content')) {
$schema->dropTable('dtb_display_content');
}
}
}