diff --git a/src/Phpmig/Adapter/AdapterInterface.php b/src/Phpmig/Adapter/AdapterInterface.php index c5fc79e..b7a68e3 100644 --- a/src/Phpmig/Adapter/AdapterInterface.php +++ b/src/Phpmig/Adapter/AdapterInterface.php @@ -59,6 +59,13 @@ public function hasSchema(); * @return AdapterInterface */ public function createSchema(); + + /** + * Is the adapter still connected? + * + * @return bool + */ + public function isConnected(); } diff --git a/src/Phpmig/Adapter/PDO/Sql.php b/src/Phpmig/Adapter/PDO/Sql.php index d3cccd4..628a02b 100644 --- a/src/Phpmig/Adapter/PDO/Sql.php +++ b/src/Phpmig/Adapter/PDO/Sql.php @@ -221,5 +221,25 @@ protected function getQuery($type) return $queries[$type]; } + + /** + * Is the adapter still connected? + * + * @return bool + */ + public function isConnected() + { + try { + $this->connection->query('SELECT 1'); + } catch (\PDOException $pdoE) { + if (stristr($pdoE->getMessage(), 'MySQL server has gone away') !== false) { + return false; + } + throw $e; + } + + return true; + } + } diff --git a/src/Phpmig/Console/Command/AbstractCommand.php b/src/Phpmig/Console/Command/AbstractCommand.php index 646c766..cafd1fe 100644 --- a/src/Phpmig/Console/Command/AbstractCommand.php +++ b/src/Phpmig/Console/Command/AbstractCommand.php @@ -148,7 +148,11 @@ protected function bootstrapAdapter(InputInterface $input) $adapter = $container['phpmig.sets'][$set]['adapter']; } if (isset($container['phpmig.adapter'])) { - $adapter = $container['phpmig.adapter']; + if (is_callable($container['phpmig.adapter'])) { + $adapter = $container['phpmig.adapter'](); + } else { + $adapter = $container['phpmig.adapter']; + } } if (!($adapter instanceof AdapterInterface)) { diff --git a/src/Phpmig/Migration/Migrator.php b/src/Phpmig/Migration/Migrator.php index 1b4cb32..fbd358b 100644 --- a/src/Phpmig/Migration/Migrator.php +++ b/src/Phpmig/Migration/Migrator.php @@ -142,6 +142,11 @@ public function setContainer(\ArrayAccess $container) */ public function getAdapter() { + if (!$this->adapter->isConnected()) { + if (is_callable($this->container['phpmig.adapter'])) { + $this->adapter = $this->container['phpmig.adapter'](); + } + } return $this->adapter; }