<?php

namespace KupShop\GlobalDiscountsBundle\Resources\upgrade;

class GlobalDiscountsUpgrade extends \UpgradeNew
{
    public function check_tableExists()
    {
        return $this->checkTableExists('global_discounts');
    }

    /** Create table 'global_discounts' */
    public function upgrade_tableExists()
    {
        sqlQuery('CREATE TABLE IF NOT EXISTS global_discounts (
                id INT AUTO_INCREMENT NOT NULL, 
                position INT DEFAULT NULL,
                name VARCHAR(60) NOT NULL, 
                discount DECIMAL(15,4) DEFAULT 0,
                filter MEDIUMTEXT DEFAULT NULL,
                date_from DATETIME NOT NULL,
                date_to DATETIME NOT NULL,
                active TINYINT DEFAULT 0,
                PRIMARY KEY(id)
            ) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci ENGINE = InnoDB;
        ');

        $this->upgradeOK();
    }

    public function check_DateNullable()
    {
        return $this->checkColumnIsNull('global_discounts', 'date_from', true);
    }

    /** 'global_discounts': enable null values in date columns */
    public function upgrade_DateNullable()
    {
        sqlQuery('ALTER TABLE global_discounts MODIFY date_from DATETIME DEFAULT NULL');
        sqlQuery('ALTER TABLE global_discounts MODIFY date_to DATETIME DEFAULT NULL');

        $this->upgradeOK();
    }
}
