<?php

class m130708_145446_plate_list_management extends MyDbMigration
{
  public function up()
  {
    if($this->dbConnection->schema->getTable('vehicletable') == null)
    {
       
      $this->createTable('plate_list',
          array('id' 									 => 'pk',
              'name' 									 => 'varchar(255)',
              'source'   							 => 'varchar(255)',
              'code'      						 => 'int',
              'auto_import'					   => 'boolean',
              'import_folder'					 => 'varchar(255)',
              'epoch'      						 => 'datetime',

          ),
          'ENGINE=MyISAM'
      );
    }
    // vehicle
    if ($this->dbConnection->schema->getTable('vehicletable')->getColumn('source') == null)
      $this->addColumn('vehicletable', 'source', 'varchar(255)');

    if ($this->dbConnection->schema->getTable('vehicletable')->getColumn('privacy_level') == null)
      $this->addColumn('vehicletable', 'privacy_level', 'int');

    if ($this->dbConnection->schema->getTable('vehicletable')->getColumn('action') == null)
      $this->addColumn('vehicletable', 'action', 'text');

    if ($this->dbConnection->schema->getTable('vehicletable')->getColumn('list_code') == null)
      $this->addColumn('vehicletable', 'list_code', 'int');

    if ($this->dbConnection->schema->getTable('vehicletable')->getColumn('type') == null)
      $this->addColumn('vehicletable', 'type', 'varchar(255)');

    if ($this->dbConnection->schema->getTable('vehicletable')->getColumn('category') == null)
      $this->addColumn('vehicletable', 'category', 'varchar(255)');

    if ($this->dbConnection->schema->getTable('vehicletable')->getColumn('note') == null)
      $this->addColumn('vehicletable', 'note', 'text');

    // abbonamento
    if ($this->dbConnection->schema->getTable('abbonamentotable')->getColumn('list_code') == null)
      $this->addColumn('abbonamentotable', 'list_code', 'int');

    // user-vehicle_owner
    if ($this->dbConnection->schema->getTable('usertable')->getColumn('list_code') == null)
      $this->addColumn('usertable', 'list_code', 'int');

    if ($this->dbConnection->schema->getTable('whitelisttable')->getColumn('list_code') == null)
      $this->addColumn('whitelisttable', 'list_code', 'int');

  }

  public function down()
  {
    if($this->dbConnection->schema->getTable('vehicletable') != null)
      $this->dropTable('plate_list');

    // vehicle
    if ($this->dbConnection->schema->getTable('vehicletable')->getColumn('source') != null)
      $this->dropColumn('vehicletable', 'source');

    if ($this->dbConnection->schema->getTable('vehicletable')->getColumn('privacy_level') != null)
      $this->dropColumn('vehicletable', 'privacy_level');

    if ($this->dbConnection->schema->getTable('vehicletable')->getColumn('action') != null)
      $this->dropColumn('vehicletable', 'action');

    if ($this->dbConnection->schema->getTable('vehicletable')->getColumn('list_code') != null)
      $this->dropColumn('vehicletable', 'list_code');

    if ($this->dbConnection->schema->getTable('vehicletable')->getColumn('type') != null)
      $this->dropColumn('vehicletable', 'type');

    if ($this->dbConnection->schema->getTable('vehicletable')->getColumn('category') != null)
      $this->dropColumn('vehicletable', 'category');

    if ($this->dbConnection->schema->getTable('vehicletable')->getColumn('note') != null)
      $this->dropColumn('vehicletable', 'note');

    // abbonamento
    if ($this->dbConnection->schema->getTable('abbonamentotable')->getColumn('list_code') != null)
      $this->dropColumn('abbonamentotable', 'list_code');

    // user-vehicle_owner
    if ($this->dbConnection->schema->getTable('usertable')->getColumn('list_code') != null)
      $this->dropColumn('usertable', 'list_code');

    // user-vehicle_owner
    if ($this->dbConnection->schema->getTable('whitelisttable')->getColumn('list_code') != null)
      $this->dropColumn('whitelisttable', 'list_code');

    return true;
  }

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

  public function safeDown()
  {
  }
  */
}