<?php

class m150120_143517_update_storicotable_velocit_field extends MyDbMigration
{
	public function up()
	{
		if ($this->dbConnection->schema->getTable('storicotable')->getColumn('Velocit') != null)
		{
			echo "rename column";
			
			if($this->dbConnection->schema->getTable('storicotable_temp'))
				$this->dropTable('storicotable_temp');
		
			$this->execute("
			CREATE TABLE  `storicotable_temp` (
			`ID` bigint(20) NOT NULL auto_increment,
			`UserID` bigint(20) default NULL,
			`EventType` char(250) default NULL,
			`EventMode` char(250) default NULL,
			`ModifiedBy` char(250) default NULL,
			`EventValid` char(250) default NULL,
			`ValidatedBy` char(250) default NULL,
			`ValidationTime` datetime default NULL,
			`Protocol` char(250) default NULL,
			`NextID` bigint(20) default NULL,
			`PreviousID` bigint(20) default NULL,
			`TicketID` bigint(20) default NULL,
			`Ticket` char(250) default NULL,
			`AreaID` bigint(20) default NULL,
			`PositionID` bigint(20) default NULL,
			`Direction` bigint(20) default NULL,
			`Speed` double default NULL,
			`Classe` char(250) default NULL,
			`Height` double default NULL,
			`Width` double default NULL,
			`Length` double default NULL,
			`LaneID` bigint(20) default NULL,
			`IsBoxComputed` bigint(20) default NULL,
			`Gate` bigint(20) default NULL,
			`StartTime` datetime default NULL,
			`EndTime` datetime default NULL,
			`Plate` char(250) default NULL,
			`ExtPlate` char(250) default NULL,
			`Nationality` char(250) default NULL,
			`NetworkID` bigint(20) default NULL,
			`Image` mediumblob,
			`ImageLength` bigint(20) default NULL,
			`Comments` char(250) default NULL,
			`ExtraData` mediumblob,
			`ExtraDataLength` bigint(20) default NULL,
			`Signature` mediumblob,
			`SignatureLength` bigint(20) default NULL,
			`UUID` char(250) default NULL,
			`ExtraInfoID` bigint(20) default NULL,
			`MatchMeasure` double default NULL,
			`epoch` datetime default NULL,
			`GateDescription` char(250) default NULL,
			`TransitComments` char(250) default NULL,
			`GateType` char(10) default NULL,
			`Kemler` char(250) default NULL,
			`PlateCheck` bigint(20) default NULL,
			`BlobFileName` char(250) default NULL,
			`Color` char(250) default NULL,
			`data_check` tinyint(4) default NULL,
			`id_run` bigint(20) default NULL,
			`id_vehicle_color` bigint(20) default NULL,
			`data_check_time` datetime default NULL,
			`id_data_check_user` bigint(20) default NULL,
			`id_validate_user` bigint(20) default NULL,
			`validation_result` char(250) default NULL,
			`ext_code` char(250) default NULL,
			`speed_limit` bigint(20) default NULL,
			`custom_plate_type` char(250) default NULL,
			`custom_property_type` char(250) default NULL,
			`custom_vehicle_type` varchar(250) default NULL,
			`custom_vehicle_property` varchar(255) default NULL,
			`ext_code_time` datetime default NULL,
			`speed_limit_signs` char(250) default NULL,
			`truck_bar_type` char(250) default NULL,
			`custom_nationality` varchar(255) default NULL,
			`measured_speed` float default NULL,
			PRIMARY KEY  (`ID`),
			KEY `index_order_starttime` (`StartTime`),
			KEY `index_order_plate` (`Plate`),
			KEY `index_order_protocol` (`Protocol`),
			KEY `index_order_validatedby` (`ValidatedBy`),
			KEY `index_order_gate` (`Gate`),
			KEY `index_order_uuid` (`UUID`),
			KEY `index_order_epoch` (`epoch`),
			KEY `index_order_ticketid` (`TicketID`),
			KEY `index_order_areaid` (`AreaID`),
			KEY `validated_user_validatetime` (`ValidatedBy`,`id_data_check_user`,`data_check_time`),
			KEY `validate_approved_no_extcode` (`ValidatedBy`,`validation_result`,`ext_code`,`id_validate_user`,`ValidationTime`),
			KEY `Index_starttime_validatedby_gate_id` (`StartTime`,`ValidatedBy`,`Gate`,`ID`),
			KEY `Index_for_cedolino` (`data_check_time`,`id_data_check_user`,`ValidatedBy`,`ID`),
			KEY `Index_for_report` (`ValidationTime`,`id_validate_user`,`validation_result`,`ValidatedBy`,`ID`),
			KEY `Index_for_pending` (`validation_result`,`ValidatedBy`,`ID`),
			KEY `Index_idrun_Validatedby_starttime_id` (`id_run`,`ValidatedBy`,`StartTime`,`ID`),
			FULLTEXT KEY `plate_fulltext_index` (`Plate`),
			FULLTEXT KEY `Index_kemler_fulltext` (`Kemler`)
			) ENGINE=MyISAM AUTO_INCREMENT=2511 DEFAULT CHARSET=latin1 MAX_ROWS=1000000000;			
			");
			
			$this->execute("
					insert into storicotable_temp
					(ID,
					UserID,
					EventType,
					EventMode,
					ModifiedBy,
					EventValid,
					ValidatedBy,
					ValidationTime,
					Protocol,
					NextID,
					PreviousID,
					TicketID,
					Ticket,
					AreaID,
					PositionID,
					Direction,
					Classe,
					Gate,
					StartTime,
					EndTime,
					Plate,
					ExtPlate,
					Nationality,
					Image,
					ImageLength,
					Comments,
					ExtraData,
					ExtraDataLength,
					Signature,
					SignatureLength,
					UUID,
					ExtraInfoID,
					MatchMeasure,
					epoch,
					GateDescription,
					Speed,
					TransitComments,
					GateType,
					Kemler,
					PlateCheck,
					BlobFileName,
					measured_speed,
					Height,
					Width,
					Length,
					LaneID,
					IsBoxComputed,
					NetworkID,
					Color,
					data_check,
					id_run,
					id_vehicle_color,
					data_check_time,
					id_data_check_user,
					id_validate_user,
					validation_result,
					ext_code,
					speed_limit,
					custom_plate_type,
					custom_property_type,
					custom_vehicle_type,
					ext_code_time,
					speed_limit_signs,
					truck_bar_type,
					custom_nationality,
					custom_vehicle_property)
					SELECT
					ID,
					UserID,
					EventType,
					EventMode,
					ModifiedBy,
					EventValid,
					ValidatedBy,
					ValidationTime,
					Protocol,
					NextID,
					PreviousID,
					TicketID,
					Ticket,
					AreaID,
					PositionID,
					Direction,
					Classe,
					Gate,
					StartTime,
					EndTime,
					Plate,
					ExtPlate,
					Nationality,
					Image,
					ImageLength,
					Comments,
					ExtraData,
					ExtraDataLength,
					Signature,
					SignatureLength,
					UUID,
					ExtraInfoID,
					MatchMeasure,
					epoch,
					GateDescription,
					Speed,
					TransitComments,
					GateType,
					Kemler,
					PlateCheck,
					BlobFileName,
					measured_speed,
					Height,
					Width,
					Length,
					LaneID,
					IsBoxComputed,
					NetworkID,
					Color,
					data_check,
					id_run,
					id_vehicle_color,
					data_check_time,
					id_data_check_user,
					id_validate_user,
					validation_result,
					ext_code,
					speed_limit,
					custom_plate_type,
					custom_property_type,
					custom_vehicle_type,
					ext_code_time,
					speed_limit_signs,
					truck_bar_type,
					custom_nationality,
					custom_vehicle_property
					FROM storicotable");
			
			$this->dropTable('storicotable');
			$this->renameTable('storicotable_temp', 'storicotable');
		}
	}

	public function down()
	{
		echo "m150120_143517_update_storicotable_velocit_field does not support migration down.\n";
		return true;
	}

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

	public function safeDown()
	{
	}
	*/
}