<?php

namespace KupShop\FeedsBundle\Upgrade;

class HeurekaSKUpgrade extends \UpgradeNew
{
    public function check_sharedFeedHeurekaSK()
    {
        return $this->checkTableExists('feed_heureka_sk', 'kupshop_shared');
    }

    /** Add kupshop_shared.feed_heureka_sk */
    public function upgrade_sharedFeedHeurekaSK()
    {
        sqlQuery('CREATE TABLE IF NOT EXISTS kupshop_shared.feed_heureka_sk
            (
                id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
                name VARCHAR(100) NOT NULL,
                category_text VARCHAR(250) NOT NULL
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
        ');

        $this->upgradeOK();
    }

    public function check_sectionFeedHeurekaSKColumn()
    {
        return $this->checkColumnExists('sections', 'feed_heureka_sk');
    }

    /** Add sections.feed_heureka_sk column */
    public function upgrade_sectionFeedHeurekaSKColumn()
    {
        sqlQuery('
            ALTER TABLE sections ADD COLUMN feed_heureka_sk INT(10) UNSIGNED NULL AFTER feed_heureka;
        ');

        sqlQuery('UPDATE sections SET feed_heureka_sk = feed_heureka
            WHERE sections.feed_heureka IN (SELECT id FROM kupshop_shared.feed_heureka_sk);');

        $this->upgradeOK();
    }
}
