migrations/Version20251027191440.php line 1

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace DoctrineMigrations;
  4. use Doctrine\DBAL\Schema\Schema;
  5. use Doctrine\Migrations\AbstractMigration;
  6. /**
  7.  * Migration: garante que todos os campos da entidade StdAdsClients existam na tabela std_ads_clients.
  8.  */
  9. final class Version20251027191440 extends AbstractMigration
  10. {
  11.     public function getDescription(): string
  12.     {
  13.         return 'Adiciona campos faltantes na tabela std_ads_clients (contact_name, company_vat, notes)';
  14.     }
  15.     public function up(Schema $schema): void
  16.     {
  17.         $this->addSql("
  18.             ALTER TABLE std_ads_clients 
  19.             MODIFY COLUMN created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  20.             MODIFY COLUMN updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;
  21.         ");
  22.         
  23.         // Verifica e adiciona campo contact_name
  24.         $this->addSql("
  25.             SET @col_exists := (
  26.                 SELECT COUNT(*) 
  27.                 FROM information_schema.COLUMNS 
  28.                 WHERE TABLE_NAME = 'std_ads_clients' AND COLUMN_NAME = 'contact_name'
  29.             );
  30.         ");
  31.         $this->addSql("
  32.             SET @query := IF(@col_exists = 0, 
  33.                 'ALTER TABLE std_ads_clients ADD COLUMN contact_name VARCHAR(120) NULL AFTER name;', 
  34.                 'SELECT 1;'
  35.             );
  36.         ");
  37.         $this->addSql("PREPARE stmt FROM @query;");
  38.         $this->addSql("EXECUTE stmt;");
  39.         $this->addSql("DEALLOCATE PREPARE stmt;");
  40.         // Verifica e adiciona campo company_vat
  41.         $this->addSql("
  42.             SET @col_exists := (
  43.                 SELECT COUNT(*) 
  44.                 FROM information_schema.COLUMNS 
  45.                 WHERE TABLE_NAME = 'std_ads_clients' AND COLUMN_NAME = 'company_vat'
  46.             );
  47.         ");
  48.         $this->addSql("
  49.             SET @query := IF(@col_exists = 0, 
  50.                 'ALTER TABLE std_ads_clients ADD COLUMN company_vat VARCHAR(50) NULL AFTER nif;', 
  51.                 'SELECT 1;'
  52.             );
  53.         ");
  54.         $this->addSql("PREPARE stmt FROM @query;");
  55.         $this->addSql("EXECUTE stmt;");
  56.         $this->addSql("DEALLOCATE PREPARE stmt;");
  57.         // Verifica e adiciona campo notes
  58.         $this->addSql("
  59.             SET @col_exists := (
  60.                 SELECT COUNT(*) 
  61.                 FROM information_schema.COLUMNS 
  62.                 WHERE TABLE_NAME = 'std_ads_clients' AND COLUMN_NAME = 'notes'
  63.             );
  64.         ");
  65.         $this->addSql("
  66.             SET @query := IF(@col_exists = 0, 
  67.                 'ALTER TABLE std_ads_clients ADD COLUMN notes TEXT NULL AFTER company_vat;', 
  68.                 'SELECT 1;'
  69.             );
  70.         ");
  71.         $this->addSql("PREPARE stmt FROM @query;");
  72.         $this->addSql("EXECUTE stmt;");
  73.         $this->addSql("DEALLOCATE PREPARE stmt;");
  74.     }
  75.     public function down(Schema $schema): void
  76.     {
  77.         $this->addSql("
  78.             ALTER TABLE std_ads_clients
  79.             DROP COLUMN contact_name,
  80.             DROP COLUMN company_vat,
  81.             DROP COLUMN notes;
  82.         ");
  83.     }
  84. }