<?php

namespace KupShop\ElninoBundle\Resources\upgrade;

class AbraPicturesUpgrade extends \UpgradeNew
{
    public function check_columnIdVariation()
    {
        return !$this->checkColumnExists('abra_pictures', 'id_variation');
    }

    /** 'abra_pictures' rename id_variation column */
    public function upgrade_columnIdVariation()
    {
        try {
            sqlQuery('ALTER TABLE abra_pictures DROP FOREIGN KEY abra_pictures_ibfk_1;');
            sqlQuery('ALTER TABLE abra_pictures DROP FOREIGN KEY abra_pictures_ibfk_3;');
        } catch (\Exception $e) {
        }

        sqlQuery('ALTER TABLE abra_pictures CHANGE id_variation id_abra_product VARCHAR(30) DEFAULT NULL;');
        sqlQuery('ALTER TABLE abra_pictures ADD FOREIGN KEY (abra_pictures_ibfk_1) REFERENCES abra_products(id_abra)');

        $this->upgradeOK();
    }

    public function check_ForeignKey()
    {
        return $this->checkForeignKeyExists('abra_pictures', 'id_abra_product');
    }

    /** abra_pictures add foreign key on id_abra_product */
    public function upgrade_ForeignKey()
    {
        try {
            sqlQuery('UPDATE abra_pictures SET id_abra_product = null WHERE id_abra_product = "" ');
            sqlQuery('ALTER TABLE abra_pictures ADD CONSTRAINT abra_pictures_ibfk_1 FOREIGN KEY (id_abra_product) REFERENCES abra_products(id_abra)');
        } catch (\Exception $e) {
            $this->addError($e->getMessage());
        }

        $this->upgradeOK();
    }
}
