<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* 商品テーブルに商品注意書きカラムを追加
*/
final class Version20251121000000 extends AbstractMigration
{
public function getDescription(): string
{
return '商品テーブル(dtb_product)に商品注意書き(product_caution_note)カラムを追加';
}
public function up(Schema $schema): void
{
// MySQL
if ($this->connection->getDatabasePlatform()->getName() === 'mysql') {
// テーブルが存在するか確認
$tableExists = $this->connection->fetchOne("
SELECT COUNT(*)
FROM information_schema.TABLES
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'dtb_product'
");
if ($tableExists) {
// カラムが存在しない場合のみ追加
$columnExists = $this->connection->fetchOne("
SELECT COUNT(*)
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'dtb_product'
AND COLUMN_NAME = 'product_caution_note'
");
if (!$columnExists) {
$this->addSql('ALTER TABLE dtb_product ADD product_caution_note TEXT DEFAULT NULL');
}
}
}
// PostgreSQL
else if ($this->connection->getDatabasePlatform()->getName() === 'postgresql') {
// テーブルが存在するか確認
$tableExists = $this->connection->fetchOne("
SELECT COUNT(*)
FROM information_schema.tables
WHERE table_schema = 'public'
AND table_name = 'dtb_product'
");
if ($tableExists) {
// カラムが存在しない場合のみ追加
$columnExists = $this->connection->fetchOne("
SELECT COUNT(*)
FROM information_schema.columns
WHERE table_schema = 'public'
AND table_name = 'dtb_product'
AND column_name = 'product_caution_note'
");
if (!$columnExists) {
$this->addSql('ALTER TABLE dtb_product ADD product_caution_note TEXT DEFAULT NULL');
}
}
}
}
public function down(Schema $schema): void
{
// MySQL
if ($this->connection->getDatabasePlatform()->getName() === 'mysql') {
// カラムが存在する場合のみ削除
$columnExists = $this->connection->fetchOne("
SELECT COUNT(*)
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'dtb_product'
AND COLUMN_NAME = 'product_caution_note'
");
if ($columnExists) {
$this->addSql('ALTER TABLE dtb_product DROP product_caution_note');
}
}
// PostgreSQL
else if ($this->connection->getDatabasePlatform()->getName() === 'postgresql') {
// カラムが存在する場合のみ削除
$columnExists = $this->connection->fetchOne("
SELECT COUNT(*)
FROM information_schema.columns
WHERE table_schema = 'public'
AND table_name = 'dtb_product'
AND column_name = 'product_caution_note'
");
if ($columnExists) {
$this->addSql('ALTER TABLE dtb_product DROP product_caution_note');
}
}
}
}