<?php

/**
 * Created by PhpStorm.
 * User: ondra
 * Date: 30.1.18
 * Time: 15:34.
 */

namespace KupShop\RewriteBundle\Resources\upgrade;

class RewriteUpgrade extends \UpgradeNew
{
    public function check_rewriteTable()
    {
        return $this->checkTableExists('rewrite');
    }

    /** Create rewrite table */
    public function upgrade_rewriteTable()
    {
        sqlQuery('CREATE TABLE IF NOT EXISTS rewrite (
              id INT(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
              url VARCHAR(255) NOT NULL UNIQUE,
              type INT(11) NOT NULL,
              related_id VARCHAR(255) NOT NULL
            )');

        // migrate urls from old_urls
        if (!$this->checkTableExists('old_urls')) {
            sqlQuery('INSERT INTO rewrite (url, type, related_id)
                        SELECT url, type, related_id FROM old_urls');
        }

        $this->upgradeOK();
    }

    public function check_relatedIdLength()
    {
        return $this->checkColumnType('rewrite', 'related_id', 'VARCHAR(255)');
    }

    /** Rewrite 'related_id' to VARCHAR(255) */
    public function upgrade_relatedIdLength()
    {
        sqlQuery('ALTER TABLE rewrite MODIFY related_id VARCHAR(255)');

        $this->upgradeOK();
    }
}
