<?php
/*
* @package LabCMS
* @copyright (c) 2006 Alexander Levin aka Loki (loki_angel@mail.ru)
* @support http://labcms.ru
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*/
	$success=true;

	if (Db::i()->selectCell("SELECT module_id FROM ?_modules WHERE module='guest'")) {//проверяем что модуль установлен
		Db::i()->query("ALTER TABLE ?_guest ADD `putdate` INT UNSIGNED NOT NULL DEFAULT 0 AFTER `date`;");
		Db::i()->query("UPDATE ?_guest SET putdate=UNIX_TIMESTAMP(`date`);");
		Db::i()->query("ALTER TABLE ?_guest DROP `date`;");
	}
	
	Db::i()->query("ALTER TABLE ?_users_online ADD `request_time_tmp` INT UNSIGNED NOT NULL DEFAULT 0");
	Db::i()->query("UPDATE ?_users_online SET request_time_tmp=UNIX_TIMESTAMP(request_time)");
	Db::i()->query("ALTER TABLE ?_users_online DROP `request_time`");
	Db::i()->query("ALTER TABLE ?_users_online CHANGE `request_time_tmp` `request_time` INT( 10 ) UNSIGNED DEFAULT NULL");
	
	if ($module_id=Db::i()->selectCell("SELECT module_id FROM ?_modules WHERE module='admin_pages'")) {//проверяем что модуль установлен
		Db::i()->query("ALTER TABLE ?_pages ADD `date_updated_tmp` INT UNSIGNED NOT NULL DEFAULT 0 AFTER `date_updated`;");
		Db::i()->query("UPDATE ?_pages SET date_updated_tmp=UNIX_TIMESTAMP(`date_updated`);");
		Db::i()->query("ALTER TABLE ?_pages DROP `date_updated`;");
		Db::i()->query("ALTER TABLE ?_pages CHANGE `date_updated_tmp` `date_updated` INT( 10 ) UNSIGNED NOT NULL DEFAULT '0'");
		
		//меняем ключи у комментариев к страницам
		Db::i()->query("CREATE TABLE IF NOT EXISTS ?_comments_items_tmp (
		`item_id` int(11) NOT NULL AUTO_INCREMENT,
		`key` int(11) NOT NULL,
		`type` tinyint(4) NOT NULL DEFAULT '1',
		`url` varchar(150) NOT NULL,
		`title` varchar(150) NOT NULL,
		`module_id` int(11) NOT NULL,
		`counter` int(11) NOT NULL DEFAULT '0',
		PRIMARY KEY (`item_id`),
		UNIQUE KEY `module_id` (`module_id`,`key`),
		KEY `item_key` (`key`,`module_id`,`item_id`)
	) ENGINE=MyISAM  DEFAULT CHARSET=utf8;");
		Db::i()->query("INSERT INTO  ?_comments_items_tmp (SELECT item_id, page_id as `key`, `type`, url, title, module_id, counter FROM ?_comments_items ci JOIN ?_pages p ON ci.`key`=p.tree_id WHERE module_id=?)", $module_id);
		Db::i()->query("DELETE FROM ?_comments_items WHERE module_id=?", $module_id);
		Db::i()->query("INSERT INTO  ?_comments_items SELECT * FROM ?_comments_items_tmp");
		Db::i()->query("DROP TABLE ?_comments_items_tmp");
	}
	
	