migrations/Version20251126162334.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.  * Auto-generated Migration: Please modify to your needs!
  8.  */
  9. final class Version20251126162334 extends AbstractMigration
  10. {
  11.     public function getDescription(): string
  12.     {
  13.         return 'Adiciona contact_name, company_vat e notes em std_ads_clients.';
  14.     }
  15.     public function up(Schema $schema): void
  16.     {
  17.         $this->addSql("
  18.             ALTER TABLE std_ads_clients 
  19.                 MODIFY created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  20.                 MODIFY updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
  21.         ");
  22.         $exists $this->connection->fetchOne("
  23.             SELECT COUNT(*) 
  24.             FROM information_schema.COLUMNS 
  25.             WHERE TABLE_NAME = 'std_ads_clients'
  26.               AND COLUMN_NAME = 'contact_name'
  27.               AND TABLE_SCHEMA = DATABASE()
  28.         ");
  29.         if ((int)$exists === 0) {
  30.             $this->addSql("
  31.                 ALTER TABLE std_ads_clients 
  32.                     ADD COLUMN contact_name VARCHAR(120) NULL AFTER name
  33.             ");
  34.         }
  35.         $exists $this->connection->fetchOne("
  36.             SELECT COUNT(*) 
  37.             FROM information_schema.COLUMNS 
  38.             WHERE TABLE_NAME = 'std_ads_clients'
  39.               AND COLUMN_NAME = 'company_vat'
  40.               AND TABLE_SCHEMA = DATABASE()
  41.         ");
  42.         if ((int)$exists === 0) {
  43.             $this->addSql("
  44.                 ALTER TABLE std_ads_clients 
  45.                     ADD COLUMN company_vat VARCHAR(50) NULL AFTER nif
  46.             ");
  47.         }
  48.         $exists $this->connection->fetchOne("
  49.             SELECT COUNT(*) 
  50.             FROM information_schema.COLUMNS 
  51.             WHERE TABLE_NAME = 'std_ads_clients'
  52.               AND COLUMN_NAME = 'notes'
  53.               AND TABLE_SCHEMA = DATABASE()
  54.         ");
  55.         if ((int)$exists === 0) {
  56.             $this->addSql("
  57.                 ALTER TABLE std_ads_clients 
  58.                     ADD COLUMN notes TEXT NULL AFTER company_vat
  59.             ");
  60.         }
  61.     }
  62.     public function down(Schema $schema): void
  63.     {
  64.         // remove se existir
  65.         $cols = ['contact_name''company_vat''notes'];
  66.         foreach ($cols as $col) {
  67.             $exists $this->connection->fetchOne("
  68.                 SELECT COUNT(*)
  69.                 FROM information_schema.COLUMNS
  70.                 WHERE TABLE_NAME = 'std_ads_clients'
  71.                   AND COLUMN_NAME = '$col'
  72.                   AND TABLE_SCHEMA = DATABASE()
  73.             ");
  74.             if ((int)$exists === 1) {
  75.                 $this->addSql("ALTER TABLE std_ads_clients DROP COLUMN $col");
  76.             }
  77.         }
  78.     }
  79. }