<?phpdeclare(strict_types=1);namespace DoctrineMigrations;use Doctrine\DBAL\Schema\Schema;use Doctrine\Migrations\AbstractMigration;/** * Auto-generated Migration: Please modify to your needs! */final class Version20251103155616 extends AbstractMigration{ public function getDescription(): string { return 'Final alignment of campaigns, ads, and targeting structure (MySQL-compatible version)'; } public function up(Schema $schema): void { // 1. Remover campos obsoletos da tabela std_ads_campaigns (se existirem) try { $this->addSql('ALTER TABLE std_ads_campaigns DROP COLUMN daily_click_cap'); } catch (\Throwable $e) { // ignore if column not exists } try { $this->addSql('ALTER TABLE std_ads_campaigns DROP COLUMN cpc_value'); } catch (\Throwable $e) { // ignore if column not exists } // 2. Adicionar colunas de controle a std_advertisements (se não existirem) try { $this->addSql('ALTER TABLE std_advertisements ADD COLUMN daily_click_cap INT DEFAULT 0 AFTER clicks'); } catch (\Throwable $e) { // ignore if already exists } try { $this->addSql('ALTER TABLE std_advertisements ADD COLUMN cpc_value DECIMAL(10,2) DEFAULT 0.00 AFTER daily_click_cap'); } catch (\Throwable $e) { // ignore if already exists } try { $this->addSql('ALTER TABLE std_advertisements ADD COLUMN impressions INT DEFAULT 0 AFTER cpc_value'); } catch (\Throwable $e) { // ignore if already exists } // 3. Corrigir relação das regras (campaign_id → ad_id) try { $this->addSql('ALTER TABLE std_ads_targeting_rules DROP FOREIGN KEY fk_targeting_campaign'); } catch (\Throwable $e) { // ignore if FK already removed } try { $this->addSql('ALTER TABLE std_ads_targeting_rules DROP COLUMN campaign_id'); } catch (\Throwable $e) { // ignore if column already dropped } try { $this->addSql('ALTER TABLE std_ads_targeting_rules ADD COLUMN ad_id INT UNSIGNED NOT NULL AFTER id'); } catch (\Throwable $e) { // ignore if already exists } try { $this->addSql('CREATE INDEX idx_targeting_ad ON std_ads_targeting_rules (ad_id)'); } catch (\Throwable $e) { // ignore if index already exists } try { $this->addSql(' ALTER TABLE std_ads_targeting_rules ADD CONSTRAINT fk_targeting_ad FOREIGN KEY (ad_id) REFERENCES std_advertisements (id) ON DELETE CASCADE '); } catch (\Throwable $e) { // ignore if FK already exists } // 4. Corrigir nome de coluna em std_ads_targeting_themes try { $this->addSql('ALTER TABLE std_ads_targeting_themes CHANGE theme_page_id theme_id INT UNSIGNED NOT NULL'); } catch (\Throwable $e) { // ignore if already changed } } public function down(Schema $schema): void { // Reverter estrutura try { $this->addSql('ALTER TABLE std_ads_campaigns ADD COLUMN daily_click_cap INT DEFAULT 0 AFTER end_at'); $this->addSql('ALTER TABLE std_ads_campaigns ADD COLUMN cpc_value DECIMAL(10,2) DEFAULT 0.00 AFTER daily_click_cap'); } catch (\Throwable $e) { } try { $this->addSql('ALTER TABLE std_advertisements DROP COLUMN daily_click_cap'); $this->addSql('ALTER TABLE std_advertisements DROP COLUMN cpc_value'); $this->addSql('ALTER TABLE std_advertisements DROP COLUMN impressions'); } catch (\Throwable $e) { } try { $this->addSql('ALTER TABLE std_ads_targeting_rules DROP FOREIGN KEY fk_targeting_ad'); $this->addSql('ALTER TABLE std_ads_targeting_rules DROP COLUMN ad_id'); $this->addSql('ALTER TABLE std_ads_targeting_rules ADD COLUMN campaign_id INT UNSIGNED NOT NULL AFTER id'); $this->addSql('CREATE INDEX idx_targeting_campaign ON std_ads_targeting_rules (campaign_id)'); $this->addSql(' ALTER TABLE std_ads_targeting_rules ADD CONSTRAINT fk_targeting_campaign FOREIGN KEY (campaign_id) REFERENCES std_ads_campaigns (id) ON DELETE CASCADE '); } catch (\Throwable $e) { } try { $this->addSql('ALTER TABLE std_ads_targeting_themes CHANGE theme_id theme_page_id INT UNSIGNED NOT NULL'); } catch (\Throwable $e) { } }}