我们正在使用原则迁移,当迁移包含多个操作且其中一个操作失败时,通常会出现问题.
例如,如果存在迁移添加5个外键并且其中第5个在字段长度不同时失败,则使用字段修复错误并重新生成迁移并不能解决整个问题,而现在出现错误连接4已经存在密钥的事实,并且不允许迁移成功运行.
是否有一种稳定的方式来使用Doctrine迁移而没有提到的明显问题?我们早先使用过.sql文件,实际上并没有好多少,但我很确定使用Doctrine的项目有正确的数据库版本控制方式吗?
基于模型和模式之间的差异生成迁移是很好的,我想进一步保持这种可能性.
谢谢 解决方法: 我解决了这个问题,解决方案并不是那么好,但我想这对其他人有用.我确实使用了CLI我已经完成了文件,每次迁移都会更新数据库中的数字,类似于Timo在回答问题之前的答案,但这仍然不是很有效但值得做.
我接下来做了什么解决了什么,去吧 doctrine / lib / Doctrine / Migration / Builder.PHP第531行.每次迁移都会扩展默认类的定义.由于我使用CLI并且找不到将参数传递到这个地方的方法,我刚刚将Doctrine_Migration_Base替换为下面的另一个类MY_Doctrine_Migration_Base.
如果您不使用CLI,我会说您应该尝试传递选项而不是替换源.
所以下面的类扩展了Doctrine_Migration_Base并覆盖了一堆方法,检查是否可以进行更改,然后调用父方法来完成它们.它没有涵盖当前的所有方法,只是我写这篇文章时遇到的方法.
现在,每个迁移Doctrine创建都扩展了我的课程,旨在防止我最初提到的问题.
<?PHP
class MY_Doctrine_Migration_Base extends Doctrine_Migration_Base {
public function __construct() {
$this->connection = Doctrine_Manager::getInstance()->getCurrentConnection();
}
public function addIndex($tableName, $indexName, array $definition) {
foreach ($this->connection->execute("SHOW INDEXES IN $tableName")->fetchAll(PDO::FETCH_ASSOC) as $index) {
if ($index['Key_name'] === $indexName.'_idx') {
echo "Index $indexName already exists in table $tableName. Skippingn";
return;
}
}
parent::addIndex($tableName, $indexName, $definition);
}
public function removeColumn($tableName, $columnName) {
if ($this->column_exists($tableName, $columnName)) {
parent::removeColumn($tableName, $columnName);
} else {
echo "Column $columnName doesn't exist in $tableName. Can't dropn";
}
}
public function createTable($tableName, array $fields = array(), array $options = array()) {
if ($this->connection->execute("SHOW TABLES LIKE '$tableName'")->fetchAll(PDO::FETCH_ASSOC)) {
echo "Table $tableName already exists. Can't createn";
} else {
parent::createTable($tableName, $fields, $options);
}
}
public function addColumn($tableName, $columnName, $type, $length = null, array $options = array()) {
if (! $this->column_exists($tableName, $columnName)) {
parent::addColumn($tableName, $columnName, $type, $length, $options);
} else {
echo "Column $columnName already exists in $tableName. Can't addn";
}
}
private function column_exists($tableName, $columnName) {
$exception = FALSE;
try { //parsing information_schema sucks because security will hurt too bad if we have access to it. This lame shit is still better
$this->connection->execute("SELECT $columnName FROM $tableName")->fetchAll(PDO::FETCH_ASSOC);
} catch (Exception $exception) {}
//if someone knows how to check for column existence without exceptions AND WITHOUT INFORMATION SCHEMA please rewrite this stuff
return $exception === FALSE;
}
}
关于如何改进这一建议的建议值得欢迎. (编辑:北几岛)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|