<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20251126162334 extends AbstractMigration
{
public function getDescription(): string
{
return 'Adiciona contact_name, company_vat e notes em std_ads_clients.';
}
public function up(Schema $schema): void
{
$this->addSql("
ALTER TABLE std_ads_clients
MODIFY created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
MODIFY updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
");
$exists = $this->connection->fetchOne("
SELECT COUNT(*)
FROM information_schema.COLUMNS
WHERE TABLE_NAME = 'std_ads_clients'
AND COLUMN_NAME = 'contact_name'
AND TABLE_SCHEMA = DATABASE()
");
if ((int)$exists === 0) {
$this->addSql("
ALTER TABLE std_ads_clients
ADD COLUMN contact_name VARCHAR(120) NULL AFTER name
");
}
$exists = $this->connection->fetchOne("
SELECT COUNT(*)
FROM information_schema.COLUMNS
WHERE TABLE_NAME = 'std_ads_clients'
AND COLUMN_NAME = 'company_vat'
AND TABLE_SCHEMA = DATABASE()
");
if ((int)$exists === 0) {
$this->addSql("
ALTER TABLE std_ads_clients
ADD COLUMN company_vat VARCHAR(50) NULL AFTER nif
");
}
$exists = $this->connection->fetchOne("
SELECT COUNT(*)
FROM information_schema.COLUMNS
WHERE TABLE_NAME = 'std_ads_clients'
AND COLUMN_NAME = 'notes'
AND TABLE_SCHEMA = DATABASE()
");
if ((int)$exists === 0) {
$this->addSql("
ALTER TABLE std_ads_clients
ADD COLUMN notes TEXT NULL AFTER company_vat
");
}
}
public function down(Schema $schema): void
{
// remove se existir
$cols = ['contact_name', 'company_vat', 'notes'];
foreach ($cols as $col) {
$exists = $this->connection->fetchOne("
SELECT COUNT(*)
FROM information_schema.COLUMNS
WHERE TABLE_NAME = 'std_ads_clients'
AND COLUMN_NAME = '$col'
AND TABLE_SCHEMA = DATABASE()
");
if ((int)$exists === 1) {
$this->addSql("ALTER TABLE std_ads_clients DROP COLUMN $col");
}
}
}
}