Fixed migrations

This commit is contained in:
Dmitry Khomutov 2016-05-05 22:33:12 +06:00
parent ca2f5ed197
commit ff36c2504f
7 changed files with 56 additions and 68 deletions

View file

@ -10,8 +10,12 @@ class ChangeBuildKeysMigration extends AbstractMigration
public function up() public function up()
{ {
$project = $this->table('project'); $project = $this->table('project');
$project->renameColumn('git_key', 'ssh_private_key'); if (!$project->hasColumn('ssh_private_key') && $project->hasColumn('git_key')) {
$project->renameColumn('public_key', 'ssh_public_key'); $project->renameColumn('git_key', 'ssh_private_key');
}
if (!$project->hasColumn('ssh_public_key') && $project->hasColumn('public_key')) {
$project->renameColumn('public_key', 'ssh_public_key');
}
} }
/** /**
@ -20,7 +24,11 @@ class ChangeBuildKeysMigration extends AbstractMigration
public function down() public function down()
{ {
$project = $this->table('project'); $project = $this->table('project');
$project->renameColumn('ssh_private_key', 'git_key'); if (!$project->hasColumn('git_key') && $project->hasColumn('ssh_private_key')) {
$project->renameColumn('ssh_public_key', 'public_key'); $project->renameColumn('ssh_private_key', 'git_key');
}
if (!$project->hasColumn('public_key') && $project->hasColumn('ssh_public_key')) {
$project->renameColumn('ssh_public_key', 'public_key');
}
} }
} }

View file

@ -23,10 +23,9 @@ class ChooseBranch extends AbstractMigration
public function up() public function up()
{ {
$project = $this->table('project'); $project = $this->table('project');
$project->addColumn('branch', 'string', [ if (!$project->hasColumn('branch')) {
'after' => 'reference', $project->addColumn('branch', 'string', ['after' => 'reference', 'limit' => 250])->save();
'limit' => 250 }
])->save();
} }
/** /**
@ -35,6 +34,8 @@ class ChooseBranch extends AbstractMigration
public function down() public function down()
{ {
$project = $this->table('project'); $project = $this->table('project');
$project->removeColumn('branch')->save(); if ($project->hasColumn('branch')) {
$project->removeColumn('branch')->save();
}
} }
} }

View file

@ -10,8 +10,9 @@ class ArchiveProject extends AbstractMigration
public function up() public function up()
{ {
$project = $this->table('project'); $project = $this->table('project');
$project->addColumn('archived', 'boolean', ['default' => 0]); if (!$project->hasColumn('archived')) {
$project->save(); $project->addColumn('archived', 'boolean', ['default' => 0])->save();
}
} }
/** /**
@ -20,7 +21,8 @@ class ArchiveProject extends AbstractMigration
public function down() public function down()
{ {
$project = $this->table('project'); $project = $this->table('project');
$project->removeColumn('archived'); if ($project->hasColumn('archived')) {
$project->save(); $project->removeColumn('archived')->save();
}
} }
} }

View file

@ -12,16 +12,10 @@ class FixColumnTypes extends AbstractMigration
{ {
// Update the build log column to MEDIUMTEXT: // Update the build log column to MEDIUMTEXT:
$build = $this->table('build'); $build = $this->table('build');
$build->changeColumn('log', 'text', [ $build->changeColumn('log', 'text', ['null' => true, 'limit' => MysqlAdapter::TEXT_MEDIUM]);
'null' => true,
'limit' => MysqlAdapter::TEXT_MEDIUM,
]);
// Update the build meta value column to MEDIUMTEXT: // Update the build meta value column to MEDIUMTEXT:
$buildMeta = $this->table('build_meta'); $buildMeta = $this->table('build_meta');
$buildMeta->changeColumn('meta_value', 'text', [ $buildMeta->changeColumn('meta_value', 'text', ['null' => false, 'limit' => MysqlAdapter::TEXT_MEDIUM]);
'null' => false,
'limit' => MysqlAdapter::TEXT_MEDIUM,
]);
} }
} }

View file

@ -10,10 +10,12 @@ class UniqueEmailAndNameUserFields extends AbstractMigration
public function up() public function up()
{ {
$user_table = $this->table('user'); $user_table = $this->table('user');
$user_table if (!$user_table->hasIndex('email', ['unique' => true])) {
->addIndex('email', ['unique' => true]) $user_table->addIndex('email', ['unique' => true])->save();
->addIndex('name', ['unique' => true]) }
->save(); if (!$user_table->hasIndex('name', ['unique' => true])) {
$user_table->addIndex('name', ['unique' => true])->save();
}
} }
/** /**
@ -22,9 +24,11 @@ class UniqueEmailAndNameUserFields extends AbstractMigration
public function down() public function down()
{ {
$user_table = $this->table('user'); $user_table = $this->table('user');
$user_table if ($user_table->hasIndex('email', ['unique' => true])) {
->removeIndex('email', ['unique' => true]) $user_table->removeIndex('email', ['unique' => true])->save();
->removeIndex('name', ['unique' => true]) }
->save(); if ($user_table->hasIndex('name', ['unique' => true])) {
$user_table->removeIndex('name', ['unique' => true])->save();
}
} }
} }

View file

@ -7,23 +7,23 @@ class AddProjectGroups extends AbstractMigration
public function change() public function change()
{ {
$table = $this->table('project_group'); $table = $this->table('project_group');
$table->addColumn('title', 'string', ['limit' => 100, 'null' => false]); if (!$table->hasColumn('title')) {
$table->save(); $table->addColumn('title', 'string', ['limit' => 100, 'null' => false])->save();
$group = new \PHPCI\Model\ProjectGroup(); $group = new \PHPCI\Model\ProjectGroup();
$group->setTitle('Projects'); $group->setTitle('Projects');
/** @var \PHPCI\Model\ProjectGroup $group */ \b8\Store\Factory::getStore('ProjectGroup')->save($group);
$group = \b8\Store\Factory::getStore('ProjectGroup')->save($group); }
$table = $this->table('project'); $table = $this->table('project');
$table->addColumn('group_id', 'integer', [ if (!$table->hasColumn('group_id')) {
'signed' => true, $table->addColumn('group_id', 'integer', [
'null' => false, 'signed' => true,
'default' => $group->getId(), 'null' => false,
]); 'default' => 1,
]);
$table->addForeignKey('group_id', 'project_group', 'id', ['delete'=> 'RESTRICT', 'update' => 'CASCADE']); $table->addForeignKey('group_id', 'project_group', 'id', ['delete'=> 'RESTRICT', 'update' => 'CASCADE'])->save();
$table->save(); }
} }
} }

View file

@ -4,37 +4,16 @@ use Phinx\Migration\AbstractMigration;
class RemoveUniqueNameIndex extends AbstractMigration class RemoveUniqueNameIndex extends AbstractMigration
{ {
/**
* Change Method.
*
* Write your reversible migrations using this method.
*
* More information on writing migrations is available here:
* http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
*
* The following commands can be used in this method and Phinx will
* automatically reverse them when rolling back:
*
* createTable
* renameTable
* addColumn
* renameColumn
* addIndex
* addForeignKey
*
* Remember to call "create()" or "update()" and NOT "save()" when working
* with the Table class.
*/
public function change() public function change()
{ {
$user = $this->table('user'); $user = $this->table('user');
if ($user->hasIndex('name', ['unique' => true])) { if ($user->hasIndex('name', ['unique' => true])) {
$user->removeIndex('name', ['unique' => true]); $user->removeIndex('name', ['unique' => true])->save();
$user->save();
} }
$user->addIndex('name', ['unique' => false]); if (!$user->hasIndex('name', ['unique' => true])) {
$user->save(); $user->addIndex('name', ['unique' => false])->save();
}
} }
} }