From ef1e4eb07f91b7c68abb1c1850d2cdc01d1bdf24 Mon Sep 17 00:00:00 2001 From: "Marc J. Schmidt" Date: Tue, 15 Apr 2014 19:19:52 +0200 Subject: [PATCH] Fixed #477, Fixed #607, Fixed #604, Fixed #610 --- .../Archivable/ArchivableBehavior.php | 1 - .../ConcreteInheritanceBehavior.php | 11 +- .../Generator/Builder/Util/SchemaReader.php | 12 +- .../Command/MigrationDiffCommand.php | 49 +++++-- .../Generator/Command/MigrationUpCommand.php | 5 +- .../Generator/Manager/AbstractManager.php | 47 +++++- src/Propel/Generator/Manager/SqlManager.php | 28 ---- src/Propel/Generator/Model/Database.php | 10 +- .../Model/Diff/DatabaseComparator.php | 46 +++++- .../Generator/Model/Diff/DatabaseDiff.php | 21 +++ .../Generator/Model/Diff/TableComparator.php | 16 +-- src/Propel/Generator/Model/ForeignKey.php | 35 ++++- src/Propel/Generator/Model/Index.php | 64 +++++---- src/Propel/Generator/Model/Table.php | 86 ++--------- src/Propel/Generator/Model/Unique.php | 15 -- .../Generator/Platform/DefaultPlatform.php | 2 +- .../Generator/Platform/OraclePlatform.php | 2 +- .../Generator/Platform/SqlitePlatform.php | 39 ++--- .../Generator/Reverse/MssqlSchemaParser.php | 2 +- .../Generator/Reverse/MysqlSchemaParser.php | 83 +++++++---- .../Generator/Reverse/OracleSchemaParser.php | 3 +- .../Generator/Reverse/PgsqlSchemaParser.php | 123 +++++++++++----- .../Reverse/SchemaParserInterface.php | 4 +- .../Generator/Reverse/SqliteSchemaParser.php | 135 ++++++++++++------ src/Propel/Generator/Util/QuickBuilder.php | 5 +- tests/Fixtures/schemas/schema.xml | 1 - .../Archivable/ArchivableBehaviorTest.php | 4 +- .../Behavior/I18n/I18nBehaviorTest.php | 10 +- .../Versionable/VersionableBehaviorTest.php | 28 +++- .../Generator/Migration/MigrationTestCase.php | 4 +- .../PropelDatabaseTableComparatorTest.php | 42 ++++++ .../PropelTableForeignKeyComparatorTest.php | 19 +-- .../Tests/Generator/Model/IndexTest.php | 4 +- .../Tests/Generator/Model/ModelTestCase.php | 3 +- .../Tests/Generator/Model/TableTest.php | 18 ++- .../Tests/Generator/Model/UniqueTest.php | 4 +- .../Generator/Platform/MssqlPlatformTest.php | 58 ++++---- .../MysqlPlatformMigrationMyISAMTest.php | 29 ++-- .../Platform/MysqlPlatformMigrationTest.php | 99 +++++++++---- .../Platform/MysqlPlatformMyISAMTest.php | 20 +-- .../Generator/Platform/MysqlPlatformTest.php | 40 +++--- .../Platform/OraclePlatformMigrationTest.php | 59 +++----- .../Generator/Platform/OraclePlatformTest.php | 44 +++--- .../Platform/PgsqlPlatformMigrationTest.php | 43 +++--- .../Generator/Platform/PgsqlPlatformTest.php | 24 ++-- .../PlatformMigrationTestProvider.php | 38 ++--- .../Platform/PlatformTestProvider.php | 6 +- .../Generator/Platform/SqlitePlatformTest.php | 6 +- .../Tests/Generator/Util/QuickBuilderTest.php | 3 +- tests/Propel/Tests/Issues/Issue617Test.php | 8 +- tests/Propel/Tests/Resources/blog-schema.xml | 6 +- 51 files changed, 881 insertions(+), 583 deletions(-) diff --git a/src/Propel/Generator/Behavior/Archivable/ArchivableBehavior.php b/src/Propel/Generator/Behavior/Archivable/ArchivableBehavior.php index dcaff5dff3..9805916f0b 100644 --- a/src/Propel/Generator/Behavior/Archivable/ArchivableBehavior.php +++ b/src/Propel/Generator/Behavior/Archivable/ArchivableBehavior.php @@ -102,7 +102,6 @@ protected function addArchiveTable() // copy the indices foreach ($table->getIndices() as $index) { $copiedIndex = clone $index; - $copiedIndex->setName(''); $archiveTable->addIndex($copiedIndex); } // copy unique indices to indices diff --git a/src/Propel/Generator/Behavior/ConcreteInheritance/ConcreteInheritanceBehavior.php b/src/Propel/Generator/Behavior/ConcreteInheritance/ConcreteInheritanceBehavior.php index b2be92e7ac..2bcb704216 100644 --- a/src/Propel/Generator/Behavior/ConcreteInheritance/ConcreteInheritanceBehavior.php +++ b/src/Propel/Generator/Behavior/ConcreteInheritance/ConcreteInheritanceBehavior.php @@ -10,6 +10,7 @@ namespace Propel\Generator\Behavior\ConcreteInheritance; +use Propel\Generator\Exception\InvalidArgumentException; use Propel\Generator\Model\Behavior; use Propel\Generator\Model\ForeignKey; @@ -66,7 +67,9 @@ public function modifyTable() if ($column->isPrimaryKey() && $this->isCopyData()) { $fk = new ForeignKey(); $fk->setForeignTableCommonName($column->getTable()->getCommonName()); - $fk->setForeignSchemaName($column->getTable()->getSchema()); + if ($table->guessSchemaName() != $column->getTable()->guessSchemaName()) { + $fk->setForeignSchemaName($column->getTable()->guessSchemaName()); + } $fk->setOnDelete('CASCADE'); $fk->setOnUpdate(null); $fk->addReference($copiedColumn, $column); @@ -123,7 +126,11 @@ protected function getParentTable() $tableName = $this->getParameter('schema').$database->getPlatform()->getSchemaDelimiter().$tableName; } - return $database->getTable($tableName); + if (!$table = $database->getTable($tableName)) { + throw new InvalidArgumentException(sprintf('Table "%s" used in the concrete_inheritance behavior at table "%s" not exist.', $tableName, $this->getTable()->getName())); + } + + return $table; } protected function isCopyData() diff --git a/src/Propel/Generator/Builder/Util/SchemaReader.php b/src/Propel/Generator/Builder/Util/SchemaReader.php index c3ed18b12a..2c03ff035e 100644 --- a/src/Propel/Generator/Builder/Util/SchemaReader.php +++ b/src/Propel/Generator/Builder/Util/SchemaReader.php @@ -232,11 +232,13 @@ public function startElement($parser, $name, $attributes) break; case 'index': - $this->currIndex = $this->currTable->addIndex($attributes); + $this->currIndex = new Index(); + $this->currIndex->loadMapping($attributes); break; case 'unique': - $this->currUnique = $this->currTable->addUnique($attributes); + $this->currUnique = new Unique(); + $this->currUnique->loadMapping($attributes); break; case 'vendor': @@ -359,6 +361,12 @@ protected function _throwInvalidTagException($parser, $tag_name) public function endElement($parser, $name) { + if ('index' === $name) { + $this->currTable->addIndex($this->currIndex); + } else if ('unique' === $name) { + $this->currTable->addUnique($this->currUnique); + } + if (self::DEBUG) { print('endElement(' . $name . ") called\n"); } diff --git a/src/Propel/Generator/Command/MigrationDiffCommand.php b/src/Propel/Generator/Command/MigrationDiffCommand.php index ce8bc93938..a117a72b89 100644 --- a/src/Propel/Generator/Command/MigrationDiffCommand.php +++ b/src/Propel/Generator/Command/MigrationDiffCommand.php @@ -41,6 +41,7 @@ protected function configure() ->addOption('output-dir', null, InputOption::VALUE_REQUIRED, 'The output directory', self::DEFAULT_OUTPUT_DIRECTORY) ->addOption('migration-table', null, InputOption::VALUE_REQUIRED, 'Migration table name', self::DEFAULT_MIGRATION_TABLE) ->addOption('connection', null, InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, 'Connection to use', array()) + ->addOption('table-renaming', null, InputOption::VALUE_NONE, 'Detect table renaming', null) ->addOption('editor', null, InputOption::VALUE_OPTIONAL, 'The text editor to use to open diff files', null) ->setName('migration:diff') ->setAliases(array('diff')) @@ -85,8 +86,15 @@ protected function execute(InputInterface $input, OutputInterface $output) } $totalNbTables = 0; - $schema = new Schema(); - foreach ($connections as $name => $params) { + $reversedSchema = new Schema(); + + foreach ($manager->getDatabases() as $appDatabase) { + + $name = $appDatabase->getName(); + if (!$params = @$connections[$name]) { + $output->writeln(sprintf('No connection configured for database "%s"', $name)); + } + if ($input->getOption('verbose')) { $output->writeln(sprintf('Connecting to database "%s" using DSN "%s"', $name, $params['dsn'])); } @@ -99,14 +107,22 @@ protected function execute(InputInterface $input, OutputInterface $output) continue; } + $additionalTables = []; + foreach ($appDatabase->getTables() as $table) { + if ($table->getSchema() && $table->getSchema() != $appDatabase->getSchema()) { + $additionalTables[] = $table; + } + } + $database = new Database($name); $database->setPlatform($platform); + $database->setSchema($appDatabase->getSchema()); $database->setDefaultIdMethod(IdMethod::NATIVE); $parser = $generatorConfig->getConfiguredSchemaParser($conn); - $nbTables = $parser->parse($database, $this); + $nbTables = $parser->parse($database, $additionalTables); - $schema->addDatabase($database); + $reversedSchema->addDatabase($database); $totalNbTables += $nbTables; if ($input->getOption('verbose')) { @@ -120,27 +136,24 @@ protected function execute(InputInterface $input, OutputInterface $output) $output->writeln('No table found in all databases'); } - $appDatasFromXml = $manager->getDataModels(); - $appDataFromXml = array_pop($appDatasFromXml); - // comparing models $output->writeln('Comparing models...'); + $tableRenaming = $input->getOption('table-renaming'); $migrationsUp = array(); $migrationsDown = array(); - foreach ($schema->getDatabases() as $database) { + foreach ($reversedSchema->getDatabases() as $database) { $name = $database->getName(); if ($input->getOption('verbose')) { $output->writeln(sprintf('Comparing database "%s"', $name)); } - if (!$appDataFromXml->hasDatabase($name)) { - // FIXME: tables present in database but not in XML + if (!$appDataDatabase = $manager->getDatabase($name)) { continue; } - $databaseDiff = DatabaseComparator::computeDiff($database, $appDataFromXml->getDatabase($name)); + $databaseDiff = DatabaseComparator::computeDiff($database, $appDataDatabase, false, $tableRenaming); if (!$databaseDiff) { if ($input->getOption('verbose')) { @@ -151,6 +164,13 @@ protected function execute(InputInterface $input, OutputInterface $output) $output->writeln(sprintf('Structure of database was modified in datasource "%s": %s', $name, $databaseDiff->getDescription())); + foreach ($databaseDiff->getPossibleRenamedTables() as $fromTableName => $toTableName) { + $output->writeln(sprintf( + 'Possible table renaming detected: "%s" to "%s". It will be deleted and recreated. Use --table-renaming to only rename it.', + $fromTableName, $toTableName + )); + } + $platform = $generatorConfig->getConfiguredPlatform(null, $name); $migrationsUp[$name] = $platform->getModifyDatabaseDDL($databaseDiff); $migrationsDown[$name] = $platform->getModifyDatabaseDDL($databaseDiff->getReverseDiff()); @@ -185,8 +205,11 @@ protected function execute(InputInterface $input, OutputInterface $output) */ protected function getReverseClass(InputInterface $input) { - $reverse = strstr($input->getOption('platform'), 'Platform', true); - $reverse = 'Propel\\Generator\\Reverse\\'.$reverse.'SchemaParser'; + $reverse = $input->getOption('platform'); + if (false !== strpos($reverse, 'Platform')) { + $reverse = strstr($input->getOption('platform'), 'Platform', true); + } + $reverse = sprintf('Propel\\Generator\\Reverse\\%sSchemaParser', ucfirst($reverse)); return $reverse; } diff --git a/src/Propel/Generator/Command/MigrationUpCommand.php b/src/Propel/Generator/Command/MigrationUpCommand.php index c86f553d0f..b9e55024bc 100644 --- a/src/Propel/Generator/Command/MigrationUpCommand.php +++ b/src/Propel/Generator/Command/MigrationUpCommand.php @@ -10,6 +10,7 @@ namespace Propel\Generator\Command; +use Propel\Runtime\Exception\RuntimeException; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; @@ -112,9 +113,7 @@ protected function execute(InputInterface $input, OutputInterface $output) $stmt->execute(); $res++; } catch (\PDOException $e) { - $output->writeln(sprintf('Failed to execute SQL "%s". Aborting migration.', $statement)); - - return false; + throw new RuntimeException(sprintf('Failed to execute SQL "%s". Aborting migration.', $statement), 0, $e); } } if (!$res) { diff --git a/src/Propel/Generator/Manager/AbstractManager.php b/src/Propel/Generator/Manager/AbstractManager.php index 6fd63e8a46..bd28b890ab 100644 --- a/src/Propel/Generator/Manager/AbstractManager.php +++ b/src/Propel/Generator/Manager/AbstractManager.php @@ -14,6 +14,7 @@ use Propel\Generator\Config\GeneratorConfigInterface; use Propel\Generator\Exception\BuildException; use Propel\Generator\Exception\EngineException; +use Propel\Generator\Model\Database; use Propel\Generator\Model\Schema; /** @@ -31,6 +32,11 @@ abstract class AbstractManager */ protected $dataModels = array(); + /** + * @var Database[] + */ + protected $databases; + /** * Map of data model name to database name. * Should probably stick to the convention @@ -162,6 +168,44 @@ public function getDataModelDbMap() return $this->dataModelDbMap; } + /** + * @return Database[] + */ + public function getDatabases() + { + if (null === $this->databases) { + $databases = array(); + foreach ($this->getDataModels() as $dataModel) { + foreach ($dataModel->getDatabases() as $database) { + if (!isset($databases[$database->getName()])) { + $databases[$database->getName()] = $database; + } else { + $tables = $database->getTables(); + // Merge tables from different schema.xml to the same database + foreach ($tables as $table) { + if (!$databases[$database->getName()]->hasTable($table->getName(), true)) { + $databases[$database->getName()]->addTable($table); + } + } + } + } + } + $this->databases = $databases; + } + + return $this->databases; + } + + /** + * @param string $name + * @return Database|null + */ + public function getDatabase($name) + { + $dbs = $this->getDatabases(); + return @$dbs[$name]; + } + /** * Sets whether to perform validation on the datamodel schema.xml file(s). * @@ -233,11 +277,8 @@ protected function loadDataModels() $dom = new \DOMDocument('1.0', 'UTF-8'); $dom->load($dmFilename); - - $this->includeExternalSchemas($dom, $schema->getPath()); - // normalize (or transform) the XML document using XSLT if ($this->getGeneratorConfig()->getBuildProperty('schemaTransform') && $this->xsl) { $this->log('Transforming ' . $dmFilename . ' using stylesheet ' . $this->xsl->getPath()); diff --git a/src/Propel/Generator/Manager/SqlManager.php b/src/Propel/Generator/Manager/SqlManager.php index e6eecaec0e..a31f4707ad 100644 --- a/src/Propel/Generator/Manager/SqlManager.php +++ b/src/Propel/Generator/Manager/SqlManager.php @@ -67,34 +67,6 @@ public function getConnection($datasource) return $this->connections[$datasource]; } - /** - * @return array - */ - public function getDatabases() - { - if (null === $this->databases) { - $databases = array(); - foreach ($this->getDataModels() as $dataModel) { - foreach ($dataModel->getDatabases() as $database) { - if (!isset($databases[$database->getName()])) { - $databases[$database->getName()] = $database; - } else { - $tables = $database->getTables(); - // Merge tables from different schema.xml to the same database - foreach ($tables as $table) { - if (!$databases[$database->getName()]->hasTable($table->getName(), true)) { - $databases[$database->getName()]->addTable($table); - } - } - } - } - } - $this->databases = $databases; - } - - return $this->databases; - } - /** * @return string */ diff --git a/src/Propel/Generator/Model/Database.php b/src/Propel/Generator/Model/Database.php index 3cf6a5a683..400ab41157 100644 --- a/src/Propel/Generator/Model/Database.php +++ b/src/Propel/Generator/Model/Database.php @@ -460,7 +460,6 @@ public function addTable($table) if (!$table instanceof Table) { $tbl = new Table(); $tbl->setDatabase($this); - $tbl->setSchema($this->getSchema()); $tbl->loadMapping($table); return $this->addTable($tbl); @@ -472,10 +471,6 @@ public function addTable($table) throw new EngineException(sprintf('Table "%s" declared twice', $table->getName())); } - if (null === $table->getSchema()) { - $table->setSchema($this->getSchema()); - } - $this->tables[] = $table; $this->tablesByName[$table->getName()] = $table; $this->tablesByLowercaseName[strtolower($table->getName())] = $table; @@ -798,7 +793,6 @@ protected function registerBehavior(Behavior $behavior) protected function setupTableReferrers() { foreach ($this->tables as $table) { - $table->doNaming(); $table->setupReferrers(); } } @@ -823,8 +817,10 @@ public function __toString() $fks = []; foreach ($table->getForeignKeys() as $fk) { - $fks[] = sprintf(" %s (%s => %s)", + $fks[] = sprintf(" %s to %s.%s (%s => %s)", $fk->getName(), + $fk->getForeignSchemaName(), + $fk->getForeignTableCommonName(), join(', ', $fk->getLocalColumns()), join(', ', $fk->getForeignColumns()) ); diff --git a/src/Propel/Generator/Model/Diff/DatabaseComparator.php b/src/Propel/Generator/Model/Diff/DatabaseComparator.php index d1d11e5f7d..9875cdb551 100644 --- a/src/Propel/Generator/Model/Diff/DatabaseComparator.php +++ b/src/Propel/Generator/Model/Diff/DatabaseComparator.php @@ -34,6 +34,14 @@ class DatabaseComparator */ protected $toDatabase; + /** + * Whether we should detect renamings and track it via `addRenamedTable` at the + * DatabaseDiff object. + * + * @var bool + */ + protected $withRenaming = false; + public function __construct($databaseDiff = null) { $this->databaseDiff = (null === $databaseDiff) ? new DatabaseDiff() : $databaseDiff; @@ -92,11 +100,12 @@ public function getToDatabase() * @param boolean $caseInsensitive * @return DatabaseDiff|Boolean */ - public static function computeDiff(Database $fromDatabase, Database $toDatabase, $caseInsensitive = false) + public static function computeDiff(Database $fromDatabase, Database $toDatabase, $caseInsensitive = false, $withRenaming = false) { $dc = new self(); $dc->setFromDatabase($fromDatabase); $dc->setToDatabase($toDatabase); + $dc->setWithRenaming($withRenaming); $platform = $toDatabase->getPlatform() ?: $fromDatabase->getPlatform(); @@ -115,6 +124,22 @@ public static function computeDiff(Database $fromDatabase, Database $toDatabase, return ($differences > 0) ? $dc->getDatabaseDiff() : false; } + /** + * @param boolean $withRenaming + */ + public function setWithRenaming($withRenaming) + { + $this->withRenaming = $withRenaming; + } + + /** + * @return boolean + */ + public function getWithRenaming() + { + return $this->withRenaming; + } + /** * Returns the number of differences. * @@ -158,6 +183,25 @@ public function compareTables($caseInsensitive = false) } } + // check for table renamings + foreach ($this->databaseDiff->getAddedTables() as $addedTableName => $addedTable) { + foreach ($this->databaseDiff->getRemovedTables() as $removedTableName => $removedTable) { + if (!TableComparator::computeDiff($addedTable, $removedTable, $caseInsensitive)) { + // no difference except the name, that's probably a renaming + if ($this->getWithRenaming()) { + $this->databaseDiff->addRenamedTable($removedTableName, $addedTableName); + $this->databaseDiff->removeAddedTable($addedTableName); + $this->databaseDiff->removeRemovedTable($removedTableName); + $databaseDifferences--; + } else { + $this->databaseDiff->addPossibleRenamedTable($removedTableName, $addedTableName); + } + // skip to the next added table + break; + } + } + } + return $databaseDifferences; } } diff --git a/src/Propel/Generator/Model/Diff/DatabaseDiff.php b/src/Propel/Generator/Model/Diff/DatabaseDiff.php index 69f9240df1..3bbfbf4690 100644 --- a/src/Propel/Generator/Model/Diff/DatabaseDiff.php +++ b/src/Propel/Generator/Model/Diff/DatabaseDiff.php @@ -24,6 +24,7 @@ class DatabaseDiff protected $removedTables; protected $modifiedTables; protected $renamedTables; + protected $possibleRenamedTables; public function __construct() { @@ -31,6 +32,7 @@ public function __construct() $this->removedTables = []; $this->modifiedTables = []; $this->renamedTables = []; + $this->possibleRenamedTables = []; } /** @@ -64,6 +66,25 @@ public function removeAddedTable($name) unset($this->addedTables[$name]); } + /** + * @return string[] + */ + public function getPossibleRenamedTables() + { + return $this->possibleRenamedTables; + } + + /** + * Adds a possible renamed table. + * + * @param string $fromName + * @param string $toName + */ + public function addPossibleRenamedTable($fromName, $toName) + { + $this->possibleRenamedTables[$fromName] = $toName; + } + /** * Returns the list of added tables. * diff --git a/src/Propel/Generator/Model/Diff/TableComparator.php b/src/Propel/Generator/Model/Diff/TableComparator.php index c1421fa70d..bfb356efe9 100644 --- a/src/Propel/Generator/Model/Diff/TableComparator.php +++ b/src/Propel/Generator/Model/Diff/TableComparator.php @@ -288,14 +288,14 @@ public function compareForeignKeys($caseInsensitive = false) foreach ($fromTableFks as $fromTableFkPos => $fromTableFk) { foreach ($toTableFks as $toTableFkPos => $toTableFk) { - if (false === ForeignKeyComparator::computeDiff($fromTableFk, $toTableFk, $caseInsensitive)) { - unset($fromTableFks[$fromTableFkPos]); - unset($toTableFks[$toTableFkPos]); - } else { - $test = $caseInsensitive ? - strtolower($fromTableFk->getName()) == strtolower($toTableFk->getName()) : - $fromTableFk->getName() == $toTableFk->getName(); - if ($test) { + $sameName = $caseInsensitive ? + strtolower($fromTableFk->getName()) == strtolower($toTableFk->getName()) : + $fromTableFk->getName() == $toTableFk->getName(); + if ($sameName) { + if (false === ForeignKeyComparator::computeDiff($fromTableFk, $toTableFk, $caseInsensitive)) { + unset($fromTableFks[$fromTableFkPos]); + unset($toTableFks[$toTableFkPos]); + } else { // same name, but different columns $this->tableDiff->addModifiedFk($fromTableFk->getName(), $fromTableFk, $toTableFk); unset($fromTableFks[$fromTableFkPos]); diff --git a/src/Propel/Generator/Model/ForeignKey.php b/src/Propel/Generator/Model/ForeignKey.php index 888ba82284..48f9d3b5c7 100644 --- a/src/Propel/Generator/Model/ForeignKey.php +++ b/src/Propel/Generator/Model/ForeignKey.php @@ -95,6 +95,11 @@ class ForeignKey extends MappingModel */ private $skipSql; + /** + * @var bool + */ + private $autoNaming = false; + /** * Constructs a new ForeignKey object. * @@ -120,10 +125,6 @@ protected function setupObject() $this->foreignTableCommonName = $this->parentTable->getDatabase()->getTablePrefix() . $this->getAttribute('foreignTable'); $this->foreignSchemaName = $this->getAttribute('foreignSchema'); - if (!$this->foreignSchemaName && $schema = $this->getSchemaName()) { - $this->foreignSchemaName = $schema; - } - $this->name = $this->getAttribute('name'); $this->phpName = $this->getAttribute('phpName'); $this->refPhpName = $this->getAttribute('refPhpName'); @@ -133,6 +134,27 @@ protected function setupObject() $this->skipSql = $this->booleanValue($this->getAttribute('skipSql')); } + protected function doNaming() + { + if (!$this->name || $this->autoNaming) { + $newName = 'fk_'; + + $hash = []; + $hash[] = $this->foreignSchemaName . '.' . $this->foreignTableCommonName; + $hash[] = implode(',', (array)$this->localColumns); + $hash[] = implode(',', (array)$this->foreignColumns); + + $newName .= substr(md5(strtolower(implode(':', $hash))), 0, 6); + + if ($this->parentTable) { + $newName = $this->parentTable->getCommonName() . '_' . $newName; + } + + $this->name = $newName; + $this->autoNaming = true; + } + } + /** * Returns the normalized input of onDelete and onUpdate behaviors. * @@ -236,6 +258,8 @@ public function setOnUpdate($behavior) */ public function getName() { + $this->doNaming(); + return $this->name; } @@ -246,6 +270,7 @@ public function getName() */ public function setName($name) { + $this->autoNaming = !$name; //if no name we activate autoNaming $this->name = $name; } @@ -345,7 +370,7 @@ public function getForeignTableName() } $database = $this->getDatabase(); - if ($database && ($schema = $database->getSchema()) && $platform->supportsSchemas()) { + if ($database && ($schema = $this->parentTable->guessSchemaName()) && $platform->supportsSchemas()) { return $schema . $platform->getSchemaDelimiter() . $this->foreignTableCommonName diff --git a/src/Propel/Generator/Model/Index.php b/src/Propel/Generator/Model/Index.php index d39091d04f..cee137fa30 100644 --- a/src/Propel/Generator/Model/Index.php +++ b/src/Propel/Generator/Model/Index.php @@ -10,8 +10,6 @@ namespace Propel\Generator\Model; -use Propel\Generator\Exception\EngineException; - /** * Information about indices of a table. * @@ -36,12 +34,17 @@ class Index extends MappingModel /** * @var string[] */ - private $columns; + protected $columns; /** * @var string[] */ - private $columnsSize; + protected $columnsSize; + + /** + * @var bool + */ + protected $autoNaming = false; /** * Creates a new Index instance. @@ -77,6 +80,7 @@ public function isUnique() */ public function setName($name) { + $this->autoNaming = !$name; //if no name we activate autoNaming $this->name = $name; } @@ -87,22 +91,39 @@ public function setName($name) */ public function getName() { - if (!$this->name) { - try { - // generate an index name if we don't have a supplied one - $this->createName(); - } catch (EngineException $e) { - // still no name - } - } + $this->doNaming(); - if ($database = $this->table->getDatabase()) { + if ($this->table && $database = $this->table->getDatabase()) { return substr($this->name, 0, $database->getMaxColumnNameLength()); } return $this->name; } + protected function doNaming() + { + if (!$this->name || $this->autoNaming) { + $newName = sprintf('%s_', $this instanceof Unique ? 'u' : 'i'); + + if ($this->columns) { + $hash = []; + $hash[] = implode(',', (array)$this->columns); + $hash[] = implode(',', (array)$this->columnsSize); + + $newName .= substr(md5(strtolower(implode(':', $hash))), 0, 6); + } else { + $newName .= 'no_columns'; + } + + if ($this->table) { + $newName = $this->table->getCommonName() . '_' . $newName; + } + + $this->name = $newName; + $this->autoNaming = true; + } + } + public function getFQName() { $table = $this->getTable(); @@ -117,17 +138,6 @@ public function getFQName() return $this->getName(); } - protected function createName() - { - $inputs[] = $this->table->getDatabase(); - $inputs[] = $this->table->getCommonName(); - $inputs[] = 'I'; - $inputs[] = count($this->table->getIndices()) + 1; - - // @TODO replace the factory by a real object - $this->name = NameFactory::generateName(NameFactory::CONSTRAINT_GENERATOR, $inputs); - } - /** * Sets the index parent Table. * @@ -161,7 +171,7 @@ public function getTableName() /** * Adds a new column to the index. * - * @param Column|string $data Column or attributes from XML. + * @param Column|array $data Column or attributes from XML. */ public function addColumn($data) { @@ -191,7 +201,7 @@ public function hasColumn($name) /** * Sets an array of columns to use for the index. * - * @param array $columns + * @param array $columns array of array definitions $columns[]['name'] = 'columnName' */ public function setColumns(array $columns) { @@ -299,6 +309,6 @@ public function getColumns() protected function setupObject() { - $this->name = $this->getAttribute('name'); + $this->setName($this->getAttribute('name')); } } diff --git a/src/Propel/Generator/Model/Table.php b/src/Propel/Generator/Model/Table.php index d975b09efc..fd3c37bb15 100644 --- a/src/Propel/Generator/Model/Table.php +++ b/src/Propel/Generator/Model/Table.php @@ -255,10 +255,6 @@ public function doFinalInitialization() $this->doHeavyIndexing(); } - // Name any indices which are missing a name using the - // appropriate algorithm. - $this->doNaming(); - // if idMethod is "native" and in fact there are no autoIncrement // columns in the table, then change it to "none" $anyAutoInc = false; @@ -341,7 +337,7 @@ public function addExtraIndices() } // no matching index defined in the schema, so we have to create one - $name = sprintf('I_referenced_%s_%s', $foreignKey->getName(), ++$counter); + $name = sprintf('i_referenced_%s_%s', $foreignKey->getName(), ++$counter); if ($this->hasIndex($name)) { // if we have already a index with this name, then it looks like the columns of this index have just // been changed, so remove it and inject it again. This is the case if a referenced table is handled @@ -366,7 +362,7 @@ public function addExtraIndices() // MySQL needs indices on any columns that serve as foreign keys. // These are not auto-created prior to 4.1.2. - $name = substr_replace($foreignKey->getName(), 'FI_', strrpos($foreignKey->getName(), 'FK_'), 3); + $name = substr_replace($foreignKey->getName(), 'fi_', strrpos($foreignKey->getName(), 'fk_'), 3); if ($this->hasIndex($name)) { // if we already have an index with this name, then it looks like the columns of this index have just // been changed, so remove it and inject it again. This is the case if a referenced table is handled @@ -445,67 +441,6 @@ public function getColumnList($columns, $delimiter = ',') return implode($delimiter, $list); } - /** - * Names composing objects which haven't yet been named. This - * currently consists of foreign-key and index entities. - */ - public function doNaming() - { - if (!$this->getDatabase()) { - return; - } - - for ($i = 0, $size = count($this->foreignKeys); $i < $size; $i++) { - $fk = $this->foreignKeys[$i]; - $name = $fk->getName(); - if (empty($name)) { - $name = $this->acquireConstraintName('FK', $i + 1); - $fk->setName($name); - } - } - - for ($i = 0, $size = count($this->indices); $i < $size; $i++) { - $index = $this->indices[$i]; - $name = $index->getName(); - if (empty($name)) { - $name = $this->acquireConstraintName('I', $i + 1); - $index->setName($name); - } - } - - for ($i = 0, $size = count($this->unices); $i < $size; $i++) { - $index = $this->unices[$i]; - $name = $index->getName(); - if (empty($name)) { - $name = $this->acquireConstraintName('U', $i + 1); - $index->setName($name); - } - } - - // NOTE: Most RDBMSes can apparently name unique column - // constraints/indices themselves (using MySQL and Oracle - // as test cases), so we'll assume that we needn't add an - // entry to the system name list for these. - } - - /** - * Macro to a constraint name. - * - * @param string $nameType Constraint type - * @param integer $nbr Unique number for this constraint type - * @return string Unique name for constraint - * @throws EngineException - */ - private function acquireConstraintName($nameType, $nbr) - { - return NameFactory::generateName(NameFactory::CONSTRAINT_GENERATOR, [ - $this->database, - $this->getCommonName(), - $nameType, - $nbr - ]); - } - /** * Returns the name of the base class used for superclass of all objects * of this table. @@ -653,7 +588,6 @@ public function addForeignKey($foreignKey) if (!in_array($fk->getForeignTableName(), $this->foreignTableNames)) { $this->foreignTableNames[] = $fk->getForeignTableName(); } - $this->doNaming(); return $fk; } @@ -952,13 +886,20 @@ public function hasIndex($name) * * @param Index|array $index * @return Index + * + * @throw InvalidArgumentException */ public function addIndex($index) { if ($index instanceof Index) { + if ($this->hasIndex($index->getName())) { + throw new InvalidArgumentException(sprintf('Index "%s" already exist.', $index->getName())); + } + if (!$index->getColumns()) { + throw new InvalidArgumentException(sprintf('Index "%s" has no columns.', $index->getName())); + } $index->setTable($this); // force the name to be created if empty. - $index->getName(); $this->indices[] = $index; return $index; @@ -966,6 +907,9 @@ public function addIndex($index) $idx = new Index(); $idx->loadMapping($index); + foreach((array)@$index['columns'] as $column) { + $idx->addColumn($column); + } return $this->addIndex($idx); } @@ -1068,11 +1012,11 @@ public function getName() } /** - * Returns the schema name. + * Returns the schema name from this table or from its database. * * @return string */ - private function guessSchemaName() + public function guessSchemaName() { return $this->schema ?: $this->database->getSchema(); } diff --git a/src/Propel/Generator/Model/Unique.php b/src/Propel/Generator/Model/Unique.php index 7aa2aea1e6..04e793d55b 100644 --- a/src/Propel/Generator/Model/Unique.php +++ b/src/Propel/Generator/Model/Unique.php @@ -34,19 +34,4 @@ public function isUnique() return true; } - /** - * Creates a default name for this index. - * - */ - protected function createName() - { - // ASSUMPTION: This Index not yet added to the list. - $inputs[] = $this->table->getDatabase(); - $inputs[] = $this->table->getCommonName(); - $inputs[] = 'U'; - $inputs[] = count($this->table->getUnices()) + 1; - - // @TODO replace the factory by a real object - $this->name = NameFactory::generateName(NameFactory::CONSTRAINT_GENERATOR, $inputs); - } } diff --git a/src/Propel/Generator/Platform/DefaultPlatform.php b/src/Propel/Generator/Platform/DefaultPlatform.php index dea136a651..f5df5dc9dd 100644 --- a/src/Propel/Generator/Platform/DefaultPlatform.php +++ b/src/Propel/Generator/Platform/DefaultPlatform.php @@ -472,7 +472,7 @@ public function getPrimaryKeyName(Table $table) { $tableName = $table->getCommonName(); - return $tableName . '_PK'; + return $tableName . '_pk'; } /** diff --git a/src/Propel/Generator/Platform/OraclePlatform.php b/src/Propel/Generator/Platform/OraclePlatform.php index 275215afad..ba28430a12 100644 --- a/src/Propel/Generator/Platform/OraclePlatform.php +++ b/src/Propel/Generator/Platform/OraclePlatform.php @@ -188,7 +188,7 @@ public function getPrimaryKeyName(Table $table) // pk constraint name must be 30 chars at most $tableName = substr($tableName, 0, min(27, strlen($tableName))); - return $tableName . '_PK'; + return $tableName . '_pk'; } public function getPrimaryKeyDDL(Table $table) diff --git a/src/Propel/Generator/Platform/SqlitePlatform.php b/src/Propel/Generator/Platform/SqlitePlatform.php index 13768064d9..022f9311d3 100644 --- a/src/Propel/Generator/Platform/SqlitePlatform.php +++ b/src/Propel/Generator/Platform/SqlitePlatform.php @@ -267,31 +267,25 @@ public function getAddTablesDDL(Database $database) /** * Unfortunately, SQLite does not support composite pks where one is AUTOINCREMENT, - * so we have to flag both as NOT NULL and create a UNIQUE constraint. + * so we have to flag both as NOT NULL and create in either way a UNIQUE constraint over pks since + * those UNIQUE is otherwise automatically created by the sqlite engine. * * @param Table $table */ public function normalizeTable(Table $table) { - if (count($pks = $table->getPrimaryKey()) > 1 && $table->hasAutoIncrementPrimaryKey()) { - foreach ($pks as $pk) { - //no pk can be NULL, as usual - $pk->setNotNull(true); - //in SQLite the column with the AUTOINCREMENT MUST be a primary key, too. - if (!$pk->isAutoIncrement()) { - //for all other sub keys we remove it, since we create a UNIQUE constraint over all primary keys. - $pk->setPrimaryKey(false); - } - } - + if ($table->getPrimaryKey()) { //search if there is already a UNIQUE constraint over the primary keys $pkUniqueExist = false; foreach ($table->getUnices() as $unique) { - $allPk = false; + $coversAllPrimaryKeys = true; foreach ($unique->getColumns() as $columnName) { - $allPk &= $table->getColumn($columnName)->isPrimaryKey(); + if (!$table->getColumn($columnName)->isPrimaryKey()) { + $coversAllPrimaryKeys = false; + break; + } } - if ($allPk) { + if ($coversAllPrimaryKeys) { //there's already a unique constraint with the composite pk $pkUniqueExist = true; break; @@ -301,11 +295,24 @@ public function normalizeTable(Table $table) //there is none, let's create it if (!$pkUniqueExist) { $unique = new Unique(); - foreach ($pks as $pk) { + foreach ($table->getPrimaryKey() as $pk) { $unique->addColumn($pk); } $table->addUnique($unique); } + + if ($table->hasAutoIncrementPrimaryKey()) { + foreach ($table->getPrimaryKey() as $pk) { + //no pk can be NULL, as usual + $pk->setNotNull(true); + //in SQLite the column with the AUTOINCREMENT MUST be a primary key, too. + if (!$pk->isAutoIncrement()) { + //for all other sub keys we remove it, since we create a UNIQUE constraint over all primary keys. + $pk->setPrimaryKey(false); + } + } + } + } parent::normalizeTable($table); diff --git a/src/Propel/Generator/Reverse/MssqlSchemaParser.php b/src/Propel/Generator/Reverse/MssqlSchemaParser.php index 93dd256445..f7fe984bf9 100644 --- a/src/Propel/Generator/Reverse/MssqlSchemaParser.php +++ b/src/Propel/Generator/Reverse/MssqlSchemaParser.php @@ -78,7 +78,7 @@ protected function getTypeMapping() return self::$mssqlTypeMap; } - public function parse(Database $database) + public function parse(Database $database, array $additionalTables = array()) { $dataFetcher = $this->dbh->query("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_NAME <> 'dtproperties'"); diff --git a/src/Propel/Generator/Reverse/MysqlSchemaParser.php b/src/Propel/Generator/Reverse/MysqlSchemaParser.php index 308407efde..a811583fd6 100644 --- a/src/Propel/Generator/Reverse/MysqlSchemaParser.php +++ b/src/Propel/Generator/Reverse/MysqlSchemaParser.php @@ -87,18 +87,51 @@ protected function getTypeMapping() } /** - * + * @param Database $database + * @param Table[] $additionalTables + * @return int */ - public function parse(Database $database) + public function parse(Database $database, array $additionalTables = array()) { if ($this->getGeneratorConfig()) { $this->addVendorInfo = $this->getGeneratorConfig()->getBuildProperty('addVendorInfo'); } + $this->parseTables($database); + foreach ($additionalTables as $table) { + $this->parseTables($database, $table); + } + + // Now populate only columns. + foreach ($database->getTables() as $table) { + $this->addColumns($table); + } + + // Now add indices and constraints. + foreach ($database->getTables() as $table) { + $this->addForeignKeys($table); + $this->addIndexes($table); + $this->addPrimaryKey($table); + + $this->addTableVendorInfo($table); + } + + return count($database->getTables()); + } + + protected function parseTables(Database $database, $filterTable = null) + { $sql = 'SHOW FULL TABLES'; - if ($schema = $database->getSchema()) { + + if ($filterTable) { + if ($schema = $filterTable->getSchema()) { + $sql .= ' FROM ' . $database->getPlatform()->quoteIdentifier($schema); + } + $sql .= sprintf(" LIKE '%s'", $filterTable->getCommonName()); + } else if ($schema = $database->getSchema()) { $sql .= ' FROM ' . $database->getPlatform()->quoteIdentifier($schema); } + $dataFetcher = $this->dbh->query($sql); // First load the tables (important that this happen before filling out details of tables) @@ -113,25 +146,12 @@ public function parse(Database $database) $table = new Table($name); $table->setIdMethod($database->getDefaultIdMethod()); + if ($filterTable && $filterTable->getSchema()) { + $table->setSchema($filterTable->getSchema()); + } $database->addTable($table); $tables[] = $table; } - - // Now populate only columns. - foreach ($tables as $table) { - $this->addColumns($table); - } - - // Now add indices and constraints. - foreach ($tables as $table) { - $this->addForeignKeys($table); - $this->addIndexes($table); - $this->addPrimaryKey($table); - - $this->addTableVendorInfo($table); - } - - return count($tables); } /** @@ -266,13 +286,13 @@ protected function addForeignKeys(Table $table) $foreignKeys = array(); // local store to avoid duplicates // Get the information on all the foreign keys - $pattern = '/CONSTRAINT `([^`]+)` FOREIGN KEY \((.+)\) REFERENCES `([^`]*)` \((.+)\)(.*)/'; + $pattern = '/CONSTRAINT `([^`]+)` FOREIGN KEY \((.+)\) REFERENCES `([^\s]+)` \((.+)\)(.*)/'; if (preg_match_all($pattern, $row[1], $matches)) { $tmpArray = array_keys($matches[0]); foreach ($tmpArray as $curKey) { $name = $matches[1][$curKey]; $rawlcol = $matches[2][$curKey]; - $ftbl = $matches[3][$curKey]; + $ftbl = str_replace('`', '', $matches[3][$curKey]); $rawfcol = $matches[4][$curKey]; $fkey = $matches[5][$curKey]; @@ -312,6 +332,10 @@ protected function addForeignKeys(Table $table) $localColumns = array(); $foreignColumns = array(); + if ($table->guessSchemaName() != $database->getSchema() && false == strpos($ftbl, $database->getPlatform()->getSchemaDelimiter())) { + $ftbl = $table->guessSchemaName() . $database->getPlatform()->getSchemaDelimiter() . $ftbl; + } + $foreignTable = $database->getTable($ftbl, true); if (!$foreignTable) { @@ -328,7 +352,9 @@ protected function addForeignKeys(Table $table) if (!isset($foreignKeys[$name])) { $fk = new ForeignKey($name); $fk->setForeignTableCommonName($foreignTable->getCommonName()); - $fk->setForeignSchemaName($foreignTable->getSchema()); + if ($table->guessSchemaName() != $foreignTable->guessSchemaName()) { + $fk->setForeignSchemaName($foreignTable->guessSchemaName()); + } $fk->setOnDelete($fkactions['ON DELETE']); $fk->setOnUpdate($fkactions['ON UPDATE']); $table->addForeignKey($fk); @@ -375,11 +401,6 @@ protected function addIndexes(Table $table) $vi = $this->getNewVendorInfoObject($row); $indexes[$name]->addVendorInfo($vi); } - if ($isUnique) { - $table->addUnique($indexes[$name]); - } else { - $table->addIndex($indexes[$name]); - } } $indexes[$name]->addColumn([ @@ -387,6 +408,14 @@ protected function addIndexes(Table $table) 'size' => $colSize ]); } + + foreach ($indexes as $index) { + if ($index instanceof Unique) { + $table->addUnique($index); + } else { + $table->addIndex($index); + } + } } /** diff --git a/src/Propel/Generator/Reverse/OracleSchemaParser.php b/src/Propel/Generator/Reverse/OracleSchemaParser.php index 7fcd6c1991..5ba45cd2c0 100644 --- a/src/Propel/Generator/Reverse/OracleSchemaParser.php +++ b/src/Propel/Generator/Reverse/OracleSchemaParser.php @@ -76,8 +76,9 @@ protected function getTypeMapping() /** * Searches for tables in the database. Maybe we want to search also the views. * @param Database $database The Database model class to add tables to. + * @param Table[] $additionalTables */ - public function parse(Database $database) + public function parse(Database $database, array $additionalTables = array()) { $tables = array(); $stmt = $this->dbh->query("SELECT OBJECT_NAME FROM USER_OBJECTS WHERE OBJECT_TYPE = 'TABLE'"); diff --git a/src/Propel/Generator/Reverse/PgsqlSchemaParser.php b/src/Propel/Generator/Reverse/PgsqlSchemaParser.php index ed41f12cc5..58bf6b79e1 100644 --- a/src/Propel/Generator/Reverse/PgsqlSchemaParser.php +++ b/src/Propel/Generator/Reverse/PgsqlSchemaParser.php @@ -94,15 +94,59 @@ protected function getTypeMapping() * Parses a database schema. * * @param Database $database + * @param Table[] $additionalTables * @return integer */ - public function parse(Database $database) + public function parse(Database $database, array $additionalTables = array()) + { + $tableWraps = array(); + + $this->parseTables($tableWraps, $database); + foreach ($additionalTables as $table) { + $this->parseTables($tableWraps, $database, $table); + } + + // Now populate only columns. + foreach ($tableWraps as $wrap) { + $this->addColumns($wrap->table, $wrap->oid); + } + + // Now add indexes and constraints. + foreach ($tableWraps as $wrap) { + $this->addForeignKeys($wrap->table, $wrap->oid); + $this->addIndexes($wrap->table, $wrap->oid); + $this->addPrimaryKey($wrap->table, $wrap->oid); + } + + $this->addSequences($database); + + return count($tableWraps); + } + + protected function parseTables(&$tableWraps, Database $database, Table $filterTable = null) { $stmt = null; - $searchPath = '?'; - $params = [$database->getSchema()]; - if (!$database->getSchema()) { + $params = []; + + $sql = " + SELECT c.oid, c.relname, n.nspname + FROM pg_class c join pg_namespace n on (c.relnamespace=n.oid) + WHERE c.relkind = 'r' + AND n.nspname NOT IN ('information_schema','pg_catalog') + AND n.nspname NOT LIKE 'pg_temp%' + AND n.nspname NOT LIKE 'pg_toast%'"; + + if ($filterTable) { + if ($schema = $filterTable->getSchema()) { + $sql .= ' AND n.nspname = ?'; + $params[] = $schema; + } + + $sql .= ' AND c.relname = ?'; + $params[] = $filterTable->getCommonName(); + + } else if (!$database->getSchema()) { $stmt = $this->dbh->query('SHOW search_path'); $searchPathString = $stmt->fetchColumn(); @@ -114,23 +158,21 @@ public function parse(Database $database) $path = '?'; } $searchPath = implode(', ', $searchPath); + $sql .= " + AND n.nspname IN ($searchPath)"; + + } elseif ($database->getSchema()) { + $sql .= " + AND n.nspname = ?"; + $params[] = $database->getSchema(); } - $stmt = $this->dbh->prepare("SELECT c.oid, - c.relname, n.nspname - FROM pg_class c join pg_namespace n on (c.relnamespace=n.oid) - WHERE c.relkind = 'r' - AND n.nspname NOT IN ('information_schema','pg_catalog') - AND n.nspname NOT LIKE 'pg_temp%' - AND n.nspname NOT LIKE 'pg_toast%' - AND n.nspname IN ($searchPath) - ORDER BY relname" - ); + $sql .= " + ORDER BY relname"; + $stmt = $this->dbh->prepare($sql); $stmt->execute($params); - $tableWraps = array(); - // First load the tables (important that this happen before filling out details of tables) while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { $name = $row['relname']; @@ -153,21 +195,6 @@ public function parse(Database $database) $tableWraps[] = $wrap; } - // Now populate only columns. - foreach ($tableWraps as $wrap) { - $this->addColumns($wrap->table, $wrap->oid); - } - - // Now add indexes and constraints. - foreach ($tableWraps as $wrap) { - $this->addForeignKeys($wrap->table, $wrap->oid); - $this->addIndexes($wrap->table, $wrap->oid); - $this->addPrimaryKey($wrap->table, $wrap->oid); - } - - $this->addSequences($database); - - return count($tableWraps); } /** @@ -183,7 +210,11 @@ protected function addColumns(Table $table, $oid) $searchPath = '?'; $params = [$table->getDatabase()->getSchema()]; - if (!$table->getDatabase()->getSchema()) { + + if ($schema = $table->getSchema()) { + $searchPath = '?'; + $params = [$schema]; + } else if (!$table->getDatabase()->getSchema()) { $stmt = $this->dbh->query('SHOW search_path'); $searchPathString = $stmt->fetchColumn(); @@ -208,10 +239,9 @@ protected function addColumns(Table $table, $oid) character_maximum_length FROM information_schema.columns WHERE - table_schema IN($searchPath) AND table_name = ? + table_schema IN ($searchPath) AND table_name = ? "); - $stmt->bindValue(1, $oid, \PDO::PARAM_INT); $params[] = $table->getCommonName(); $stmt->execute($params); @@ -326,7 +356,7 @@ protected function addForeignKeys(Table $table, $oid) $name = $row['conname']; $localTable = $row['fktab']; $localColumns = explode(',', trim($row['fkcols'], '{}')); - $foreignTable = $row['reftab']; + $foreignTableName = $row['reftab']; $foreignColumns = explode(',', trim($row['refcols'], '{}')); // On Update @@ -370,13 +400,19 @@ protected function addForeignKeys(Table $table, $oid) break; } - $foreignTable = $database->getTable($foreignTable); + $foreignTable = $database->getTable($foreignTableName); $localTable = $database->getTable($localTable); + if (!$foreignTable) { + continue; + } + if (!isset($foreignKeys[$name])) { $fk = new ForeignKey($name); $fk->setForeignTableCommonName($foreignTable->getCommonName()); - $fk->setForeignSchemaName($foreignTable->getSchema()); + if ($table->guessSchemaName() != $foreignTable->guessSchemaName()) { + $fk->setForeignSchemaName($foreignTable->getSchema()); + } $fk->setOnDelete($ondelete); $fk->setOnUpdate($onupdate); $table->addForeignKey($fk); @@ -425,10 +461,8 @@ protected function addIndexes(Table $table, $oid) if (!isset($indexes[$name])) { if ($unique) { $indexes[$name] = new Unique($name); - $table->addUnique($indexes[$name]); } else { $indexes[$name] = new Index($name); - $table->addIndex($indexes[$name]); } } @@ -444,6 +478,14 @@ protected function addIndexes(Table $table, $oid) } } + + foreach ($indexes as $index) { + if ($index instanceof Unique) { + $table->addUnique($index); + } else { + $table->addIndex($index); + } + } } /** @@ -478,6 +520,9 @@ protected function addPrimaryKey(Table $table, $oid) $stmt2->execute(); $row2 = $stmt2->fetch(\PDO::FETCH_ASSOC); + if (!$table->getColumn($row2['attname'])) { + continue; + } $table->getColumn($row2['attname'])->setPrimaryKey(true); } } diff --git a/src/Propel/Generator/Reverse/SchemaParserInterface.php b/src/Propel/Generator/Reverse/SchemaParserInterface.php index e4a447c727..9aa694322c 100644 --- a/src/Propel/Generator/Reverse/SchemaParserInterface.php +++ b/src/Propel/Generator/Reverse/SchemaParserInterface.php @@ -12,6 +12,7 @@ use Propel\Generator\Config\GeneratorConfigInterface; use Propel\Generator\Model\Database; +use Propel\Generator\Model\Table; use Propel\Runtime\Connection\ConnectionInterface; /** @@ -69,7 +70,8 @@ public function setPlatform($platform); * Parse the schema and populate passed-in Database model object. * * @param Database $database + * @param Table[] $additionalTables additional tables to parse and add to $database * @return int number of generated tables */ - public function parse(Database $database); + public function parse(Database $database, array $additionalTables = array()); } diff --git a/src/Propel/Generator/Reverse/SqliteSchemaParser.php b/src/Propel/Generator/Reverse/SqliteSchemaParser.php index 259dcaa4c0..b281c3a8bd 100644 --- a/src/Propel/Generator/Reverse/SqliteSchemaParser.php +++ b/src/Propel/Generator/Reverse/SqliteSchemaParser.php @@ -80,64 +80,93 @@ protected function getTypeMapping() /** * */ - public function parse(Database $database) + public function parse(Database $database, array $additionalTables = array()) { - $dataFetcher = $this->dbh->query(" + if ($this->getGeneratorConfig()) { + $this->addVendorInfo = $this->getGeneratorConfig()->getBuildProperty('addVendorInfo'); + } + + $this->parseTables($database); + + foreach ($additionalTables as $table) { + $this->parseTables($database, $table); + } + + // Now populate only columns. + foreach ($database->getTables() as $table) { + $this->addColumns($table); + } + + // Now add indexes and constraints. + foreach ($database->getTables() as $table) { + $this->addIndexes($table); + $this->addForeignKeys($table); + } + + return count($database->getTables()); + } + + protected function parseTables(Database $database, Table $filterTable = null) + { + $sql = " SELECT name FROM sqlite_master WHERE type='table' + %filter% UNION ALL SELECT name FROM sqlite_temp_master WHERE type='table' - ORDER BY name;"); + %filter% + ORDER BY name;"; + + $filter = ''; + + if ($filterTable) { + if ($schema = $filterTable->getSchema()) { + $filter = sprintf(" AND name LIKE '%s§%%'", $schema); + } + $filter .= sprintf(" AND (name = '%s' OR name LIKE '%%§%1\$s')", $filterTable->getCommonName()); + } else if ($schema = $database->getSchema()) { + $filter = sprintf(" AND name LIKE '%s§%%'", $schema); + } + + $sql = str_replace('%filter%', $filter, $sql); + + $dataFetcher = $this->dbh->query($sql); // First load the tables (important that this happen before filling out details of tables) - $tables = array(); foreach ($dataFetcher as $row) { - $name = $row[0]; - $commonName = ''; + $tableName = $row[0]; + $tableSchema = ''; - if ('sqlite_' == substr($name, 0, 7)) { + if ('sqlite_' == substr($tableName, 0, 7)) { continue; } - if ($database->getSchema()) { - if (false !== ($pos = strpos($name, '§'))) { - if ($database->getSchema()) { - if ($database->getSchema() !== substr($name, 0, $pos)) { - continue; - } else { - $commonName = substr($name, $pos+2); //2 because the delimiter § uses in UTF8 one byte more. - } - } - } else { + if (false !== ($pos = strpos($tableName, '§'))) { + $tableSchema = substr($tableName, 0, $pos); + $tableName = substr($tableName, $pos + 2); + } + + $table = new Table($tableName); + + if ($filterTable && $filterTable->getSchema()) { + $table->setSchema($filterTable->getSchema()); + } else { + if (!$database->getSchema() && $tableSchema) { + //we have no schema to filter, but this belongs to one, so next continue; } } - if ($name === $this->getMigrationTable()) { + if ($tableName === $this->getMigrationTable()) { continue; } - $table = new Table($commonName ?: $name); $table->setIdMethod($database->getDefaultIdMethod()); $database->addTable($table); - $tables[] = $table; - } - - // Now populate only columns. - foreach ($tables as $table) { - $this->addColumns($table); } - - // Now add indexes and constraints. - foreach ($tables as $table) { - $this->addIndexes($table); - $this->addForeignKeys($table); - } - - return count($tables); } /** @@ -185,12 +214,21 @@ protected function addColumns(Table $table) $column->getDomain()->replaceScale($scale); if (null !== $default) { - $column->getDomain()->setDefaultValue(new ColumnDefaultValue($default, ColumnDefaultValue::TYPE_VALUE)); + if ("'" !== substr($default, 0, 1) && strpos($default, '(')) { + $defaultType = ColumnDefaultValue::TYPE_EXPR; + if ('datetime(CURRENT_TIMESTAMP, \'localtime\')' === $default) { + $default = 'CURRENT_TIMESTAMP'; + } + } else { + $defaultType = ColumnDefaultValue::TYPE_VALUE; + $default = str_replace("'", '', $default); + } + $column->getDomain()->setDefaultValue(new ColumnDefaultValue($default, $defaultType)); } $column->setNotNull($notNull); - if (1 == $row['pk']) { + if (0 < $row['pk']+0) { $column->setPrimaryKey(true); } @@ -217,6 +255,8 @@ protected function addColumns(Table $table) protected function addForeignKeys(Table $table) { + $database = $table->getDatabase(); + $stmt = $this->dbh->query('PRAGMA foreign_key_list("' . $table->getName() . '")'); $lastId = null; @@ -224,22 +264,27 @@ protected function addForeignKeys(Table $table) if ($lastId !== $row['id']) { $fk = new ForeignKey(); - $tableName = $row['table']; - $tableSchema = ''; + $onDelete = $row['on_delete']; + if ($onDelete && 'NO ACTION' !== $onDelete) { + $fk->setOnDelete($onDelete); + } - if (false !== ($pos = strpos($tableName, '§'))) { - $tableName = substr($tableName, $pos + 2); - $tableSchema = substr($tableName, 0, $pos); + $onUpdate = $row['on_update']; + if ($onUpdate && 'NO ACTION' !== $onUpdate) { + $fk->setOnUpdate($onUpdate); } - $fk->setForeignTableCommonName($tableName); - if ($table->getDatabase()->getSchema() != $tableSchema) { - $fk->setForeignSchemaName($tableSchema); + $foreignTable = $database->getTable($row['table'], true); + + if (!$foreignTable) { + continue; } - $fk->setOnDelete($row['on_delete']); - $fk->setOnUpdate($row['on_update']); $table->addForeignKey($fk); + $fk->setForeignTableCommonName($foreignTable->getCommonName()); + if ($table->guessSchemaName() != $foreignTable->guessSchemaName()) { + $fk->setForeignSchemaName($foreignTable->guessSchemaName()); + } $lastId = $row['id']; } diff --git a/src/Propel/Generator/Util/QuickBuilder.php b/src/Propel/Generator/Util/QuickBuilder.php index 0fbcc7f621..9e1d26632a 100644 --- a/src/Propel/Generator/Util/QuickBuilder.php +++ b/src/Propel/Generator/Util/QuickBuilder.php @@ -210,7 +210,8 @@ public function getDatabase() public function buildSQL(ConnectionInterface $con) { - $statements = SqlParser::parseString($this->getSQL()); + $sql = $this->getSQL(); + $statements = SqlParser::parseString($sql); foreach ($statements as $statement) { if (strpos($statement, 'DROP') === 0) { // drop statements cause errors since the table doesn't exist @@ -222,7 +223,7 @@ public function buildSQL(ConnectionInterface $con) // only execute if has no error $stmt->execute(); } - } catch (\PDOException $e) { + } catch (\Exception $e) { throw new \Exception('SQL failed: ' . $statement, 0, $e); } } diff --git a/tests/Fixtures/schemas/schema.xml b/tests/Fixtures/schemas/schema.xml index a568c5a4e9..d66903b2b1 100644 --- a/tests/Fixtures/schemas/schema.xml +++ b/tests/Fixtures/schemas/schema.xml @@ -77,7 +77,6 @@ - diff --git a/tests/Propel/Tests/Generator/Behavior/Archivable/ArchivableBehaviorTest.php b/tests/Propel/Tests/Generator/Behavior/Archivable/ArchivableBehaviorTest.php index 4bf609c94d..e1b59cced3 100644 --- a/tests/Propel/Tests/Generator/Behavior/Archivable/ArchivableBehaviorTest.php +++ b/tests/Propel/Tests/Generator/Behavior/Archivable/ArchivableBehaviorTest.php @@ -152,14 +152,14 @@ public function testDoesNotCopyForeignKeys() public function testCopiesIndices() { $table = \Map\ArchivableTest1ArchiveTableMap::getTableMap(); - $expected = "CREATE INDEX archivable_test_1_archive_I_1 ON archivable_test_1_archive (title,age);"; + $expected = "CREATE INDEX archivable_test_1_archive_i_6c947f ON archivable_test_1_archive (title,age);"; $this->assertContains($expected, self::$generatedSQL); } public function testCopiesUniquesToIndices() { $table = \Map\ArchivableTest2ArchiveTableMap::getTableMap(); - $expected = "CREATE INDEX my_old_archivable_test_3_I_1 ON my_old_archivable_test_3 (title);"; + $expected = "CREATE INDEX my_old_archivable_test_3_i_639136 ON my_old_archivable_test_3 (title);"; $this->assertContains($expected, self::$generatedSQL); } diff --git a/tests/Propel/Tests/Generator/Behavior/I18n/I18nBehaviorTest.php b/tests/Propel/Tests/Generator/Behavior/I18n/I18nBehaviorTest.php index 9ba1e00302..15dc4dc01c 100644 --- a/tests/Propel/Tests/Generator/Behavior/I18n/I18nBehaviorTest.php +++ b/tests/Propel/Tests/Generator/Behavior/I18n/I18nBehaviorTest.php @@ -50,6 +50,7 @@ public function testModifyDatabaseOverridesDefaultLocale() id INTEGER NOT NULL, locale VARCHAR(5) DEFAULT 'fr_FR' NOT NULL, PRIMARY KEY (id,locale), + UNIQUE (id,locale), FOREIGN KEY (id) REFERENCES i18n_behavior_test_0 (id) ON DELETE CASCADE ); @@ -86,6 +87,7 @@ public function testModifyDatabaseDoesNotOverrideTableLocale() id INTEGER NOT NULL, locale VARCHAR(5) DEFAULT 'pt_PT' NOT NULL, PRIMARY KEY (id,locale), + UNIQUE (id,locale), FOREIGN KEY (id) REFERENCES i18n_behavior_test_0 (id) ON DELETE CASCADE ); @@ -210,6 +212,7 @@ public function testModifyTableMovesI18nColumns($schema) locale VARCHAR(5) DEFAULT 'en_US' NOT NULL, bar VARCHAR(100), PRIMARY KEY (id,locale), + UNIQUE (id,locale), FOREIGN KEY (id) REFERENCES i18n_behavior_test_0 (id) EOF; $this->assertContains($expected, $builder->getSQL()); @@ -226,7 +229,8 @@ public function testModifyTableDoesNotMoveNonI18nColumns($schema) CREATE TABLE i18n_behavior_test_0 ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, - foo INTEGER + foo INTEGER, + UNIQUE (id) ); EOF; $this->assertContains($expected, $builder->getSQL()); @@ -258,6 +262,7 @@ public function testModiFyTableUsesCustomI18nTableName() id INTEGER NOT NULL, locale VARCHAR(5) DEFAULT 'en_US' NOT NULL, PRIMARY KEY (id,locale), + UNIQUE (id,locale), FOREIGN KEY (id) REFERENCES i18n_behavior_test_0 (id) ON DELETE CASCADE ); @@ -291,6 +296,7 @@ public function testModiFyTableUsesCustomLocaleColumnName() id INTEGER NOT NULL, culture VARCHAR(5) DEFAULT 'en_US' NOT NULL, PRIMARY KEY (id,culture), + UNIQUE (id,culture), FOREIGN KEY (id) REFERENCES i18n_behavior_test_0 (id) ON DELETE CASCADE ); @@ -324,6 +330,7 @@ public function testModiFyTableUsesCustomLocaleDefault() id INTEGER NOT NULL, locale VARCHAR(5) DEFAULT 'fr_FR' NOT NULL, PRIMARY KEY (id,locale), + UNIQUE (id,locale), FOREIGN KEY (id) REFERENCES i18n_behavior_test_0 (id) ON DELETE CASCADE ); @@ -357,6 +364,7 @@ public function testModiFyTableUsesCustomI18nLocaleLength() id INTEGER NOT NULL, locale VARCHAR(6) DEFAULT 'en_US' NOT NULL, PRIMARY KEY (id,locale), + UNIQUE (id,locale), FOREIGN KEY (id) REFERENCES i18n_behavior_test_0 (id) ON DELETE CASCADE ); diff --git a/tests/Propel/Tests/Generator/Behavior/Versionable/VersionableBehaviorTest.php b/tests/Propel/Tests/Generator/Behavior/Versionable/VersionableBehaviorTest.php index c0130c070f..4cb792ee6f 100644 --- a/tests/Propel/Tests/Generator/Behavior/Versionable/VersionableBehaviorTest.php +++ b/tests/Propel/Tests/Generator/Behavior/Versionable/VersionableBehaviorTest.php @@ -52,7 +52,8 @@ public function testModifyTableAddsVersionColumn($schema) ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, bar INTEGER, - version INTEGER DEFAULT 0 + version INTEGER DEFAULT 0, + UNIQUE (id) ); EOF; $this->assertContains($expected, $builder->getSQL()); @@ -84,7 +85,8 @@ public function testModifyTableAddsVersionColumnCustomName() ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, bar INTEGER, - foo_ver INTEGER DEFAULT 0 + foo_ver INTEGER DEFAULT 0, + UNIQUE (id) ); EOF; $this->assertContains($expected, $builder->getSQL()); @@ -115,7 +117,8 @@ public function testModifyTableDoesNotAddVersionColumnIfExists() ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, bar INTEGER, - version BIGINT + version BIGINT, + UNIQUE (id) ); EOF; $this->assertContains($expected, $builder->getSQL()); @@ -165,6 +168,7 @@ public function testModifyTableAddsVersionColumnForForeignKeysIfForeignTableIsVe bar INTEGER, foreign_id INTEGER, version INTEGER DEFAULT 0, + UNIQUE (id), FOREIGN KEY (foreign_id) REFERENCES versionable_behavior_test_1 (id) ); EOF; @@ -185,6 +189,7 @@ public function testModifyTableAddsVersionColumnForForeignKeysIfForeignTableIsVe version INTEGER DEFAULT 0 NOT NULL, foreign_id_version INTEGER DEFAULT 0, PRIMARY KEY (id,version), + UNIQUE (id,version), FOREIGN KEY (id) REFERENCES versionable_behavior_test_0 (id) ON DELETE CASCADE ); @@ -210,7 +215,8 @@ public function testModifyTableAddsVersionColumnForReferrersIfForeignTableIsVers ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, bar INTEGER, - version INTEGER DEFAULT 0 + version INTEGER DEFAULT 0, + UNIQUE (id) ); EOF; $this->assertContains($expected, $builder->getSQL()); @@ -230,6 +236,7 @@ public function testModifyTableAddsVersionColumnForReferrersIfForeignTableIsVers versionable_behavior_test_0_ids MEDIUMTEXT, versionable_behavior_test_0_versions MEDIUMTEXT, PRIMARY KEY (id,version), + UNIQUE (id,version), FOREIGN KEY (id) REFERENCES versionable_behavior_test_1 (id) ON DELETE CASCADE ); @@ -257,6 +264,7 @@ public function testModifyTableAddsVersionTable($schema) bar INTEGER, version INTEGER DEFAULT 0 NOT NULL, PRIMARY KEY (id,version), + UNIQUE (id,version), FOREIGN KEY (id) REFERENCES versionable_behavior_test_0 (id) ON DELETE CASCADE ); @@ -292,6 +300,7 @@ public function testModifyTableAddsVersionTableCustomName() bar INTEGER, version INTEGER DEFAULT 0 NOT NULL, PRIMARY KEY (id,version), + UNIQUE (id,version), FOREIGN KEY (id) REFERENCES versionable_behavior_test_0 (id) ON DELETE CASCADE ); @@ -328,7 +337,8 @@ public function testModifyTableDoesNotAddVersionTableIfExists() ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, bar INTEGER, - version INTEGER DEFAULT 0 + version INTEGER DEFAULT 0, + UNIQUE (id) ); ----------------------------------------------------------------------- @@ -340,7 +350,8 @@ public function testModifyTableDoesNotAddVersionTableIfExists() CREATE TABLE versionable_behavior_test_0_version ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, - baz INTEGER + baz INTEGER, + UNIQUE (id) ); EOF; @@ -387,7 +398,8 @@ public function testModifyTableAddsLogColumns($schema) version INTEGER DEFAULT 0, version_created_at TIMESTAMP, version_created_by VARCHAR(100), - version_comment VARCHAR(255) + version_comment VARCHAR(255), + UNIQUE (id) ); EOF; $this->assertContains($expected, $builder->getSQL()); @@ -416,6 +428,7 @@ public function testModifyTableAddsVersionTableLogColumns($schema) version_created_by VARCHAR(100), version_comment VARCHAR(255), PRIMARY KEY (id,version), + UNIQUE (id,version), FOREIGN KEY (id) REFERENCES versionable_behavior_test_0 (id) ON DELETE CASCADE ); @@ -447,6 +460,7 @@ public function testDatabaseLevelBehavior() bar INTEGER, version INTEGER DEFAULT 0 NOT NULL, PRIMARY KEY (id,version), + UNIQUE (id,version), FOREIGN KEY (id) REFERENCES versionable_behavior_test_0 (id) ON DELETE CASCADE ); diff --git a/tests/Propel/Tests/Generator/Migration/MigrationTestCase.php b/tests/Propel/Tests/Generator/Migration/MigrationTestCase.php index 9108b6d4b8..0145089f3b 100644 --- a/tests/Propel/Tests/Generator/Migration/MigrationTestCase.php +++ b/tests/Propel/Tests/Generator/Migration/MigrationTestCase.php @@ -135,13 +135,13 @@ public function migrateAndTest($originXml, $targetXml) try { $this->applyXmlAndTest($originXml); } catch (BuildException $e) { - throw new BuildException('There was a exception in initialing the first(origin) schema', 0, $e); + throw new BuildException('There was a exception in applying the first(origin) schema', 0, $e); } try { $this->applyXmlAndTest($targetXml, true); } catch (BuildException $e) { - throw new BuildException('There was a exception in initialing the second(target) schema', 0, $e); + throw new BuildException('There was a exception in applying the second(target) schema', 0, $e); } } diff --git a/tests/Propel/Tests/Generator/Model/Diff/PropelDatabaseTableComparatorTest.php b/tests/Propel/Tests/Generator/Model/Diff/PropelDatabaseTableComparatorTest.php index 74bc2fabf3..8eded77121 100644 --- a/tests/Propel/Tests/Generator/Model/Diff/PropelDatabaseTableComparatorTest.php +++ b/tests/Propel/Tests/Generator/Model/Diff/PropelDatabaseTableComparatorTest.php @@ -270,6 +270,48 @@ public function testCompareModifiedTable() $this->assertEquals(array('Foo_Table' => $tableDiff), $databaseDiff->getModifiedTables()); } + public function testCompareRenamedTable() + { + $d1 = new Database(); + $t1 = new Table('Foo_Table'); + $c1 = new Column('Foo'); + $c1->getDomain()->copy($this->platform->getDomainForType('DOUBLE')); + $c1->getDomain()->replaceScale(2); + $c1->getDomain()->replaceSize(3); + $c1->setNotNull(true); + $c1->getDomain()->setDefaultValue(new ColumnDefaultValue(123, ColumnDefaultValue::TYPE_VALUE)); + $t1->addColumn($c1); + $d1->addTable($t1); + $t2 = new Table('Bar'); + $d1->addTable($t2); + + $d2 = new Database(); + $t3 = new Table('Foo_Table2'); + $c3 = new Column('Foo'); + $c3->getDomain()->copy($this->platform->getDomainForType('DOUBLE')); + $c3->getDomain()->replaceScale(2); + $c3->getDomain()->replaceSize(3); + $c3->setNotNull(true); + $c3->getDomain()->setDefaultValue(new ColumnDefaultValue(123, ColumnDefaultValue::TYPE_VALUE)); + $t3->addColumn($c3); + $d2->addTable($t3); + $t4 = new Table('Bar'); + $d2->addTable($t4); + + $dc = new DatabaseComparator(); + $dc->setFromDatabase($d1); + $dc->setToDatabase($d2); + $dc->setWithRenaming(true); + $nbDiffs = $dc->compareTables(); + $databaseDiff = $dc->getDatabaseDiff(); + $this->assertEquals(1, $nbDiffs); + $this->assertEquals(1, count($databaseDiff->getRenamedTables())); + $this->assertEquals(array('Foo_Table' => 'Foo_Table2'), $databaseDiff->getRenamedTables()); + $this->assertEquals(array(), $databaseDiff->getAddedTables()); + $this->assertEquals(array(), $databaseDiff->getRemovedTables()); + } + + public function testCompareSeveralTableDifferences() { $d1 = new Database(); diff --git a/tests/Propel/Tests/Generator/Model/Diff/PropelTableForeignKeyComparatorTest.php b/tests/Propel/Tests/Generator/Model/Diff/PropelTableForeignKeyComparatorTest.php index f36ddc3422..4e1355fd62 100644 --- a/tests/Propel/Tests/Generator/Model/Diff/PropelTableForeignKeyComparatorTest.php +++ b/tests/Propel/Tests/Generator/Model/Diff/PropelTableForeignKeyComparatorTest.php @@ -78,7 +78,8 @@ public function testCaseInsensitive() $fk2->addReference($c3, $c4); $t2->addForeignKey($fk2); - $this->assertFalse(TableComparator::computeDiff($t1, $t2, true)); + $diff = TableComparator::computeDiff($t1, $t2, true); + $this->assertFalse($diff); } public function testCompareAddedFks() @@ -87,7 +88,6 @@ public function testCompareAddedFks() $db1->setPlatform($this->platform); $t1 = new Table('Baz'); $db1->addTable($t1); - $t1->doNaming(); $db2 = new Database(); $db2->setPlatform($this->platform); @@ -98,7 +98,6 @@ public function testCompareAddedFks() $t2 = new Table('Baz'); $t2->addForeignKey($fk2); $db2->addTable($t2); - $t2->doNaming(); $tc = new TableComparator(); $tc->setFromTable($t1); @@ -107,7 +106,7 @@ public function testCompareAddedFks() $tableDiff = $tc->getTableDiff(); $this->assertEquals(1, $nbDiffs); $this->assertEquals(1, count($tableDiff->getAddedFks())); - $this->assertEquals(array('Baz_FK_1' => $fk2), $tableDiff->getAddedFks()); + $this->assertEquals(array('Baz_fk_9c94ed' => $fk2), $tableDiff->getAddedFks()); } public function testCompareRemovedFks() @@ -121,13 +120,11 @@ public function testCompareRemovedFks() $t1 = new Table('Baz'); $t1->addForeignKey($fk1); $db1->addTable($t1); - $t1->doNaming(); $db2 = new Database(); $db2->setPlatform($this->platform); $t2 = new Table('Baz'); $db2->addTable($t2); - $t2->doNaming(); $tc = new TableComparator(); $tc->setFromTable($t1); @@ -136,7 +133,7 @@ public function testCompareRemovedFks() $tableDiff = $tc->getTableDiff(); $this->assertEquals(1, $nbDiffs); $this->assertEquals(1, count($tableDiff->getRemovedFks())); - $this->assertEquals(array('Baz_FK_1' => $fk1), $tableDiff->getRemovedFks()); + $this->assertEquals(array('Baz_fk_9c94ed' => $fk1), $tableDiff->getRemovedFks()); } public function testCompareModifiedFks() @@ -145,23 +142,21 @@ public function testCompareModifiedFks() $db1->setPlatform($this->platform); $c1 = new Column('Foo'); $c2 = new Column('Bar'); - $fk1 = new ForeignKey(); + $fk1 = new ForeignKey('my_foreign_key'); $fk1->addReference($c1, $c2); $t1 = new Table('Baz'); $t1->addForeignKey($fk1); $db1->addTable($t1); - $t1->doNaming(); $db2 = new Database(); $db2->setPlatform($this->platform); $c3 = new Column('Foo'); $c4 = new Column('Bar2'); - $fk2 = new ForeignKey(); + $fk2 = new ForeignKey('my_foreign_key'); $fk2->addReference($c3, $c4); $t2 = new Table('Baz'); $t2->addForeignKey($fk2); $db2->addTable($t2); - $t2->doNaming(); $tc = new TableComparator(); $tc->setFromTable($t1); @@ -170,6 +165,6 @@ public function testCompareModifiedFks() $tableDiff = $tc->getTableDiff(); $this->assertEquals(1, $nbDiffs); $this->assertEquals(1, count($tableDiff->getModifiedFks())); - $this->assertEquals(array('Baz_FK_1' => array($fk1, $fk2)), $tableDiff->getModifiedFks()); + $this->assertEquals(array('my_foreign_key' => array($fk1, $fk2)), $tableDiff->getModifiedFks()); } } diff --git a/tests/Propel/Tests/Generator/Model/IndexTest.php b/tests/Propel/Tests/Generator/Model/IndexTest.php index 8b762e2cc8..f5ce6da894 100644 --- a/tests/Propel/Tests/Generator/Model/IndexTest.php +++ b/tests/Propel/Tests/Generator/Model/IndexTest.php @@ -69,8 +69,8 @@ public function testCreateDefaultIndexName($tableName, $maxColumnNameLength, $in public function provideTableSpecificAttributes() { return [ - [ 'books', 64, 'books_I_3' ], - [ 'super_long_table_name', 16, 'super_long_t_I_3' ], + [ 'books', 64, 'books_i_no_columns' ], + [ 'super_long_table_name', 16, 'super_long_table' ], ]; } diff --git a/tests/Propel/Tests/Generator/Model/ModelTestCase.php b/tests/Propel/Tests/Generator/Model/ModelTestCase.php index 37b41dde4a..5589724ed0 100644 --- a/tests/Propel/Tests/Generator/Model/ModelTestCase.php +++ b/tests/Propel/Tests/Generator/Model/ModelTestCase.php @@ -9,6 +9,7 @@ */ namespace Propel\Tests\Generator\Model; +use Propel\Generator\Model\Index; use Propel\Tests\TestCase; /** @@ -168,7 +169,7 @@ protected function getIndexMock($name = null, array $options = array()) ->method('setTable') ; $index - ->expects($this->once()) + ->expects($this->any()) ->method('getName') ->will($this->returnValue($name)) ; diff --git a/tests/Propel/Tests/Generator/Model/TableTest.php b/tests/Propel/Tests/Generator/Model/TableTest.php index 3f936c8d44..9c38b4ca28 100644 --- a/tests/Propel/Tests/Generator/Model/TableTest.php +++ b/tests/Propel/Tests/Generator/Model/TableTest.php @@ -12,6 +12,7 @@ use Propel\Generator\Model\Column; use Propel\Generator\Model\Database; +use Propel\Generator\Model\Index; use Propel\Generator\Model\Table; /** @@ -504,7 +505,20 @@ public function testSetInterface() public function testAddIndex() { $table = new Table(); - $table->addIndex($this->getIndexMock('author_idx')); + $index = new Index(); + $index->addColumn(['name' => 'bla']); + $table->addIndex($index); + + $this->assertCount(1, $table->getIndices()); + } + + /** + * @expectedException \Propel\Generator\Exception\InvalidArgumentException + */ + public function testAddEmptyIndex() + { + $table = new Table(); + $table->addIndex(new Index()); $this->assertCount(1, $table->getIndices()); } @@ -512,7 +526,7 @@ public function testAddIndex() public function testAddArrayIndex() { $table = new Table(); - $table->addIndex(array('name' => 'author_idx')); + $table->addIndex(array('name' => 'author_idx', 'columns' => [['name' => 'bla']])); $this->assertCount(1, $table->getIndices()); } diff --git a/tests/Propel/Tests/Generator/Model/UniqueTest.php b/tests/Propel/Tests/Generator/Model/UniqueTest.php index f1536f06fc..3b2930a7f4 100644 --- a/tests/Propel/Tests/Generator/Model/UniqueTest.php +++ b/tests/Propel/Tests/Generator/Model/UniqueTest.php @@ -48,8 +48,8 @@ public function testCreateDefaultUniqueIndexName($tableName, $maxColumnNameLengt public function provideTableSpecificAttributes() { return [ - [ 'books', 64, 'books_U_3' ], - [ 'super_long_table_name', 16, 'super_long_t_U_3' ], + [ 'books', 64, 'books_u_no_columns' ], + [ 'super_long_table_name', 16, 'super_long_table' ], ]; } } diff --git a/tests/Propel/Tests/Generator/Platform/MssqlPlatformTest.php b/tests/Propel/Tests/Generator/Platform/MssqlPlatformTest.php index 5b45060963..5241c22121 100644 --- a/tests/Propel/Tests/Generator/Platform/MssqlPlatformTest.php +++ b/tests/Propel/Tests/Generator/Platform/MssqlPlatformTest.php @@ -64,8 +64,8 @@ public function testGetAddTablesDDL($schema) -- book ----------------------------------------------------------------------- -IF EXISTS (SELECT 1 FROM sysobjects WHERE type ='RI' AND name='book_FK_1') - ALTER TABLE book DROP CONSTRAINT book_FK_1; +IF EXISTS (SELECT 1 FROM sysobjects WHERE type ='RI' AND name='book_fk_ea464c') + ALTER TABLE book DROP CONSTRAINT book_fk_ea464c; IF EXISTS (SELECT 1 FROM sysobjects WHERE type = 'U' AND name = 'book') BEGIN @@ -97,13 +97,13 @@ public function testGetAddTablesDDL($schema) id INT NOT NULL IDENTITY, title VARCHAR(255) NOT NULL, author_id INT NULL, - CONSTRAINT book_PK PRIMARY KEY (id) + CONSTRAINT book_pk PRIMARY KEY (id) ); -CREATE INDEX book_I_1 ON book (title); +CREATE INDEX book_i_639136 ON book (title); BEGIN -ALTER TABLE book ADD CONSTRAINT book_FK_1 FOREIGN KEY (author_id) REFERENCES author (id) +ALTER TABLE book ADD CONSTRAINT book_fk_ea464c FOREIGN KEY (author_id) REFERENCES author (id) END ; @@ -141,7 +141,7 @@ public function testGetAddTablesDDL($schema) id INT NOT NULL IDENTITY, first_name VARCHAR(100) NULL, last_name VARCHAR(100) NULL, - CONSTRAINT author_PK PRIMARY KEY (id) + CONSTRAINT author_pk PRIMARY KEY (id) ); EOF; @@ -160,8 +160,8 @@ public function testGetAddTablesDDLSchemas($schema) -- x.book ----------------------------------------------------------------------- -IF EXISTS (SELECT 1 FROM sysobjects WHERE type ='RI' AND name='book_FK_1') - ALTER TABLE x.book DROP CONSTRAINT book_FK_1; +IF EXISTS (SELECT 1 FROM sysobjects WHERE type ='RI' AND name='book_fk_4444ca') + ALTER TABLE x.book DROP CONSTRAINT book_fk_4444ca; IF EXISTS (SELECT 1 FROM sysobjects WHERE type = 'U' AND name = 'x.book') BEGIN @@ -193,13 +193,13 @@ public function testGetAddTablesDDLSchemas($schema) id INT NOT NULL IDENTITY, title VARCHAR(255) NOT NULL, author_id INT NULL, - CONSTRAINT book_PK PRIMARY KEY (id) + CONSTRAINT book_pk PRIMARY KEY (id) ); -CREATE INDEX book_I_1 ON x.book (title); +CREATE INDEX book_i_639136 ON x.book (title); BEGIN -ALTER TABLE x.book ADD CONSTRAINT book_FK_1 FOREIGN KEY (author_id) REFERENCES y.author (id) +ALTER TABLE x.book ADD CONSTRAINT book_fk_4444ca FOREIGN KEY (author_id) REFERENCES y.author (id) END ; @@ -237,15 +237,15 @@ public function testGetAddTablesDDLSchemas($schema) id INT NOT NULL IDENTITY, first_name VARCHAR(100) NULL, last_name VARCHAR(100) NULL, - CONSTRAINT author_PK PRIMARY KEY (id) + CONSTRAINT author_pk PRIMARY KEY (id) ); ----------------------------------------------------------------------- -- x.book_summary ----------------------------------------------------------------------- -IF EXISTS (SELECT 1 FROM sysobjects WHERE type ='RI' AND name='book_summary_FK_1') - ALTER TABLE x.book_summary DROP CONSTRAINT book_summary_FK_1; +IF EXISTS (SELECT 1 FROM sysobjects WHERE type ='RI' AND name='book_summary_fk_23450f') + ALTER TABLE x.book_summary DROP CONSTRAINT book_summary_fk_23450f; IF EXISTS (SELECT 1 FROM sysobjects WHERE type = 'U' AND name = 'x.book_summary') BEGIN @@ -277,11 +277,11 @@ public function testGetAddTablesDDLSchemas($schema) id INT NOT NULL IDENTITY, book_id INT NOT NULL, summary VARCHAR(MAX) NOT NULL, - CONSTRAINT book_summary_PK PRIMARY KEY (id) + CONSTRAINT book_summary_pk PRIMARY KEY (id) ); BEGIN -ALTER TABLE x.book_summary ADD CONSTRAINT book_summary_FK_1 FOREIGN KEY (book_id) REFERENCES x.book (id) ON DELETE CASCADE +ALTER TABLE x.book_summary ADD CONSTRAINT book_summary_fk_23450f FOREIGN KEY (book_id) REFERENCES x.book (id) ON DELETE CASCADE END ; @@ -311,7 +311,7 @@ public function testGetAddTableDDLSimplePK($schema) ( id INT NOT NULL IDENTITY, bar VARCHAR(255) NOT NULL, - CONSTRAINT foo_PK PRIMARY KEY (id) + CONSTRAINT foo_pk PRIMARY KEY (id) ); "; $this->assertEquals($expected, $this->getPlatform()->getAddTableDDL($table)); @@ -329,7 +329,7 @@ public function testGetAddTableDDLCompositePK($schema) foo INT NOT NULL, bar INT NOT NULL, baz VARCHAR(255) NOT NULL, - CONSTRAINT foo_PK PRIMARY KEY (foo,bar) + CONSTRAINT foo_pk PRIMARY KEY (foo,bar) ); "; $this->assertEquals($expected, $this->getPlatform()->getAddTableDDL($table)); @@ -346,7 +346,7 @@ public function testGetAddTableDDLUniqueIndex($schema) ( id INT NOT NULL IDENTITY, bar INT NULL, - CONSTRAINT foo_PK PRIMARY KEY (id), + CONSTRAINT foo_pk PRIMARY KEY (id), UNIQUE (bar) ); "; @@ -364,7 +364,7 @@ public function testGetAddTableDDLSchema($schema) ( id INT NOT NULL IDENTITY, bar INT NULL, - CONSTRAINT foo_PK PRIMARY KEY (id) + CONSTRAINT foo_pk PRIMARY KEY (id) ); "; $this->assertEquals($expected, $this->getPlatform()->getAddTableDDL($table)); @@ -456,7 +456,7 @@ public function testGetPrimaryKeyDDLSimpleKey() $column = new Column('bar'); $column->setPrimaryKey(true); $table->addColumn($column); - $expected = 'CONSTRAINT foo_PK PRIMARY KEY (bar)'; + $expected = 'CONSTRAINT foo_pk PRIMARY KEY (bar)'; $this->assertEquals($expected, $this->getPlatform()->getPrimaryKeyDDL($table)); } @@ -469,7 +469,7 @@ public function testGetPrimaryKeyDDLCompositeKey() $column2 = new Column('bar2'); $column2->setPrimaryKey(true); $table->addColumn($column2); - $expected = 'CONSTRAINT foo_PK PRIMARY KEY (bar1,bar2)'; + $expected = 'CONSTRAINT foo_pk PRIMARY KEY (bar1,bar2)'; $this->assertEquals($expected, $this->getPlatform()->getPrimaryKeyDDL($table)); } @@ -479,7 +479,7 @@ public function testGetPrimaryKeyDDLCompositeKey() public function testGetDropPrimaryKeyDDL($table) { $expected = " -ALTER TABLE foo DROP CONSTRAINT foo_PK; +ALTER TABLE foo DROP CONSTRAINT foo_pk; "; $this->assertEquals($expected, $this->getPlatform()->getDropPrimaryKeyDDL($table)); } @@ -490,7 +490,7 @@ public function testGetDropPrimaryKeyDDL($table) public function testGetAddPrimaryKeyDDL($table) { $expected = " -ALTER TABLE foo ADD CONSTRAINT foo_PK PRIMARY KEY (bar); +ALTER TABLE foo ADD CONSTRAINT foo_pk PRIMARY KEY (bar); "; $this->assertEquals($expected, $this->getPlatform()->getAddPrimaryKeyDDL($table)); } @@ -555,12 +555,12 @@ public function testGetAddForeignKeysDDL($table) { $expected = " BEGIN -ALTER TABLE foo ADD CONSTRAINT foo_bar_FK FOREIGN KEY (bar_id) REFERENCES bar (id) ON DELETE CASCADE +ALTER TABLE foo ADD CONSTRAINT foo_bar_fk FOREIGN KEY (bar_id) REFERENCES bar (id) ON DELETE CASCADE END ; BEGIN -ALTER TABLE foo ADD CONSTRAINT foo_baz_FK FOREIGN KEY (baz_id) REFERENCES baz (id) +ALTER TABLE foo ADD CONSTRAINT foo_baz_fk FOREIGN KEY (baz_id) REFERENCES baz (id) END ; "; @@ -574,7 +574,7 @@ public function testGetAddForeignKeyDDL($fk) { $expected = " BEGIN -ALTER TABLE foo ADD CONSTRAINT foo_bar_FK FOREIGN KEY (bar_id) REFERENCES bar (id) ON DELETE CASCADE +ALTER TABLE foo ADD CONSTRAINT foo_bar_fk FOREIGN KEY (bar_id) REFERENCES bar (id) ON DELETE CASCADE END ; "; @@ -596,7 +596,7 @@ public function testGetAddForeignKeySkipSqlDDL($fk) public function testGetDropForeignKeyDDL($fk) { $expected = " -ALTER TABLE foo DROP CONSTRAINT foo_bar_FK; +ALTER TABLE foo DROP CONSTRAINT foo_bar_fk; "; $this->assertEquals($expected, $this->getPLatform()->getDropForeignKeyDDL($fk)); } @@ -615,7 +615,7 @@ public function testGetDropForeignKeySkipSqlDDL($fk) */ public function testGetForeignKeyDDL($fk) { - $expected = 'CONSTRAINT foo_bar_FK FOREIGN KEY (bar_id) REFERENCES bar (id) ON DELETE CASCADE'; + $expected = 'CONSTRAINT foo_bar_fk FOREIGN KEY (bar_id) REFERENCES bar (id) ON DELETE CASCADE'; $this->assertEquals($expected, $this->getPLatform()->getForeignKeyDDL($fk)); } diff --git a/tests/Propel/Tests/Generator/Platform/MysqlPlatformMigrationMyISAMTest.php b/tests/Propel/Tests/Generator/Platform/MysqlPlatformMigrationMyISAMTest.php index dd7bcffa74..5a0c046480 100644 --- a/tests/Propel/Tests/Generator/Platform/MysqlPlatformMigrationMyISAMTest.php +++ b/tests/Propel/Tests/Generator/Platform/MysqlPlatformMigrationMyISAMTest.php @@ -43,7 +43,7 @@ protected function getPlatform() /** * @dataProvider providerForTestGetModifyDatabaseDDL */ - public function testGetModifyDatabaseDDL($databaseDiff) + public function testRenameTableDDL($databaseDiff) { $expected = " # This is a fix for InnoDB in MySQL >= 4.1.x @@ -52,7 +52,7 @@ public function testGetModifyDatabaseDDL($databaseDiff) DROP TABLE IF EXISTS `foo1`; -DROP TABLE IF EXISTS `foo3`; +RENAME TABLE `foo3` TO `foo4`; ALTER TABLE `foo2` @@ -65,13 +65,6 @@ public function testGetModifyDatabaseDDL($databaseDiff) `baz3` TEXT ); -CREATE TABLE `foo4` -( - `id` INTEGER NOT NULL AUTO_INCREMENT, - `yipee` INTEGER, - PRIMARY KEY (`id`) -) ENGINE=MyISAM; - CREATE TABLE `foo5` ( `id` INTEGER NOT NULL AUTO_INCREMENT, @@ -103,11 +96,11 @@ public function testGetRenameTableDDL($fromName, $toName) public function testGetModifyTableDDL($tableDiff) { $expected = " -DROP INDEX `bar_baz_FK` ON `foo`; +DROP INDEX `bar_baz_fk` ON `foo`; -DROP INDEX `foo1_FI_2` ON `foo`; +DROP INDEX `foo1_fi_2` ON `foo`; -DROP INDEX `bar_FK` ON `foo`; +DROP INDEX `bar_fk` ON `foo`; ALTER TABLE `foo` @@ -120,9 +113,9 @@ public function testGetModifyTableDDL($tableDiff) `baz3` TEXT ); -CREATE INDEX `bar_FK` ON `foo` (`bar1`); +CREATE INDEX `bar_fk` ON `foo` (`bar1`); -CREATE INDEX `baz_FK` ON `foo` (`baz3`); +CREATE INDEX `baz_fk` ON `foo` (`baz3`); "; $this->assertEquals($expected, $this->getPlatform()->getModifyTableDDL($tableDiff)); } @@ -164,13 +157,13 @@ public function testGetModifyTablePrimaryKeysDDL($tableDiff) public function testGetModifyTableIndicesDDL($tableDiff) { $expected = " -DROP INDEX `bar_FK` ON `foo`; +DROP INDEX `bar_fk` ON `foo`; -CREATE INDEX `baz_FK` ON `foo` (`baz`); +CREATE INDEX `baz_fk` ON `foo` (`baz`); -DROP INDEX `bar_baz_FK` ON `foo`; +DROP INDEX `bar_baz_fk` ON `foo`; -CREATE INDEX `bar_baz_FK` ON `foo` (`id`, `bar`, `baz`); +CREATE INDEX `bar_baz_fk` ON `foo` (`id`, `bar`, `baz`); "; $this->assertEquals($expected, $this->getPlatform()->getModifyTableIndicesDDL($tableDiff)); } diff --git a/tests/Propel/Tests/Generator/Platform/MysqlPlatformMigrationTest.php b/tests/Propel/Tests/Generator/Platform/MysqlPlatformMigrationTest.php index b795840386..e2cbd9f4bb 100644 --- a/tests/Propel/Tests/Generator/Platform/MysqlPlatformMigrationTest.php +++ b/tests/Propel/Tests/Generator/Platform/MysqlPlatformMigrationTest.php @@ -44,7 +44,7 @@ protected function getPlatform() /** * @dataProvider providerForTestGetModifyDatabaseDDL */ - public function testGetModifyDatabaseDDL($databaseDiff) + public function testRenameTableDDL($databaseDiff) { $expected = " # This is a fix for InnoDB in MySQL >= 4.1.x @@ -53,7 +53,7 @@ public function testGetModifyDatabaseDDL($databaseDiff) DROP TABLE IF EXISTS `foo1`; -DROP TABLE IF EXISTS `foo3`; +RENAME TABLE `foo3` TO `foo4`; ALTER TABLE `foo2` @@ -66,13 +66,6 @@ public function testGetModifyDatabaseDDL($databaseDiff) `baz3` TEXT ); -CREATE TABLE `foo4` -( - `id` INTEGER NOT NULL AUTO_INCREMENT, - `yipee` INTEGER, - PRIMARY KEY (`id`) -) ENGINE=InnoDB; - CREATE TABLE `foo5` ( `id` INTEGER NOT NULL AUTO_INCREMENT, @@ -104,15 +97,15 @@ public function testGetRenameTableDDL($fromName, $toName) public function testGetModifyTableDDL($tableDiff) { $expected = " -ALTER TABLE `foo` DROP FOREIGN KEY `foo1_FK_2`; +ALTER TABLE `foo` DROP FOREIGN KEY `foo1_fk_2`; -ALTER TABLE `foo` DROP FOREIGN KEY `foo1_FK_1`; +ALTER TABLE `foo` DROP FOREIGN KEY `foo1_fk_1`; -DROP INDEX `bar_baz_FK` ON `foo`; +DROP INDEX `bar_baz_fk` ON `foo`; -DROP INDEX `foo1_FI_2` ON `foo`; +DROP INDEX `foo1_fi_2` ON `foo`; -DROP INDEX `bar_FK` ON `foo`; +DROP INDEX `bar_fk` ON `foo`; ALTER TABLE `foo` @@ -125,11 +118,11 @@ public function testGetModifyTableDDL($tableDiff) `baz3` TEXT ); -CREATE INDEX `bar_FK` ON `foo` (`bar1`); +CREATE INDEX `bar_fk` ON `foo` (`bar1`); -CREATE INDEX `baz_FK` ON `foo` (`baz3`); +CREATE INDEX `baz_fk` ON `foo` (`baz3`); -ALTER TABLE `foo` ADD CONSTRAINT `foo1_FK_1` +ALTER TABLE `foo` ADD CONSTRAINT `foo1_fk_1` FOREIGN KEY (`bar1`) REFERENCES `foo2` (`bar`); "; @@ -173,13 +166,13 @@ public function testGetModifyTablePrimaryKeysDDL($tableDiff) public function testGetModifyTableIndicesDDL($tableDiff) { $expected = " -DROP INDEX `bar_FK` ON `foo`; +DROP INDEX `bar_fk` ON `foo`; -CREATE INDEX `baz_FK` ON `foo` (`baz`); +CREATE INDEX `baz_fk` ON `foo` (`baz`); -DROP INDEX `bar_baz_FK` ON `foo`; +DROP INDEX `bar_baz_fk` ON `foo`; -CREATE INDEX `bar_baz_FK` ON `foo` (`id`, `bar`, `baz`); +CREATE INDEX `bar_baz_fk` ON `foo` (`id`, `bar`, `baz`); "; $this->assertEquals($expected, $this->getPlatform()->getModifyTableIndicesDDL($tableDiff)); } @@ -190,15 +183,15 @@ public function testGetModifyTableIndicesDDL($tableDiff) public function testGetModifyTableForeignKeysDDL($tableDiff) { $expected = " -ALTER TABLE `foo1` DROP FOREIGN KEY `foo1_FK_1`; +ALTER TABLE `foo1` DROP FOREIGN KEY `foo1_fk_1`; -ALTER TABLE `foo1` ADD CONSTRAINT `foo1_FK_3` +ALTER TABLE `foo1` ADD CONSTRAINT `foo1_fk_3` FOREIGN KEY (`baz`) REFERENCES `foo2` (`baz`); -ALTER TABLE `foo1` DROP FOREIGN KEY `foo1_FK_2`; +ALTER TABLE `foo1` DROP FOREIGN KEY `foo1_fk_2`; -ALTER TABLE `foo1` ADD CONSTRAINT `foo1_FK_2` +ALTER TABLE `foo1` ADD CONSTRAINT `foo1_fk_2` FOREIGN KEY (`bar`,`id`) REFERENCES `foo2` (`bar`,`id`); "; @@ -211,11 +204,11 @@ public function testGetModifyTableForeignKeysDDL($tableDiff) public function testGetModifyTableForeignKeysSkipSqlDDL($tableDiff) { $expected = " -ALTER TABLE `foo1` DROP FOREIGN KEY `foo1_FK_1`; +ALTER TABLE `foo1` DROP FOREIGN KEY `foo1_fk_1`; "; $this->assertEquals($expected, $this->getPlatform()->getModifyTableForeignKeysDDL($tableDiff)); $expected = " -ALTER TABLE `foo1` ADD CONSTRAINT `foo1_FK_1` +ALTER TABLE `foo1` ADD CONSTRAINT `foo1_fk_1` FOREIGN KEY (`bar`) REFERENCES `foo2` (`bar`); "; @@ -347,4 +340,56 @@ public function testColumnRenaming() $this->assertEquals('bar2', $secondPair[0]->getName()); $this->assertEquals('bar_la2', $secondPair[1]->getName()); } + + public function testTableRenaming() + { + $schema1 = ' + +
+ + + +
+ + + + +
+ +'; + $schema2 = ' + + + + + +
+ + + + +
+
+'; + + $d1 = $this->getDatabaseFromSchema($schema1); + $d2 = $this->getDatabaseFromSchema($schema2); + + $diff = DatabaseComparator::computeDiff($d1, $d2, false, true); + $renamedTables = $diff->getRenamedTables(); + + $firstPair = array(key($renamedTables), current($renamedTables)); + next($renamedTables); + $secondPair = array(key($renamedTables), current($renamedTables)); + + $this->assertEquals('foo', $firstPair[0]); + $this->assertEquals('foo_bla', $firstPair[1]); + + $this->assertEquals('foo2', $secondPair[0]); + $this->assertEquals( + 'foo_bla2', + $secondPair[1], + 'Table `Foo2` should not renamed to `foo_bla` since we have already renamed a table to this name.' + ); + } } diff --git a/tests/Propel/Tests/Generator/Platform/MysqlPlatformMyISAMTest.php b/tests/Propel/Tests/Generator/Platform/MysqlPlatformMyISAMTest.php index 31b525cc49..f76428c7c4 100644 --- a/tests/Propel/Tests/Generator/Platform/MysqlPlatformMyISAMTest.php +++ b/tests/Propel/Tests/Generator/Platform/MysqlPlatformMyISAMTest.php @@ -91,8 +91,8 @@ public function testGetAddTablesDDLSchema($schema) `title` VARCHAR(255) NOT NULL, `author_id` INTEGER, PRIMARY KEY (`id`), - INDEX `book_I_1` (`title`), - INDEX `book_FI_1` (`author_id`) + INDEX `book_i_639136` (`title`), + INDEX `book_fi_4444ca` (`author_id`) ) ENGINE=MyISAM; -- --------------------------------------------------------------------- @@ -121,7 +121,7 @@ public function testGetAddTablesDDLSchema($schema) `book_id` INTEGER NOT NULL, `summary` TEXT NOT NULL, PRIMARY KEY (`id`), - INDEX `book_summary_FI_1` (`book_id`) + INDEX `book_summary_fi_23450f` (`book_id`) ) ENGINE=MyISAM; # This restores the fkey checks, after having unset them earlier @@ -155,8 +155,8 @@ public function testGetAddTablesDDL($schema) `title` VARCHAR(255) NOT NULL, `author_id` INTEGER, PRIMARY KEY (`id`), - INDEX `book_I_1` (`title`), - INDEX `book_FI_1` (`author_id`) + INDEX `book_i_639136` (`title`), + INDEX `book_fi_ea464c` (`author_id`) ) ENGINE=MyISAM; -- --------------------------------------------------------------------- @@ -237,7 +237,7 @@ public function testGetAddTableDDLUniqueIndex($schema) `id` INTEGER NOT NULL AUTO_INCREMENT, `bar` INTEGER, PRIMARY KEY (`id`), - UNIQUE INDEX `foo_U_1` (`bar`) + UNIQUE INDEX `foo_u_14f552` (`bar`) ) ENGINE=MyISAM; "; $this->assertEquals($expected, $this->getPlatform()->getAddTableDDL($table)); @@ -263,7 +263,7 @@ public function testGetAddTableDDLIndex() `id` INTEGER NOT NULL AUTO_INCREMENT, `bar` INTEGER, PRIMARY KEY (`id`), - INDEX `foo_I_1` (`bar`) + INDEX `foo_i_14f552` (`bar`) ) ENGINE=MyISAM; "; $this->assertEquals($expected, $this->getPlatform()->getAddTableDDL($table)); @@ -292,7 +292,7 @@ public function testGetAddTableDDLForeignKey() `id` INTEGER NOT NULL AUTO_INCREMENT, `bar_id` INTEGER, PRIMARY KEY (`id`), - INDEX `foo_FI_1` (`bar_id`) + INDEX `foo_fi_426410` (`bar_id`) ) ENGINE=MyISAM; "; $this->assertEquals($expected, $this->getPlatform()->getAddTableDDL($table)); @@ -321,7 +321,7 @@ public function testGetAddTableDDLForeignKeySkipSql() `id` INTEGER NOT NULL AUTO_INCREMENT, `bar_id` INTEGER, PRIMARY KEY (`id`), - INDEX `foo_FI_1` (`bar_id`) + INDEX `foo_fi_426410` (`bar_id`) ) ENGINE=MyISAM; "; $this->assertEquals($expected, $this->getPlatform()->getAddTableDDL($table)); @@ -722,7 +722,7 @@ public function testAddExtraIndicesForeignKeys() `subid` INTEGER, `id` INTEGER NOT NULL AUTO_INCREMENT, PRIMARY KEY (`id`), - INDEX `bar_FI_1` (`id`, `subid`) + INDEX `bar_fi_bb8268` (`id`, `subid`) ) ENGINE=MyISAM; "; diff --git a/tests/Propel/Tests/Generator/Platform/MysqlPlatformTest.php b/tests/Propel/Tests/Generator/Platform/MysqlPlatformTest.php index e42b5dc0f2..1771d50604 100644 --- a/tests/Propel/Tests/Generator/Platform/MysqlPlatformTest.php +++ b/tests/Propel/Tests/Generator/Platform/MysqlPlatformTest.php @@ -91,9 +91,9 @@ public function testGetAddTablesDDLSchema($schema) `title` VARCHAR(255) NOT NULL, `author_id` INTEGER, PRIMARY KEY (`id`), - INDEX `book_I_1` (`title`), - INDEX `book_FI_1` (`author_id`), - CONSTRAINT `book_FK_1` + INDEX `book_i_639136` (`title`), + INDEX `book_fi_4444ca` (`author_id`), + CONSTRAINT `book_fk_4444ca` FOREIGN KEY (`author_id`) REFERENCES `y`.`author` (`id`) ) ENGINE=InnoDB; @@ -124,8 +124,8 @@ public function testGetAddTablesDDLSchema($schema) `book_id` INTEGER NOT NULL, `summary` TEXT NOT NULL, PRIMARY KEY (`id`), - INDEX `book_summary_FI_1` (`book_id`), - CONSTRAINT `book_summary_FK_1` + INDEX `book_summary_fi_23450f` (`book_id`), + CONSTRAINT `book_summary_fk_23450f` FOREIGN KEY (`book_id`) REFERENCES `x`.`book` (`id`) ON DELETE CASCADE @@ -162,9 +162,9 @@ public function testGetAddTablesDDL($schema) `title` VARCHAR(255) NOT NULL, `author_id` INTEGER, PRIMARY KEY (`id`), - INDEX `book_I_1` (`title`), - INDEX `book_FI_1` (`author_id`), - CONSTRAINT `book_FK_1` + INDEX `book_i_639136` (`title`), + INDEX `book_fi_ea464c` (`author_id`), + CONSTRAINT `book_fk_ea464c` FOREIGN KEY (`author_id`) REFERENCES `author` (`id`) ) ENGINE=InnoDB; @@ -247,7 +247,7 @@ public function testGetAddTableDDLUniqueIndex($schema) `id` INTEGER NOT NULL AUTO_INCREMENT, `bar` INTEGER, PRIMARY KEY (`id`), - UNIQUE INDEX `foo_U_1` (`bar`) + UNIQUE INDEX `foo_u_14f552` (`bar`) ) ENGINE=InnoDB; "; $this->assertEquals($expected, $this->getPlatform()->getAddTableDDL($table)); @@ -273,7 +273,7 @@ public function testGetAddTableDDLIndex() `id` INTEGER NOT NULL AUTO_INCREMENT, `bar` INTEGER, PRIMARY KEY (`id`), - INDEX `foo_I_1` (`bar`) + INDEX `foo_i_14f552` (`bar`) ) ENGINE=InnoDB; "; $this->assertEquals($expected, $this->getPlatform()->getAddTableDDL($table)); @@ -302,8 +302,8 @@ public function testGetAddTableDDLForeignKey() `id` INTEGER NOT NULL AUTO_INCREMENT, `bar_id` INTEGER, PRIMARY KEY (`id`), - INDEX `foo_FI_1` (`bar_id`), - CONSTRAINT `foo_FK_1` + INDEX `foo_fi_426410` (`bar_id`), + CONSTRAINT `foo_fk_426410` FOREIGN KEY (`bar_id`) REFERENCES `bar` (`id`) ) ENGINE=InnoDB; @@ -334,7 +334,7 @@ public function testGetAddTableDDLForeignKeySkipSql() `id` INTEGER NOT NULL AUTO_INCREMENT, `bar_id` INTEGER, PRIMARY KEY (`id`), - INDEX `foo_FI_1` (`bar_id`) + INDEX `foo_fi_426410` (`bar_id`) ) ENGINE=InnoDB; "; $this->assertEquals($expected, $this->getPlatform()->getAddTableDDL($table)); @@ -638,12 +638,12 @@ public function testGetUniqueDDL($index) public function testGetAddForeignKeysDDL($table) { $expected = " -ALTER TABLE `foo` ADD CONSTRAINT `foo_bar_FK` +ALTER TABLE `foo` ADD CONSTRAINT `foo_bar_fk` FOREIGN KEY (`bar_id`) REFERENCES `bar` (`id`) ON DELETE CASCADE; -ALTER TABLE `foo` ADD CONSTRAINT `foo_baz_FK` +ALTER TABLE `foo` ADD CONSTRAINT `foo_baz_fk` FOREIGN KEY (`baz_id`) REFERENCES `baz` (`id`) ON DELETE SET NULL; @@ -657,7 +657,7 @@ public function testGetAddForeignKeysDDL($table) public function testGetAddForeignKeyDDL($fk) { $expected = " -ALTER TABLE `foo` ADD CONSTRAINT `foo_bar_FK` +ALTER TABLE `foo` ADD CONSTRAINT `foo_bar_fk` FOREIGN KEY (`bar_id`) REFERENCES `bar` (`id`) ON DELETE CASCADE; @@ -680,7 +680,7 @@ public function testGetAddForeignKeySkipSqlDDL($fk) public function testGetDropForeignKeyDDL($fk) { $expected = " -ALTER TABLE `foo` DROP FOREIGN KEY `foo_bar_FK`; +ALTER TABLE `foo` DROP FOREIGN KEY `foo_bar_fk`; "; $this->assertEquals($expected, $this->getPlatform()->getDropForeignKeyDDL($fk)); } @@ -699,7 +699,7 @@ public function testGetDropForeignKeySkipSqlDDL($fk) */ public function testGetForeignKeyDDL($fk) { - $expected = "CONSTRAINT `foo_bar_FK` + $expected = "CONSTRAINT `foo_bar_fk` FOREIGN KEY (`bar_id`) REFERENCES `bar` (`id`) ON DELETE CASCADE"; @@ -755,8 +755,8 @@ public function testAddExtraIndicesForeignKeys() `subid` INTEGER, `id` INTEGER NOT NULL AUTO_INCREMENT, PRIMARY KEY (`id`), - INDEX `bar_FI_1` (`id`, `subid`), - CONSTRAINT `bar_FK_1` + INDEX `bar_fi_bb8268` (`id`, `subid`), + CONSTRAINT `bar_fk_bb8268` FOREIGN KEY (`id`,`subid`) REFERENCES `foo` (`id`,`subid`) ) ENGINE=InnoDB; diff --git a/tests/Propel/Tests/Generator/Platform/OraclePlatformMigrationTest.php b/tests/Propel/Tests/Generator/Platform/OraclePlatformMigrationTest.php index b284c45816..c654e63971 100644 --- a/tests/Propel/Tests/Generator/Platform/OraclePlatformMigrationTest.php +++ b/tests/Propel/Tests/Generator/Platform/OraclePlatformMigrationTest.php @@ -42,20 +42,7 @@ public function testGetModifyDatabaseDDL($databaseDiff) DROP SEQUENCE foo1_SEQ; -DROP TABLE foo3 CASCADE CONSTRAINTS; - -DROP SEQUENCE foo3_SEQ; - -CREATE TABLE foo4 -( - id NUMBER NOT NULL, - yipee NUMBER -); - -ALTER TABLE foo4 ADD CONSTRAINT foo4_PK PRIMARY KEY (id); - -CREATE SEQUENCE foo4_SEQ - INCREMENT BY 1 START WITH 1 NOMAXVALUE NOCYCLE NOCACHE ORDER; +ALTER TABLE foo3 RENAME TO foo4; CREATE TABLE foo5 ( @@ -64,7 +51,7 @@ public function testGetModifyDatabaseDDL($databaseDiff) dfgdsgf NVARCHAR2(2000) ); -ALTER TABLE foo5 ADD CONSTRAINT foo5_PK PRIMARY KEY (id); +ALTER TABLE foo5 ADD CONSTRAINT foo5_pk PRIMARY KEY (id); CREATE SEQUENCE foo5_SEQ INCREMENT BY 1 START WITH 1 NOMAXVALUE NOCYCLE NOCACHE ORDER; @@ -103,13 +90,13 @@ public function testGetRenameTableDDL($fromName, $toName) public function testGetModifyTableDDL($tableDiff) { $expected = " -ALTER TABLE foo DROP CONSTRAINT foo1_FK_2; +ALTER TABLE foo DROP CONSTRAINT foo1_fk_2; -ALTER TABLE foo DROP CONSTRAINT foo1_FK_1; +ALTER TABLE foo DROP CONSTRAINT foo1_fk_1; -DROP INDEX bar_baz_FK; +DROP INDEX bar_baz_fk; -DROP INDEX bar_FK; +DROP INDEX bar_fk; ALTER TABLE foo RENAME COLUMN bar TO bar1; @@ -125,11 +112,11 @@ public function testGetModifyTableDDL($tableDiff) baz3 NVARCHAR2(2000) ); -CREATE INDEX bar_FK ON foo (bar1); +CREATE INDEX bar_fk ON foo (bar1); -CREATE INDEX baz_FK ON foo (baz3); +CREATE INDEX baz_fk ON foo (baz3); -ALTER TABLE foo ADD CONSTRAINT foo1_FK_1 +ALTER TABLE foo ADD CONSTRAINT foo1_fk_1 FOREIGN KEY (bar1) REFERENCES foo2 (bar); "; $this->assertEquals($expected, $this->getPlatform()->getModifyTableDDL($tableDiff)); @@ -162,9 +149,9 @@ public function testGetModifyTableColumnsDDL($tableDiff) public function testGetModifyTablePrimaryKeysDDL($tableDiff) { $expected = " -ALTER TABLE foo DROP CONSTRAINT foo_PK; +ALTER TABLE foo DROP CONSTRAINT foo_pk; -ALTER TABLE foo ADD CONSTRAINT foo_PK PRIMARY KEY (id,bar); +ALTER TABLE foo ADD CONSTRAINT foo_pk PRIMARY KEY (id,bar); "; $this->assertEquals($expected, $this->getPlatform()->getModifyTablePrimaryKeyDDL($tableDiff)); } @@ -175,13 +162,13 @@ public function testGetModifyTablePrimaryKeysDDL($tableDiff) public function testGetModifyTableIndicesDDL($tableDiff) { $expected = " -DROP INDEX bar_FK; +DROP INDEX bar_fk; -CREATE INDEX baz_FK ON foo (baz); +CREATE INDEX baz_fk ON foo (baz); -DROP INDEX bar_baz_FK; +DROP INDEX bar_baz_fk; -CREATE INDEX bar_baz_FK ON foo (id,bar,baz); +CREATE INDEX bar_baz_fk ON foo (id,bar,baz); "; $this->assertEquals($expected, $this->getPlatform()->getModifyTableIndicesDDL($tableDiff)); } @@ -192,14 +179,14 @@ public function testGetModifyTableIndicesDDL($tableDiff) public function testGetModifyTableForeignKeysDDL($tableDiff) { $expected = " -ALTER TABLE foo1 DROP CONSTRAINT foo1_FK_1; +ALTER TABLE foo1 DROP CONSTRAINT foo1_fk_1; -ALTER TABLE foo1 ADD CONSTRAINT foo1_FK_3 +ALTER TABLE foo1 ADD CONSTRAINT foo1_fk_3 FOREIGN KEY (baz) REFERENCES foo2 (baz); -ALTER TABLE foo1 DROP CONSTRAINT foo1_FK_2; +ALTER TABLE foo1 DROP CONSTRAINT foo1_fk_2; -ALTER TABLE foo1 ADD CONSTRAINT foo1_FK_2 +ALTER TABLE foo1 ADD CONSTRAINT foo1_fk_2 FOREIGN KEY (bar,id) REFERENCES foo2 (bar,id); "; $this->assertEquals($expected, $this->getPlatform()->getModifyTableForeignKeysDDL($tableDiff)); @@ -211,11 +198,11 @@ public function testGetModifyTableForeignKeysDDL($tableDiff) public function testGetModifyTableForeignKeysSkipSqlDDL($tableDiff) { $expected = " -ALTER TABLE foo1 DROP CONSTRAINT foo1_FK_1; +ALTER TABLE foo1 DROP CONSTRAINT foo1_fk_1; "; $this->assertEquals($expected, $this->getPlatform()->getModifyTableForeignKeysDDL($tableDiff)); $expected = " -ALTER TABLE foo1 ADD CONSTRAINT foo1_FK_1 +ALTER TABLE foo1 ADD CONSTRAINT foo1_fk_1 FOREIGN KEY (bar) REFERENCES foo2 (bar); "; $this->assertEquals($expected, $this->getPlatform()->getModifyTableForeignKeysDDL($tableDiff->getReverseDiff())); @@ -427,7 +414,7 @@ public function testGetModifyDatabaseWithBlockStorageDDL() ) TABLESPACE L_128K; -ALTER TABLE foo4 ADD CONSTRAINT foo4_PK PRIMARY KEY (id) +ALTER TABLE foo4 ADD CONSTRAINT foo4_pk PRIMARY KEY (id) USING INDEX PCTFREE 20 INITRANS 4 @@ -458,7 +445,7 @@ public function testGetModifyDatabaseWithBlockStorageDDL() ) TABLESPACE L_128K; -ALTER TABLE foo5 ADD CONSTRAINT foo5_PK PRIMARY KEY (id) +ALTER TABLE foo5 ADD CONSTRAINT foo5_pk PRIMARY KEY (id) USING INDEX PCTFREE 20 INITRANS 4 diff --git a/tests/Propel/Tests/Generator/Platform/OraclePlatformTest.php b/tests/Propel/Tests/Generator/Platform/OraclePlatformTest.php index c75d2eeb28..1f207c30ee 100644 --- a/tests/Propel/Tests/Generator/Platform/OraclePlatformTest.php +++ b/tests/Propel/Tests/Generator/Platform/OraclePlatformTest.php @@ -78,12 +78,12 @@ public function testGetAddTablesDDL($schema) author_id NUMBER ); -ALTER TABLE book ADD CONSTRAINT book_PK PRIMARY KEY (id); +ALTER TABLE book ADD CONSTRAINT book_pk PRIMARY KEY (id); CREATE SEQUENCE book_SEQ INCREMENT BY 1 START WITH 1 NOMAXVALUE NOCYCLE NOCACHE ORDER; -CREATE INDEX book_I_1 ON book (title); +CREATE INDEX book_i_639136 ON book (title); ----------------------------------------------------------------------- -- author @@ -100,7 +100,7 @@ public function testGetAddTablesDDL($schema) last_name NVARCHAR2(100) ); -ALTER TABLE author ADD CONSTRAINT author_PK PRIMARY KEY (id); +ALTER TABLE author ADD CONSTRAINT author_pk PRIMARY KEY (id); CREATE SEQUENCE author_SEQ INCREMENT BY 1 START WITH 1 NOMAXVALUE NOCYCLE NOCACHE ORDER; @@ -109,7 +109,7 @@ public function testGetAddTablesDDL($schema) -- Foreign Keys ----------------------------------------------------------------------- -ALTER TABLE book ADD CONSTRAINT book_FK_1 +ALTER TABLE book ADD CONSTRAINT book_fk_ea464c FOREIGN KEY (author_id) REFERENCES author (id); EOF; @@ -143,7 +143,7 @@ public function testGetAddTableDDLSimplePK($schema) bar NVARCHAR2(255) NOT NULL ); -ALTER TABLE foo ADD CONSTRAINT foo_PK PRIMARY KEY (id); +ALTER TABLE foo ADD CONSTRAINT foo_pk PRIMARY KEY (id); CREATE SEQUENCE foo_SEQ INCREMENT BY 1 START WITH 1 NOMAXVALUE NOCYCLE NOCACHE ORDER; @@ -165,7 +165,7 @@ public function testGetAddTableDDLCompositePK($schema) baz NVARCHAR2(255) NOT NULL ); -ALTER TABLE foo ADD CONSTRAINT foo_PK PRIMARY KEY (foo,bar); +ALTER TABLE foo ADD CONSTRAINT foo_pk PRIMARY KEY (foo,bar); "; $this->assertEquals($expected, $this->getPlatform()->getAddTableDDL($table)); } @@ -181,10 +181,10 @@ public function testGetAddTableDDLUniqueIndex($schema) ( id NUMBER NOT NULL, bar NUMBER, - CONSTRAINT foo_U_1 UNIQUE (bar) + CONSTRAINT foo_u_14f552 UNIQUE (bar) ); -ALTER TABLE foo ADD CONSTRAINT foo_PK PRIMARY KEY (id); +ALTER TABLE foo ADD CONSTRAINT foo_pk PRIMARY KEY (id); CREATE SEQUENCE foo_SEQ INCREMENT BY 1 START WITH 1 NOMAXVALUE NOCYCLE NOCACHE ORDER; @@ -234,7 +234,7 @@ public function testGetColumnDDLCustomSqlType() */ public function testGetPrimaryKeyDDLSimpleKey($table) { - $expected ='CONSTRAINT foo_PK PRIMARY KEY (bar)'; + $expected ='CONSTRAINT foo_pk PRIMARY KEY (bar)'; $this->assertEquals($expected, $this->getPlatform()->getPrimaryKeyDDL($table)); } @@ -244,7 +244,7 @@ public function testGetPrimaryKeyDDLLongTableName() $column = new Column('bar'); $column->setPrimaryKey(true); $table->addColumn($column); - $expected = 'CONSTRAINT this_table_has_a_very_long__PK PRIMARY KEY (bar)'; + $expected = 'CONSTRAINT this_table_has_a_very_long__pk PRIMARY KEY (bar)'; $this->assertEquals($expected, $this->getPlatform()->getPrimaryKeyDDL($table)); } @@ -257,7 +257,7 @@ public function testGetPrimaryKeyDDLCompositeKey() $column2 = new Column('bar2'); $column2->setPrimaryKey(true); $table->addColumn($column2); - $expected = 'CONSTRAINT foo_PK PRIMARY KEY (bar1,bar2)'; + $expected = 'CONSTRAINT foo_pk PRIMARY KEY (bar1,bar2)'; $this->assertEquals($expected, $this->getPlatform()->getPrimaryKeyDDL($table)); } @@ -267,7 +267,7 @@ public function testGetPrimaryKeyDDLCompositeKey() public function testGetDropPrimaryKeyDDL($table) { $expected = " -ALTER TABLE foo DROP CONSTRAINT foo_PK; +ALTER TABLE foo DROP CONSTRAINT foo_pk; "; $this->assertEquals($expected, $this->getPlatform()->getDropPrimaryKeyDDL($table)); } @@ -278,7 +278,7 @@ public function testGetDropPrimaryKeyDDL($table) public function testGetAddPrimaryKeyDDL($table) { $expected = " -ALTER TABLE foo ADD CONSTRAINT foo_PK PRIMARY KEY (bar); +ALTER TABLE foo ADD CONSTRAINT foo_pk PRIMARY KEY (bar); "; $this->assertEquals($expected, $this->getPlatform()->getAddPrimaryKeyDDL($table)); } @@ -342,11 +342,11 @@ public function testGetUniqueDDL($index) public function testGetAddForeignKeysDDL($table) { $expected = " -ALTER TABLE foo ADD CONSTRAINT foo_bar_FK +ALTER TABLE foo ADD CONSTRAINT foo_bar_fk FOREIGN KEY (bar_id) REFERENCES bar (id) ON DELETE CASCADE; -ALTER TABLE foo ADD CONSTRAINT foo_baz_FK +ALTER TABLE foo ADD CONSTRAINT foo_baz_fk FOREIGN KEY (baz_id) REFERENCES baz (id) ON DELETE SET NULL; "; @@ -359,7 +359,7 @@ public function testGetAddForeignKeysDDL($table) public function testGetAddForeignKeyDDL($fk) { $expected = " -ALTER TABLE foo ADD CONSTRAINT foo_bar_FK +ALTER TABLE foo ADD CONSTRAINT foo_bar_fk FOREIGN KEY (bar_id) REFERENCES bar (id) ON DELETE CASCADE; "; @@ -381,7 +381,7 @@ public function testGetAddForeignKeySkipSqlDDL($fk) public function testGetDropForeignKeyDDL($fk) { $expected = " -ALTER TABLE foo DROP CONSTRAINT foo_bar_FK; +ALTER TABLE foo DROP CONSTRAINT foo_bar_fk; "; $this->assertEquals($expected, $this->getPLatform()->getDropForeignKeyDDL($fk)); } @@ -400,7 +400,7 @@ public function testGetDropForeignKeySkipSqlDDL($fk) */ public function testGetForeignKeyDDL($fk) { - $expected = "CONSTRAINT foo_bar_FK + $expected = "CONSTRAINT foo_bar_fk FOREIGN KEY (bar_id) REFERENCES bar (id) ON DELETE CASCADE"; $this->assertEquals($expected, $this->getPLatform()->getForeignKeyDDL($fk)); @@ -513,7 +513,7 @@ public function testGetOracleBlockStorageDDL() ) TABLESPACE L_128K; -ALTER TABLE book ADD CONSTRAINT book_PK PRIMARY KEY (id) +ALTER TABLE book ADD CONSTRAINT book_pk PRIMARY KEY (id) USING INDEX PCTFREE 20 INITRANS 4 @@ -528,7 +528,7 @@ public function testGetOracleBlockStorageDDL() CREATE SEQUENCE book_SEQ INCREMENT BY 1 START WITH 1 NOMAXVALUE NOCYCLE NOCACHE ORDER; -CREATE INDEX book_I_1 ON book (title) +CREATE INDEX book_i_639136 ON book (title) PCTFREE 20 INITRANS 4 STORAGE @@ -563,7 +563,7 @@ public function testGetOracleBlockStorageDDL() ) TABLESPACE L_128K; -ALTER TABLE author ADD CONSTRAINT author_PK PRIMARY KEY (id) +ALTER TABLE author ADD CONSTRAINT author_pk PRIMARY KEY (id) USING INDEX PCTFREE 20 INITRANS 4 @@ -582,7 +582,7 @@ public function testGetOracleBlockStorageDDL() -- Foreign Keys ----------------------------------------------------------------------- -ALTER TABLE book ADD CONSTRAINT book_FK_1 +ALTER TABLE book ADD CONSTRAINT book_fk_4444ca FOREIGN KEY (author_id) REFERENCES author (id); EOF; diff --git a/tests/Propel/Tests/Generator/Platform/PgsqlPlatformMigrationTest.php b/tests/Propel/Tests/Generator/Platform/PgsqlPlatformMigrationTest.php index c4bae47a80..b8042bc894 100644 --- a/tests/Propel/Tests/Generator/Platform/PgsqlPlatformMigrationTest.php +++ b/tests/Propel/Tests/Generator/Platform/PgsqlPlatformMigrationTest.php @@ -42,14 +42,7 @@ public function testGetModifyDatabaseDDL($databaseDiff) DROP TABLE IF EXISTS foo1 CASCADE; -DROP TABLE IF EXISTS foo3 CASCADE; - -CREATE TABLE foo4 -( - id serial NOT NULL, - yipee INTEGER, - PRIMARY KEY (id) -); +ALTER TABLE foo3 RENAME TO foo4; CREATE TABLE foo5 ( @@ -89,13 +82,13 @@ public function testGetModifyTableDDL($tableDiff) { $expected = <<assertEquals($expected, $this->getPlatform()->getModifyTableIndicesDDL($tableDiff)); @@ -175,15 +168,15 @@ public function testGetModifyTableForeignKeysDDL($tableDiff) { $expected = <<assertEquals($expected, $this->getPlatform()->getModifyTableForeignKeysDDL($tableDiff)); $expected = <<assertEquals($expected, $this->getPlatform()->getDropForeignKeyDDL($fk)); } @@ -697,7 +697,7 @@ public function testGetDropForeignKeySkipSqlDDL($fk) */ public function testGetForeignKeyDDL($fk) { - $expected = "CONSTRAINT foo_bar_FK + $expected = "CONSTRAINT foo_bar_fk FOREIGN KEY (bar_id) REFERENCES bar (id) ON DELETE CASCADE"; diff --git a/tests/Propel/Tests/Generator/Platform/PlatformMigrationTestProvider.php b/tests/Propel/Tests/Generator/Platform/PlatformMigrationTestProvider.php index efb6f3929a..addb24df70 100644 --- a/tests/Propel/Tests/Generator/Platform/PlatformMigrationTestProvider.php +++ b/tests/Propel/Tests/Generator/Platform/PlatformMigrationTestProvider.php @@ -63,7 +63,7 @@ public function providerForTestGetModifyDatabaseDDL() $d1 = $this->getDatabaseFromSchema($schema1); $d2 = $this->getDatabaseFromSchema($schema2); - return array(array(DatabaseComparator::computeDiff($d1, $d2))); + return array(array(DatabaseComparator::computeDiff($d1, $d2, $caseInsensitive = false, $withRenaming = true))); } public function providerForTestGetRenameTableDDL() @@ -79,16 +79,16 @@ public function providerForTestGetModifyTableDDL() - + - + - + - + @@ -107,13 +107,13 @@ public function providerForTestGetModifyTableDDL() - + - + - + @@ -199,10 +199,10 @@ public function providerForTestGetModifyTableIndicesDDL() - + - + @@ -215,12 +215,12 @@ public function providerForTestGetModifyTableIndicesDDL() - + - + @@ -244,10 +244,10 @@ public function providerForTestGetModifyTableForeignKeysDDL() - + - + @@ -265,11 +265,11 @@ public function providerForTestGetModifyTableForeignKeysDDL() - + - + @@ -297,7 +297,7 @@ public function providerForTestGetModifyTableForeignKeysSkipSqlDDL() - +
@@ -312,7 +312,7 @@ public function providerForTestGetModifyTableForeignKeysSkipSqlDDL() - +
@@ -339,7 +339,7 @@ public function providerForTestGetModifyTableForeignKeysSkipSql2DDL() - +
diff --git a/tests/Propel/Tests/Generator/Platform/PlatformTestProvider.php b/tests/Propel/Tests/Generator/Platform/PlatformTestProvider.php index 7a6ef06714..a69cb74774 100644 --- a/tests/Propel/Tests/Generator/Platform/PlatformTestProvider.php +++ b/tests/Propel/Tests/Generator/Platform/PlatformTestProvider.php @@ -265,7 +265,7 @@ public function providerForTestGetForeignKeyDDL() $column2 = new Column('id'); $column2->getDomain()->copy(new Domain('BARTYPE')); $table2->addColumn($column2); - $fk = new ForeignKey('foo_bar_FK'); + $fk = new ForeignKey('foo_bar_fk'); $fk->setForeignTableCommonName('bar'); $fk->addReference($column1, $column2); $fk->setOnDelete('CASCADE'); @@ -299,7 +299,7 @@ public function providerForTestGetForeignKeysDDL() $column2->getDomain()->copy(new Domain('BARTYPE')); $table2->addColumn($column2); - $fk = new ForeignKey('foo_bar_FK'); + $fk = new ForeignKey('foo_bar_fk'); $fk->setForeignTableCommonName('bar'); $fk->addReference($column1, $column2); $fk->setOnDelete('CASCADE'); @@ -313,7 +313,7 @@ public function providerForTestGetForeignKeysDDL() $column4->getDomain()->copy(new Domain('BAZTYPE')); $table3->addColumn($column4); - $fk = new ForeignKey('foo_baz_FK'); + $fk = new ForeignKey('foo_baz_fk'); $fk->setForeignTableCommonName('baz'); $fk->addReference($column3, $column4); $fk->setOnDelete('SETNULL'); diff --git a/tests/Propel/Tests/Generator/Platform/SqlitePlatformTest.php b/tests/Propel/Tests/Generator/Platform/SqlitePlatformTest.php index 29e3200512..2ebf466595 100644 --- a/tests/Propel/Tests/Generator/Platform/SqlitePlatformTest.php +++ b/tests/Propel/Tests/Generator/Platform/SqlitePlatformTest.php @@ -85,10 +85,11 @@ public function testGetAddTablesDDL($schema) id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, title VARCHAR(255) NOT NULL, author_id INTEGER, + UNIQUE (id), FOREIGN KEY (author_id) REFERENCES author (id) ); -CREATE INDEX book_I_1 ON book (title); +CREATE INDEX book_i_639136 ON book (title); ----------------------------------------------------------------------- -- author @@ -100,7 +101,8 @@ public function testGetAddTablesDDL($schema) ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, first_name VARCHAR(100), - last_name VARCHAR(100) + last_name VARCHAR(100), + UNIQUE (id) ); EOF; diff --git a/tests/Propel/Tests/Generator/Util/QuickBuilderTest.php b/tests/Propel/Tests/Generator/Util/QuickBuilderTest.php index 89fcf22430..69d70b7529 100644 --- a/tests/Propel/Tests/Generator/Util/QuickBuilderTest.php +++ b/tests/Propel/Tests/Generator/Util/QuickBuilderTest.php @@ -80,7 +80,8 @@ public function testGetSQL($builder) CREATE TABLE quick_build_foo_1 ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, - bar INTEGER + bar INTEGER, + UNIQUE (id) ); EOF; diff --git a/tests/Propel/Tests/Issues/Issue617Test.php b/tests/Propel/Tests/Issues/Issue617Test.php index 11195ead70..48d788a9cf 100644 --- a/tests/Propel/Tests/Issues/Issue617Test.php +++ b/tests/Propel/Tests/Issues/Issue617Test.php @@ -93,8 +93,8 @@ private function setupInitSchema() `full_name` VARCHAR(50) NOT NULL, `group_id` INTEGER, PRIMARY KEY (`id`), - INDEX `issue617_user_FI_1` (`group_id`), - CONSTRAINT `issue617_user_FK_1` + INDEX `issue617_user_fi_5936b3` (`group_id`), + CONSTRAINT `issue617_user_fk_5936b3` FOREIGN KEY (`group_id`) REFERENCES `issue617_group` (`id`) ON DELETE SET NULL @@ -150,9 +150,9 @@ private function dropForeignKey() $sql = $this->database->getPlatform()->getModifyDatabaseDDL($diff); $expected = ' -ALTER TABLE `issue617_user` DROP FOREIGN KEY `issue617_user_FK_1`; +ALTER TABLE `issue617_user` DROP FOREIGN KEY `issue617_user_fk_5936b3`; -DROP INDEX `issue617_user_FI_1` ON `issue617_user`; +DROP INDEX `issue617_user_fi_5936b3` ON `issue617_user`; ALTER TABLE `issue617_user` diff --git a/tests/Propel/Tests/Resources/blog-schema.xml b/tests/Propel/Tests/Resources/blog-schema.xml index ce88590752..c290e51535 100644 --- a/tests/Propel/Tests/Resources/blog-schema.xml +++ b/tests/Propel/Tests/Resources/blog-schema.xml @@ -21,10 +21,10 @@ - + - + @@ -71,7 +71,7 @@ - +