Skip to content

Instantly share code, notes, and snippets.

@yukikaoru
Created February 21, 2011 12:40
Show Gist options
  • Save yukikaoru/837006 to your computer and use it in GitHub Desktop.
Save yukikaoru/837006 to your computer and use it in GitHub Desktop.
Adapt to multiple connection to Doctrine_Migration.
--- a/lib/vendor/symfony/lib/plugins/sfDoctrinePlugin/lib/vendor/doctrine/Doctrine/Migration.php
+++ b/lib/vendor/symfony/lib/plugins/sfDoctrinePlugin/lib/vendor/doctrine/Doctrine/Migration.php
@@ -225,10 +225,16 @@ class Doctrine_Migration
*/
public function setCurrentVersion($number)
{
- if ($this->hasMigrated()) {
- $this->_connection->exec("UPDATE " . $this->_migrationTableName . " SET version = $number");
- } else {
- $this->_connection->exec("INSERT INTO " . $this->_migrationTableName . " (version) VALUES ($number)");
+ foreach (Doctrine_Manager::getInstance()->getConnections() as $conn) {
+ try {
+ $this->_connection = $conn;
+ if ($this->hasMigrated()) {
+ $conn->exec("UPDATE " . $this->_migrationTableName . " SET version = $number");
+ } else {
+ $conn->exec("INSERT INTO " . $this->_migrationTableName . " (version) VALUES ($number)");
+ }
+ } catch (Exception $e) {
+ }
}
}
@@ -241,9 +247,17 @@ class Doctrine_Migration
{
$this->_createMigrationTable();
- $result = $this->_connection->fetchColumn("SELECT version FROM " . $this->_migrationTableName);
+ foreach (Doctrine_Manager::getInstance()->getConnections() as $conn) {
+ try {
+ $result = $conn->fetchColumn("SELECT version FROM " . $this->_migrationTableName);
+ if (isset($result[0])) {
+ return $result[0];
+ }
+ } catch (Exception $e) {
+ }
+ }
- return isset($result[0]) ? $result[0]:0;
+ return 0;
}
/**
@@ -255,9 +269,17 @@ class Doctrine_Migration
{
$this->_createMigrationTable();
- $result = $this->_connection->fetchColumn("SELECT version FROM " . $this->_migrationTableName);
+ foreach (Doctrine_Manager::getInstance()->getConnections() as $conn) {
+ try {
+ $result = $conn->fetchColumn("SELECT version FROM " . $this->_migrationTableName);
+ if (isset($result[0])) {
+ return true;
+ }
+ } catch (Exception $e) {
+ }
+ }
- return isset($result[0]) ? true:false;
+ return false;
}
/**
@@ -316,7 +338,9 @@ class Doctrine_Migration
$this->_createMigrationTable();
- $this->_connection->beginTransaction();
+ foreach (Doctrine_Manager::getInstance()->getConnections() as $conn) {
+ $conn->beginTransaction();
+ }
try {
// If nothing specified then lets assume we are migrating from
@@ -331,7 +355,9 @@ class Doctrine_Migration
}
if ($this->hasErrors()) {
- $this->_connection->rollback();
+ foreach (Doctrine_Manager::getInstance()->getConnections() as $conn) {
+ $conn->rollback();
+ }
if ($dryRun) {
return false;
@@ -340,14 +366,18 @@ class Doctrine_Migration
}
} else {
if ($dryRun) {
- $this->_connection->rollback();
+ foreach (Doctrine_Manager::getInstance()->getConnections() as $conn) {
+ $conn->rollback();
+ }
if ($this->hasErrors()) {
return false;
} else {
return $to;
}
} else {
- $this->_connection->commit();
+ foreach (Doctrine_Manager::getInstance()->getConnections() as $conn) {
+ $conn->commit();
+ }
$this->setCurrentVersion($to);
return $to;
}
@@ -500,6 +530,9 @@ class Doctrine_Migration
try {
$migration = $this->getMigrationClass($num);
+ $conn = $this->getConnection();
+ $this->setConnection($migration->getConnection());
+
$method = 'pre' . $direction;
$migration->$method();
@@ -531,6 +564,8 @@ class Doctrine_Migration
$method = 'post' . $direction;
$migration->$method();
+
+ $this->setConnection($conn);
} catch (Exception $e) {
$this->addError($e);
}
@@ -551,12 +586,13 @@ class Doctrine_Migration
$this->_migrationTableCreated = true;
- try {
- $this->_connection->export->createTable($this->_migrationTableName, array('version' => array('type' => 'integer', 'size' => 11)));
-
- return true;
- } catch(Exception $e) {
- return false;
+ foreach (Doctrine_Manager::getInstance()->getConnections() as $conn) {
+ try {
+ $conn->export->createTable($this->_migrationTableName, array('version' => array('type' => 'integer', 'size' => 11)));
+ } catch (Exception $e) {
+ }
}
+
+ return true;
}
}
--- a/lib/vendor/symfony/lib/plugins/sfDoctrinePlugin/lib/vendor/doctrine/Doctrine/Migration/Base.php
+++ b/lib/vendor/symfony/lib/plugins/sfDoctrinePlugin/lib/vendor/doctrine/Doctrine/Migration/Base.php
@@ -502,4 +502,20 @@ abstract class Doctrine_Migration_Base
public function postDown()
{
}
+
+ /**
+ * @return Doctrine_Connection
+ */
+ public function getConnection()
+ {
+ return Doctrine_Manager::getInstance()->getConnection($this->getConnectionName());
+ }
+
+ /**
+ * @return string
+ */
+ public function getConnectionName()
+ {
+ return Doctrine_Manager::getCurrentConnection()->getName();
+ }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment