<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Migration: garante que todos os campos da entidade StdAdsClients existam na tabela std_ads_clients.
*/
final class Version20251027191440 extends AbstractMigration
{
public function getDescription(): string
{
return 'Adiciona campos faltantes na tabela std_ads_clients (contact_name, company_vat, notes)';
}
public function up(Schema $schema): void
{
$this->addSql("
ALTER TABLE std_ads_clients
MODIFY COLUMN created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
MODIFY COLUMN updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;
");
// Verifica e adiciona campo contact_name
$this->addSql("
SET @col_exists := (
SELECT COUNT(*)
FROM information_schema.COLUMNS
WHERE TABLE_NAME = 'std_ads_clients' AND COLUMN_NAME = 'contact_name'
);
");
$this->addSql("
SET @query := IF(@col_exists = 0,
'ALTER TABLE std_ads_clients ADD COLUMN contact_name VARCHAR(120) NULL AFTER name;',
'SELECT 1;'
);
");
$this->addSql("PREPARE stmt FROM @query;");
$this->addSql("EXECUTE stmt;");
$this->addSql("DEALLOCATE PREPARE stmt;");
// Verifica e adiciona campo company_vat
$this->addSql("
SET @col_exists := (
SELECT COUNT(*)
FROM information_schema.COLUMNS
WHERE TABLE_NAME = 'std_ads_clients' AND COLUMN_NAME = 'company_vat'
);
");
$this->addSql("
SET @query := IF(@col_exists = 0,
'ALTER TABLE std_ads_clients ADD COLUMN company_vat VARCHAR(50) NULL AFTER nif;',
'SELECT 1;'
);
");
$this->addSql("PREPARE stmt FROM @query;");
$this->addSql("EXECUTE stmt;");
$this->addSql("DEALLOCATE PREPARE stmt;");
// Verifica e adiciona campo notes
$this->addSql("
SET @col_exists := (
SELECT COUNT(*)
FROM information_schema.COLUMNS
WHERE TABLE_NAME = 'std_ads_clients' AND COLUMN_NAME = 'notes'
);
");
$this->addSql("
SET @query := IF(@col_exists = 0,
'ALTER TABLE std_ads_clients ADD COLUMN notes TEXT NULL AFTER company_vat;',
'SELECT 1;'
);
");
$this->addSql("PREPARE stmt FROM @query;");
$this->addSql("EXECUTE stmt;");
$this->addSql("DEALLOCATE PREPARE stmt;");
}
public function down(Schema $schema): void
{
$this->addSql("
ALTER TABLE std_ads_clients
DROP COLUMN contact_name,
DROP COLUMN company_vat,
DROP COLUMN notes;
");
}
}