diff --git a/Command/AbstractCommand.php b/Command/AbstractCommand.php index 7bb43dc..3df2948 100644 --- a/Command/AbstractCommand.php +++ b/Command/AbstractCommand.php @@ -384,10 +384,10 @@ abstract class AbstractCommand extends ContainerAwareCommand */ protected function parseDbName($dsn) { - preg_match('#dbname=([a-zA-Z0-9\_]+)#', $dsn, $matches); + preg_match('#(dbname|Database)=([a-zA-Z0-9\_]+)#', $dsn, $matches); - if (isset($matches[1])) { - return $matches[1]; + if (isset($matches[2])) { + return $matches[2]; } // e.g. SQLite diff --git a/Command/DatabaseCreateCommand.php b/Command/DatabaseCreateCommand.php index 6c9f2af..554c533 100644 --- a/Command/DatabaseCreateCommand.php +++ b/Command/DatabaseCreateCommand.php @@ -83,7 +83,7 @@ class DatabaseCreateCommand extends AbstractCommand $dbName = $this->parseDbName($config['dsn']); $config['dsn'] = preg_replace( - '#;?dbname='.$dbName.'#', + '#;?(dbname|Database)='.$dbName.'#', '', $config['dsn'] ); diff --git a/Command/DatabaseDropCommand.php b/Command/DatabaseDropCommand.php index bf17075..7e0a91c 100644 --- a/Command/DatabaseDropCommand.php +++ b/Command/DatabaseDropCommand.php @@ -14,6 +14,7 @@ use Propel\Runtime\Propel; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; +use Propel\Runtime\Connection\ConnectionManagerSingle; /** * DatabaseDropCommand class. @@ -79,9 +80,41 @@ EOT $query = 'DROP DATABASE '. $dbName .';'; } + + $manager = new ConnectionManagerSingle(); + $manager->setConfiguration($this->getTemporaryConfiguration($config)); + + $serviceContainer = Propel::getServiceContainer(); + $serviceContainer->setAdapterClass($connectionName, $config['adapter']); + $serviceContainer->setConnectionManager($connectionName, $manager); + + $connection = Propel::getConnection($connectionName); + $statement = $connection->prepare($query); $statement->execute(); $output->writeln(sprintf('Database %s has been dropped.', $dbName)); } + + /** + * Create a temporary configuration to connect to the database in order + * to create a given database. This idea comes from Doctrine1. + * + * @see https://github.com/doctrine/doctrine1/blob/master/lib/Doctrine/Connection.php#L1491 + * + * @param array $config A Propel connection configuration. + * @return array + */ + private function getTemporaryConfiguration($config) + { + $dbName = $this->parseDbName($config['dsn']); + + $config['dsn'] = preg_replace( + '#;?(dbname|Database)='.$dbName.'#', + '', + $config['dsn'] + ); + + return $config; + } } diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 2e0e7dd..0d637b6 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -161,6 +161,7 @@ class Configuration extends PropelConfiguration ->addDefaultsIfNotSet() ->children() ->booleanNode('ATTR_EMULATE_PREPARES')->defaultFalse()->end() + ->scalarNode('SQLSRV_ATTR_ENCODING')->end() ->end() ->end() ->arrayNode('model_paths')