Last active
January 1, 2016 18:19
-
-
Save tankist/8183350 to your computer and use it in GitHub Desktop.
Proposed migrations fix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Phalcon\Db\Column; | |
use Phalcon\Db\Index; | |
use Phalcon\Mvc\Model\Migration; | |
class AdsMigration_1010 extends Migration | |
{ | |
const TABLE_NAME = 'ads'; | |
public function up() | |
{ | |
/** @var \Phalcon\Db\Adapter\Pdo\Mysql $db */ | |
$db = self::$_connection; | |
$columns = $db->describeColumns(self::TABLE_NAME); | |
$columnNames = array_map(function (Column $column) { | |
return $column->getName(); | |
}, $columns); | |
$id = new Column('id', [ | |
'type' => Column::TYPE_INTEGER, | |
'notNull' => true, | |
'autoIncrement' => true, | |
'size' => 11, | |
'first' => true | |
]); | |
if (array_key_exists($id->getName(), $columnNames)) { | |
$db->modifyColumn(self::TABLE_NAME, null, $id); | |
} else { | |
$db->addColumn(self::TABLE_NAME, null, $id); | |
} | |
$publisherId = new Column('publisher_id', [ | |
'type' => Column::TYPE_INTEGER, | |
'size' => 11, | |
'after' => 'status' | |
]); | |
if (array_key_exists($publisherId->getName(), $columnNames)) { | |
$db->modifyColumn(self::TABLE_NAME, null, $publisherId); | |
} else { | |
$db->addColumn(self::TABLE_NAME, null, $publisherId); | |
} | |
$lastCheckDate = new Column('last_check_date', [ | |
'type' => Column::TYPE_DATETIME, | |
'after' => 'publisher_id', | |
]); | |
if (array_key_exists($lastCheckDate->getName(), $columnNames)) { | |
$db->modifyColumn(self::TABLE_NAME, null, $lastCheckDate); | |
} else { | |
$db->addColumn(self::TABLE_NAME, null, $lastCheckDate); | |
} | |
$indexes = $db->describeIndexes(self::TABLE_NAME); | |
$indexNames = array_map(function (Index $index) { | |
return $index->getName(); | |
}, $indexes); | |
if (array_key_exists('last_check_date', $indexNames)) { | |
$db->dropIndex(self::TABLE_NAME, null, 'last_check_date'); | |
} | |
$db->addIndex(self::TABLE_NAME, null, new Index('last_check_date', ['last_check_date'])); | |
} | |
public function down() | |
{ | |
/** @var \Phalcon\Db\Adapter\Pdo\Mysql $db */ | |
$db = self::$_connection; | |
$id = new Column('id', [ | |
'type' => Column::TYPE_INTEGER, | |
'notNull' => true, | |
'autoIncrement' => true, | |
'size' => 4, | |
'first' => true | |
]); | |
$db->modifyColumn(self::TABLE_NAME, null, $id); | |
$publisherId = new Column('publisher_id', [ | |
'type' => Column::TYPE_INTEGER, | |
'size' => 4, | |
'after' => 'status' | |
]); | |
$db->modifyColumn(self::TABLE_NAME, null, $publisherId); | |
$db->dropIndex(self::TABLE_NAME, null, 'last_check_date'); | |
$db->dropColumn(self::TABLE_NAME, null, 'last_check_date'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment