migrations/Version20251103155616.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 Version20251103155616 extends AbstractMigration
  10. {
  11.     public function getDescription(): string
  12.     {
  13.         return 'Final alignment of campaigns, ads, and targeting structure (MySQL-compatible version)';
  14.     }
  15.     public function up(Schema $schema): void
  16.     {
  17.         // 1. Remover campos obsoletos da tabela std_ads_campaigns (se existirem)
  18.         try {
  19.             $this->addSql('ALTER TABLE std_ads_campaigns DROP COLUMN daily_click_cap');
  20.         } catch (\Throwable $e) {
  21.             // ignore if column not exists
  22.         }
  23.         try {
  24.             $this->addSql('ALTER TABLE std_ads_campaigns DROP COLUMN cpc_value');
  25.         } catch (\Throwable $e) {
  26.             // ignore if column not exists
  27.         }
  28.         // 2. Adicionar colunas de controle a std_advertisements (se não existirem)
  29.         try {
  30.             $this->addSql('ALTER TABLE std_advertisements ADD COLUMN daily_click_cap INT DEFAULT 0 AFTER clicks');
  31.         } catch (\Throwable $e) {
  32.             // ignore if already exists
  33.         }
  34.         try {
  35.             $this->addSql('ALTER TABLE std_advertisements ADD COLUMN cpc_value DECIMAL(10,2) DEFAULT 0.00 AFTER daily_click_cap');
  36.         } catch (\Throwable $e) {
  37.             // ignore if already exists
  38.         }
  39.         try {
  40.             $this->addSql('ALTER TABLE std_advertisements ADD COLUMN impressions INT DEFAULT 0 AFTER cpc_value');
  41.         } catch (\Throwable $e) {
  42.             // ignore if already exists
  43.         }
  44.         // 3. Corrigir relação das regras (campaign_id → ad_id)
  45.         try {
  46.             $this->addSql('ALTER TABLE std_ads_targeting_rules DROP FOREIGN KEY fk_targeting_campaign');
  47.         } catch (\Throwable $e) {
  48.             // ignore if FK already removed
  49.         }
  50.         try {
  51.             $this->addSql('ALTER TABLE std_ads_targeting_rules DROP COLUMN campaign_id');
  52.         } catch (\Throwable $e) {
  53.             // ignore if column already dropped
  54.         }
  55.         try {
  56.             $this->addSql('ALTER TABLE std_ads_targeting_rules ADD COLUMN ad_id INT UNSIGNED NOT NULL AFTER id');
  57.         } catch (\Throwable $e) {
  58.             // ignore if already exists
  59.         }
  60.         try {
  61.             $this->addSql('CREATE INDEX idx_targeting_ad ON std_ads_targeting_rules (ad_id)');
  62.         } catch (\Throwable $e) {
  63.             // ignore if index already exists
  64.         }
  65.         try {
  66.             $this->addSql('
  67.                 ALTER TABLE std_ads_targeting_rules
  68.                 ADD CONSTRAINT fk_targeting_ad
  69.                 FOREIGN KEY (ad_id) REFERENCES std_advertisements (id) ON DELETE CASCADE
  70.             ');
  71.         } catch (\Throwable $e) {
  72.             // ignore if FK already exists
  73.         }
  74.         // 4. Corrigir nome de coluna em std_ads_targeting_themes
  75.         try {
  76.             $this->addSql('ALTER TABLE std_ads_targeting_themes CHANGE theme_page_id theme_id INT UNSIGNED NOT NULL');
  77.         } catch (\Throwable $e) {
  78.             // ignore if already changed
  79.         }
  80.     }
  81.     public function down(Schema $schema): void
  82.     {
  83.         // Reverter estrutura
  84.         try {
  85.             $this->addSql('ALTER TABLE std_ads_campaigns ADD COLUMN daily_click_cap INT DEFAULT 0 AFTER end_at');
  86.             $this->addSql('ALTER TABLE std_ads_campaigns ADD COLUMN cpc_value DECIMAL(10,2) DEFAULT 0.00 AFTER daily_click_cap');
  87.         } catch (\Throwable $e) {
  88.         }
  89.         try {
  90.             $this->addSql('ALTER TABLE std_advertisements DROP COLUMN daily_click_cap');
  91.             $this->addSql('ALTER TABLE std_advertisements DROP COLUMN cpc_value');
  92.             $this->addSql('ALTER TABLE std_advertisements DROP COLUMN impressions');
  93.         } catch (\Throwable $e) {
  94.         }
  95.         try {
  96.             $this->addSql('ALTER TABLE std_ads_targeting_rules DROP FOREIGN KEY fk_targeting_ad');
  97.             $this->addSql('ALTER TABLE std_ads_targeting_rules DROP COLUMN ad_id');
  98.             $this->addSql('ALTER TABLE std_ads_targeting_rules ADD COLUMN campaign_id INT UNSIGNED NOT NULL AFTER id');
  99.             $this->addSql('CREATE INDEX idx_targeting_campaign ON std_ads_targeting_rules (campaign_id)');
  100.             $this->addSql('
  101.                 ALTER TABLE std_ads_targeting_rules
  102.                 ADD CONSTRAINT fk_targeting_campaign
  103.                 FOREIGN KEY (campaign_id) REFERENCES std_ads_campaigns (id) ON DELETE CASCADE
  104.             ');
  105.         } catch (\Throwable $e) {
  106.         }
  107.         try {
  108.             $this->addSql('ALTER TABLE std_ads_targeting_themes CHANGE theme_id theme_page_id INT UNSIGNED NOT NULL');
  109.         } catch (\Throwable $e) {
  110.         }
  111.     }
  112. }