<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* 商品テーブルに購入点数制限カラムを追加
*/
final class Version20251121120000 extends AbstractMigration
{
public function getDescription(): string
{
return '商品テーブル(dtb_product)に購入点数制限(purchase_limit)カラムを追加';
}
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 = 'purchase_limit'
");
if (!$columnExists) {
$this->addSql('ALTER TABLE dtb_product ADD purchase_limit INT UNSIGNED DEFAULT NULL COMMENT \'購入点数制限(NULL or 0 = 制限なし)\'');
}
}
}
// 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 = 'purchase_limit'
");
if (!$columnExists) {
$this->addSql('ALTER TABLE dtb_product ADD purchase_limit INT DEFAULT NULL');
$this->addSql('COMMENT ON COLUMN dtb_product.purchase_limit IS \'購入点数制限(NULL or 0 = 制限なし)\'');
}
}
}
}
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 = 'purchase_limit'
");
if ($columnExists) {
$this->addSql('ALTER TABLE dtb_product DROP COLUMN purchase_limit');
}
}
// 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 = 'purchase_limit'
");
if ($columnExists) {
$this->addSql('ALTER TABLE dtb_product DROP COLUMN purchase_limit');
}
}
}
}