app/DoctrineMigrations/Version20260810120000.php line 1

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace DoctrineMigrations;
  4. use Doctrine\DBAL\Schema\Schema;
  5. use Doctrine\Migrations\AbstractMigration;
  6. final class Version20260810120000 extends AbstractMigration
  7. {
  8.     public function getDescription(): string
  9.     {
  10.         return 'Create dtb_display_content table and seed featured_products section';
  11.     }
  12.     public function up(Schema $schema): void
  13.     {
  14.         if (!$schema->hasTable('dtb_display_content')) {
  15.             $table $schema->createTable('dtb_display_content');
  16.             $table->addColumn('id''integer', [
  17.                 'autoincrement' => true,
  18.                 'notnull' => true,
  19.                 'unsigned' => true,
  20.             ]);
  21.             $table->setPrimaryKey(['id']);
  22.             $table->addColumn('config_key''string', [
  23.                 'length' => 64,
  24.                 'notnull' => true,
  25.             ]);
  26.             $table->addUniqueIndex(['config_key'], 'dtb_display_content_config_key_idx');
  27.             $table->addColumn('label''string', [
  28.                 'length' => 255,
  29.                 'notnull' => true,
  30.             ]);
  31.             $table->addColumn('product_ids''text', [
  32.                 'notnull' => false,
  33.             ]);
  34.             $table->addColumn('max_items''integer', [
  35.                 'notnull' => true,
  36.                 'unsigned' => true,
  37.                 'default' => 5,
  38.             ]);
  39.             $table->addColumn('help_text''string', [
  40.                 'length' => 500,
  41.                 'notnull' => false,
  42.             ]);
  43.             $table->addColumn('preview_path''string', [
  44.                 'length' => 255,
  45.                 'notnull' => false,
  46.             ]);
  47.             $table->addColumn('sort_no''integer', [
  48.                 'notnull' => true,
  49.                 'unsigned' => true,
  50.                 'default' => 0,
  51.             ]);
  52.             $table->addIndex(['sort_no'], 'dtb_display_content_sort_no_idx');
  53.             $table->addColumn('create_date''datetimetz', [
  54.                 'notnull' => true,
  55.             ]);
  56.             $table->addColumn('update_date''datetimetz', [
  57.                 'notnull' => true,
  58.             ]);
  59.         }
  60.     }
  61.     public function postUp(Schema $schema): void
  62.     {
  63.         $exists $this->connection->fetchOne(
  64.             "SELECT COUNT(*) FROM dtb_display_content WHERE config_key = 'featured_products'"
  65.         );
  66.         if ((int) $exists 0) {
  67.             return;
  68.         }
  69.         $ids = [];
  70.         try {
  71.             $rows $this->connection->fetchAllAssociative(
  72.                 'SELECT id FROM dtb_product WHERE ranking IS NOT NULL ORDER BY ranking ASC, id DESC LIMIT 5'
  73.             );
  74.             foreach ($rows as $row) {
  75.                 $ids[] = (int) $row['id'];
  76.             }
  77.         } catch (\Throwable $e) {
  78.             // ranking column may not exist on some environments
  79.             $ids = [];
  80.         }
  81.         $productIds = empty($ids) ? null '['.implode(','$ids).']';
  82.         $now = (new \DateTime())->format('Y-m-d H:i:s');
  83.         $this->connection->insert('dtb_display_content', [
  84.             'config_key' => 'featured_products',
  85.             'label' => '注目の商品',
  86.             'product_ids' => $productIds,
  87.             'max_items' => 5,
  88.             'help_text' => 'トップページの「注目の商品」に表示する公開商品のID。最大5件。',
  89.             'preview_path' => '/#ranking-section',
  90.             'sort_no' => 1,
  91.             'create_date' => $now,
  92.             'update_date' => $now,
  93.         ]);
  94.     }
  95.     public function down(Schema $schema): void
  96.     {
  97.         if ($schema->hasTable('dtb_display_content')) {
  98.             $schema->dropTable('dtb_display_content');
  99.         }
  100.     }
  101. }