<?php

class m151105_171146_user_tracking_table_data_column_changed_encoding extends MyDbMigration
{

  public function up()
  {
    echo ("Changing data from OLD format (exported to eval) to NEW (serialized)" . PHP_EOL);
    
    $reader = $this->select ( "SELECT * FROM user_tracking_table" );
    foreach ( $reader as $row )
    {
      $ut = new UserTrackingTable ();
      $ut->isNewRecord = false;
      $ut->setAttributes ( $row, false );
      
      $lengthBefore = strlen ( $ut->data );
      
      $value = $ut->DataValueOld;
      $ut->DataValue = $value;
      
      $lengthAfter = strlen ( $ut->data );
      
      echo ("ROW {$ut->id} FROM $lengthBefore TO $lengthAfter , SAVING..." . PHP_EOL);
      
      if ($ut->updateChanged ())
        echo ("DONE" . PHP_EOL);
      else
        echo ("ERROR" . PHP_EOL);
    }
    
    echo ("FINISHED" . PHP_EOL);
    
    return true;
  }

  public function down()
  {
    echo ("Changing data from NEW format (serialized) to OLD (exported to eval)" . PHP_EOL);
    
    $reader = $this->select ( "SELECT * FROM user_tracking_table" );
    foreach ( $reader as $row )
    {
      $ut = new UserTrackingTable ();
      $ut->isNewRecord = false;
      $ut->setAttributes ( $row, false );
      
      $lengthBefore = strlen ( $ut->data );
      
      $value = $ut->DataValue;
      $ut->DataValueOld = $value;
      
      $lengthAfter = strlen ( $ut->data );
      
      echo ("ROW {$ut->id} FROM $lengthBefore TO $lengthAfter , SAVING..." . PHP_EOL);
      
      if ($ut->updateChanged ())
        echo ("DONE" . PHP_EOL);
      else
        echo ("ERROR" . PHP_EOL);
    }
    
    echo ("FINISHED" . PHP_EOL);
    
    return true;
  }
}
    