<?php

class m130722_092518_add_index_for_event_count extends MyDbMigration
{
	public function up()
	{
		if(!$this->indexExist('validated_user_validatetime', 'storicotable'))
		{
			$command=$this->dbConnection->createCommand("CREATE INDEX validated_user_validatetime ON storicotable (ValidatedBy, id_data_check_user, data_check_time)");
			$command->execute();
		}
		

		if ($this->dbConnection->schema->getTable('violation_codes')->getColumn('has_goods') == null)
			$this->addColumn('violation_codes', 'has_goods', 'boolean');

		$command=$this->dbConnection->createCommand("UPDATE violation_codes SET has_goods=0");
		$command->execute();
		
		$command=$this->dbConnection->createCommand("UPDATE violation_codes SET has_goods=1 where description like '%PERICOL%'");
		$command->execute();
	}

	public function down()
	{
    $this->dropIndex('validated_user_validatetime', 'storicotable');
    
    if ($this->dbConnection->schema->getTable('violation_codes')->getColumn('has_goods') != null)
    	$this->dropColumn('violation_codes', 'has_goods');
    
		return true;
	}

	/*
	// Use safeUp/safeDown to do migration with transaction
	public function safeUp()
	{
	}

	public function safeDown()
	{
	}
	*/
}
