<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* 商品テーブルに送料無料設定項目を追加するためのマイグレーション
* dtb_productテーブルにship_free_flgとship_free_textフィールドを追加
*/
final class Version20250918140000 extends AbstractMigration
{
public function getDescription(): string
{
return 'dtb_productテーブルにship_free_flg(smallint)とship_free_text(varchar(512))を追加 - 商品ごとの送料無料設定用';
}
public function up(Schema $schema): void
{
// カラムが存在するかチェック
$schemaManager = $this->connection->createSchemaManager();
// テーブルが存在するかチェック
if (!$schemaManager->tablesExist(['dtb_product'])) {
// テーブルが存在しない場合はスキップ
return;
}
$columns = $schemaManager->listTableColumns('dtb_product');
$hasShipFreeFlg = false;
$hasShipFreeText = false;
foreach ($columns as $column) {
if ($column->getName() === 'ship_free_flg') {
$hasShipFreeFlg = true;
}
if ($column->getName() === 'ship_free_text') {
$hasShipFreeText = true;
}
}
// ship_free_flgカラムが存在しない場合のみ追加
if (!$hasShipFreeFlg) {
$this->addSql('ALTER TABLE dtb_product ADD ship_free_flg SMALLINT DEFAULT 0 NOT NULL');
}
// ship_free_textカラムが存在しない場合のみ追加
if (!$hasShipFreeText) {
$this->addSql('ALTER TABLE dtb_product ADD ship_free_text VARCHAR(512) DEFAULT NULL');
}
}
public function down(Schema $schema): void
{
$schemaManager = $this->connection->createSchemaManager();
if (!$schemaManager->tablesExist(['dtb_product'])) {
return;
}
$columns = $schemaManager->listTableColumns('dtb_product');
$columnNames = array_map(function($column) {
return $column->getName();
}, $columns);
// ship_free_flgカラムを削除
if (in_array('ship_free_flg', $columnNames)) {
$this->addSql('ALTER TABLE dtb_product DROP COLUMN ship_free_flg');
}
// ship_free_textカラムを削除
if (in_array('ship_free_text', $columnNames)) {
$this->addSql('ALTER TABLE dtb_product DROP COLUMN ship_free_text');
}
}
}