Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Migration fixes

  • Loading branch information...
commit adddfb8ae5bb30090d273ea0ec616fa5c171eaab 1 parent 8d28991
@cristianoc72 cristianoc72 authored
View
4 src/Propel/Generator/Command/AbstractCommand.php
@@ -105,6 +105,9 @@ protected function parseConnection($connection)
$name = substr($connection, 0, $pos);
$dsn = substr($connection, $pos + 1, strlen($connection));
+ $pos = strpos($dsn, ':');
+ $adapter = substr($dsn, 0, $pos);
+
$extras = array();
foreach (explode(';', $dsn) as $element) {
$parts = preg_split('/=/', $element);
@@ -113,6 +116,7 @@ protected function parseConnection($connection)
$extras[strtolower($parts[0])] = $parts[1];
}
}
+ $extras['adapter'] = $adapter;
return array($name, $dsn, $extras);
}
View
33 src/Propel/Generator/Command/MigrationDiffCommand.php
@@ -17,14 +17,17 @@
use Propel\Generator\Config\GeneratorConfig;
use Propel\Generator\Exception\RuntimeException;
use Propel\Generator\Manager\MigrationManager;
+use Propel\Generator\Model\Database;
+use Propel\Generator\Model\Diff\DatabaseComparator;
use Propel\Generator\Model\IdMethod;
+use Propel\Generator\Model\Schema;
/**
* @author William Durand <william.durand1@gmail.com>
*/
class MigrationDiffCommand extends AbstractCommand
{
- const DEFAULT_OUTPUT_DIRECTORY = 'generated-diff-classes';
+ const DEFAULT_OUTPUT_DIRECTORY = 'generated-migrations';
const DEFAULT_MIGRATION_TABLE = 'propel_migration';
@@ -51,13 +54,16 @@ protected function configure()
protected function execute(InputInterface $input, OutputInterface $output)
{
$generatorConfig = new GeneratorConfig(array(
- 'propel.platform.class' => $input->getOption('platform'),
+ 'propel.platform.class' => $input->getOption('platform'),
+ 'propel.reverse.parser.class' => $this->getReverseClass($input),
+ 'propel.migration.table' => $input->getOption('migration-table')
));
$this->createDirectory($input->getOption('output-dir'));
$manager = new MigrationManager();
$manager->setGeneratorConfig($generatorConfig);
+ $manager->setSchemas($this->getSchemas($input->getOption('input-dir')));
$connections = array();
foreach ($input->getOption('connection') as $connection) {
@@ -80,9 +86,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
$output->writeln(sprintf('Connecting to database "%s" using DSN "%s"', $name, $params['dsn']));
}
- $pdo = $generatorConfig->getBuildPDO($name);
+ $conn = $manager->getAdapterConnection($name);
$database = new Database($name);
- $platform = $generatorConfig->getConfiguredPlatform($pdo);
+ $platform = $generatorConfig->getConfiguredPlatform($conn);
if (!$platform->supportsMigrations()) {
$output->writeln(sprintf('Skipping database "%s" since vendor "%s" does not support migrations', $name, $platform->getDatabaseType()));
@@ -92,14 +98,14 @@ protected function execute(InputInterface $input, OutputInterface $output)
$database->setPlatform($platform);
$database->setDefaultIdMethod(IdMethod::NATIVE);
- $parser = $generatorConfig->getConfiguredSchemaParser($pdo);
+ $parser = $generatorConfig->getConfiguredSchemaParser($conn);
$nbTables = $parser->parse($database, $this);
$schema->addDatabase($database);
$totalNbTables += $nbTables;
if ($input->getOption('verbose')) {
- $output->writeln(sprintf('%d tables found in database "%s"', $nbTables, $name), Project::MSG_VERBOSE);
+ $output->writeln(sprintf('%d tables found in database "%s"', $nbTables, $name), Output::VERBOSITY_VERBOSE);
}
}
@@ -109,7 +115,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$output->writeln('No table found in all databases');
}
- $appDatasFromXml = $this->getDataModels();
+ $appDatasFromXml = $manager->getDataModels();
$appDataFromXml = array_pop($appDatasFromXml);
// comparing models
@@ -129,7 +135,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
continue;
}
- $databaseDiff = DatabaseComparator::computeDiff($database, $appDataFromXml->getDatabase($name), $this->isCaseInsensitive());
+ $databaseDiff = DatabaseComparator::computeDiff($database, $appDataFromXml->getDatabase($name));
if (!$databaseDiff) {
if ($input->getOption('verbose')) {
@@ -168,4 +174,15 @@ protected function execute(InputInterface $input, OutputInterface $output)
$output->writeln('Once the migration class is valid, call the "migrate" task to execute it.');
}
}
+
+ /**
+ * Return the name of the reverse parser class
+ */
+ protected function getReverseClass(InputInterface $input)
+ {
+ $reverse = strstr($input->getOption('platform'), 'Platform', true);
+ $reverse = 'Propel\\Generator\\Reverse\\'.$reverse.'SchemaParser';
+
+ return $reverse;
+ }
}
View
5 src/Propel/Generator/Command/MigrationMigrateCommand.php
@@ -16,6 +16,7 @@
use Symfony\Component\Console\Output\Output;
use Propel\Generator\Config\GeneratorConfig;
use Propel\Generator\Manager\MigrationManager;
+use Propel\Generator\Util\SqlParser;
/**
* @author William Durand <william.durand1@gmail.com>
@@ -100,7 +101,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
));
}
- $pdo = $manager->getPdoConnection($datasource);
+ $conn = $manager->getAdapterConnection($datasource);
$res = 0;
$statements = SqlParser::parseString($sql);
foreach ($statements as $statement) {
@@ -108,7 +109,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
if ($input->getOption('verbose')) {
$output->writeln(sprintf('Executing statement "%s"', $statement));
}
- $stmt = $pdo->prepare($statement);
+ $stmt = $conn->prepare($statement);
$stmt->execute();
$res++;
} catch (\PDOException $e) {
View
7 src/Propel/Generator/Config/GeneratorConfig.php
@@ -14,6 +14,7 @@
use Propel\Generator\Model\Table;
use Propel\Generator\Platform\PlatformInterface;
use Propel\Generator\Reverse\SchemaParserInterface;
+use Propel\Runtime\Connection\ConnectionInterface;
/**
* A class that holds build properties and provide a class loading mechanism for
@@ -159,7 +160,7 @@ public function getBuilderClassName($type)
* @param \PDO $con
* @return PlatformInterface
*/
- public function getConfiguredPlatform(\PDO $con = null, $database = null)
+ public function getConfiguredPlatform(ConnectionInterface $con = null, $database = null)
{
$buildConnection = $this->getBuildConnection($database);
@@ -188,10 +189,10 @@ public function getConfiguredPlatform(\PDO $con = null, $database = null)
/**
* Creates and configures a new SchemaParser class for specified platform.
- * @param \PDO $con
+ * @param ConnectionInterface $con
* @return SchemaParserInterface
*/
- public function getConfiguredSchemaParser(\PDO $con = null)
+ public function getConfiguredSchemaParser(ConnectionInterface $con = null)
{
$clazz = $this->getClassName("reverseParserClass");
$parser = new $clazz();
View
3  src/Propel/Generator/Config/GeneratorConfigInterface.php
@@ -14,6 +14,7 @@
use Propel\Generator\Builder\DataModelBuilder;
use Propel\Generator\Model\Table;
use Propel\Generator\Platform\PlatformInterface;
+use Propel\Runtime\Connection\ConnectionInterface;
interface GeneratorConfigInterface
{
@@ -56,5 +57,5 @@ public function setBuildProperty($name, $value);
* @param \PDO $con
* @return PlatformInterface
*/
- public function getConfiguredPlatform(\PDO $con = null, $database = null);
+ public function getConfiguredPlatform(ConnectionInterface $con = null, $database = null);
}
View
3  src/Propel/Generator/Config/QuickGeneratorConfig.php
@@ -16,6 +16,7 @@
use Propel\Generator\Config\GeneratorConfigInterface;
use Propel\Generator\Exception\RuntimeException;
use Propel\Generator\Model\Table;
+use \Propel\Runtime\Connection\ConnectionInterface;
class QuickGeneratorConfig implements GeneratorConfigInterface
{
@@ -151,7 +152,7 @@ public function setBuildProperty($name, $value)
/**
* {@inheritdoc}
*/
- public function getConfiguredPlatform(\PDO $con = null, $database = null)
+ public function getConfiguredPlatform(ConnectionInterface $con = null, $database = null)
{
return null;
}
View
43 src/Propel/Generator/Manager/MigrationManager.php
@@ -12,7 +12,11 @@
use Propel\Generator\Exception\InvalidArgumentException;
use Propel\Generator\Model\Column;
+use Propel\Generator\Model\Database;
use Propel\Generator\Model\Table;
+use Propel\Generator\Util\SqlParser;
+use Propel\Runtime\Adapter\AdapterFactory;
+use Propel\Runtime\Connection\ConnectionFactory;
/**
* Service class for preparing and executing migrations
@@ -29,7 +33,7 @@ class MigrationManager extends AbstractManager
/**
* @var array
*/
- protected $pdoConnections = array();
+ protected $adapterConnections = array();
/**
* @var string
@@ -65,23 +69,18 @@ public function getConnection($datasource)
return $this->connections[$datasource];
}
- public function getPdoConnection($datasource)
+ public function getAdapterConnection($datasource)
{
- if (!isset($pdoConnections[$datasource])) {
+ if (!isset($adapterConnections[$datasource])) {
$buildConnection = $this->getConnection($datasource);
$dsn = str_replace("@DB@", $datasource, $buildConnection['dsn']);
- // Set user + password to null if they are empty strings or missing
- $username = isset($buildConnection['user']) && $buildConnection['user'] ? $buildConnection['user'] : null;
- $password = isset($buildConnection['password']) && $buildConnection['password'] ? $buildConnection['password'] : null;
+ $conn = ConnectionFactory::create($buildConnection, AdapterFactory::create($buildConnection['adapter']));
- $pdo = new \PDO($dsn, $username, $password);
- $pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
-
- $pdoConnections[$datasource] = $pdo;
+ $adapterConnections[$datasource] = $conn;
}
- return $pdoConnections[$datasource];
+ return $adapterConnections[$datasource];
}
public function getPlatform($datasource)
@@ -122,11 +121,11 @@ public function getOldestDatabaseVersion()
$oldestMigrationTimestamp = null;
$migrationTimestamps = array();
foreach ($connections as $name => $params) {
- $pdo = $this->getPdoConnection($name);
+ $conn = $this->getAdapterConnection($name);
$sql = sprintf('SELECT version FROM %s', $this->getMigrationTable());
try {
- $stmt = $pdo->prepare($sql);
+ $stmt = $conn->prepare($sql);
$stmt->execute();
if ($migrationTimestamp = $stmt->fetchColumn()) {
$migrationTimestamps[$name] = $migrationTimestamp;
@@ -147,9 +146,9 @@ public function getOldestDatabaseVersion()
public function migrationTableExists($datasource)
{
- $pdo = $this->getPdoConnection($datasource);
+ $conn = $this->getAdapterConnection($datasource);
$sql = sprintf('SELECT version FROM %s', $this->getMigrationTable());
- $stmt = $pdo->prepare($sql);
+ $stmt = $conn->prepare($sql);
try {
$stmt->execute();
@@ -176,8 +175,8 @@ public function createMigrationTable($datasource)
$table->addColumn($column);
// insert the table into the database
$statements = $platform->getAddTableDDL($table);
- $pdo = $this->getPdoConnection($datasource);
- $res = SqlParser::executeString($statements, $pdo);
+ $conn = $this->getAdapterConnection($datasource);
+ $res = SqlParser::executeString($statements, $conn);
if (!$res) {
throw new \Exception(sprintf('Unable to create migration table in datasource "%s"', $datasource));
@@ -187,19 +186,19 @@ public function createMigrationTable($datasource)
public function updateLatestMigrationTimestamp($datasource, $timestamp)
{
$platform = $this->getPlatform($datasource);
- $pdo = $this->getPdoConnection($datasource);
+ $conn = $this->getAdapterConnection($datasource);
$sql = sprintf('DELETE FROM %s', $this->getMigrationTable());
- $pdo->beginTransaction();
- $stmt = $pdo->prepare($sql);
+ $conn->beginTransaction();
+ $stmt = $conn->prepare($sql);
$stmt->execute();
$sql = sprintf('INSERT INTO %s (%s) VALUES (?)',
$this->getMigrationTable(),
$platform->quoteIdentifier('version')
);
- $stmt = $pdo->prepare($sql);
+ $stmt = $conn->prepare($sql);
$stmt->bindParam(1, $timestamp, \PDO::PARAM_INT);
$stmt->execute();
- $pdo->commit();
+ $conn->commit();
}
public function getMigrationTimestamps()
View
1  src/Propel/Generator/Model/Diff/DatabaseDiff.php
@@ -10,6 +10,7 @@
namespace Propel\Generator\Model\Diff;
+use Propel\Generator\Model\IdMethod;
use Propel\Generator\Model\Table;
/**
View
9 src/Propel/Generator/Util/SqlParser.php
@@ -10,6 +10,8 @@
namespace Propel\Generator\Util;
+use \Propel\Runtime\Connection\StatementInterface;
+
/**
* Service class for parsing a large SQL string into an array of SQL statements
*
@@ -85,15 +87,18 @@ public static function executeFile($file, $connection)
*/
protected static function executeStatements($statements, $connection)
{
+ $executed = 0;
+
foreach ($statements as $statement) {
$stmt = $connection->prepare($statement);
- if ($stmt instanceof PDOStatement) {
+ if (($stmt instanceof PDOStatement) || ($stmt instanceof StatementInterface)) {
// only execute if has no error
$stmt->execute();
+ $executed++;
}
}
- return count($statements);
+ return $executed;
}
/**
Please sign in to comment.
Something went wrong with that request. Please try again.