<?php

if (!defined('BASEPATH')) {
    exit('No direct script access allowed');
}

/**
 * Upgrade Post module to version 1.0.0
 *
 * @author CIEXT skeleton
 */
class Post_upgrade_0 extends \Ciext\Migration\AbstractUpgrade
{

    public function up()
    {
        if (!$this->is_table_exist('post_category_translate')) {
            $ci = &get_instance();
            $ci->db->query('CREATE TABLE IF NOT EXISTS `post_category_translate` (
                `id_post_category_translate` int(11) NOT NULL AUTO_INCREMENT,
                `id_post_category` int(11) NOT NULL,
                `id_lan` tinyint(1) NOT NULL,
                `title` varchar(1024),
                PRIMARY KEY (`id_post_category_translate`)
            ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;');

            $ci->db->query('CREATE TABLE IF NOT EXISTS `post_translate` (
                `id_post_translate` int(11) NOT NULL AUTO_INCREMENT,
                `id_post` int(11) NOT NULL,
                `id_lan` tinyint(1) NOT NULL,
                `title` varchar(1024) CHARACTER SET utf8,
                `short_desc` text,
                `content` text,
                PRIMARY KEY (`id_post_translate`)
            ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;');

            $ci->db->query('INSERT INTO `post_category_translate` (`id_post_category`, `id_lan`, `title`) SELECT `id_post_category`, "1" as id_lan, `title` FROM post_category ;');
            $ci->db->query('INSERT INTO `post_translate` (`id_post`, `id_lan`, `title`, `short_desc`, `content`) SELECT `id_post`, "1" as id_lan, `title`, `short_desc`, `content` FROM post ;');

            $ci->db->query('ALTER TABLE `post_category` DROP `title`;');
            $ci->db->query('ALTER TABLE `post` DROP `title`;');
            $ci->db->query('ALTER TABLE `post` DROP `short_desc`;');
            $ci->db->query('ALTER TABLE `post` DROP `content`;');
        }
        
        return '1.0.0';
    }
    
    public function down()
    {
        return '0';
    }

}
