Skip to content

Commit

Permalink
Quotes the table name for add / drop constraints operation and add te…
Browse files Browse the repository at this point in the history
…sts to those methods
  • Loading branch information
HavokInspiration committed Oct 15, 2015
1 parent a34ac48 commit 8fc6323
Show file tree
Hide file tree
Showing 6 changed files with 363 additions and 12 deletions.
11 changes: 7 additions & 4 deletions src/Database/Schema/MysqlSchema.php
Expand Up @@ -394,13 +394,14 @@ public function constraintSql(Table $table, $name)
*/
public function addConstraintSql(Table $table)
{
$sqlPattern = 'ALTER TABLE %s ADD %s';
$sqlPattern = 'ALTER TABLE %s ADD %s;';
$sql = [];

foreach ($table->constraints() as $name) {
$constraint = $table->constraint($name);
if ($constraint['type'] === Table::CONSTRAINT_FOREIGN) {
$sql[] = sprintf($sqlPattern, $table->name(), $this->constraintSql($table, $name));
$tableName = $this->_driver->quoteIdentifier($table->name());
$sql[] = sprintf($sqlPattern, $tableName, $this->constraintSql($table, $name));
}
}

Expand All @@ -412,13 +413,15 @@ public function addConstraintSql(Table $table)
*/
public function dropConstraintSql(Table $table)
{
$sqlPattern = 'ALTER TABLE %s DROP FOREIGN KEY %s';
$sqlPattern = 'ALTER TABLE %s DROP FOREIGN KEY %s;';
$sql = [];

foreach ($table->constraints() as $name) {
$constraint = $table->constraint($name);
if ($constraint['type'] === Table::CONSTRAINT_FOREIGN) {
$sql[] = sprintf($sqlPattern, $table->name(), $name);
$tableName = $this->_driver->quoteIdentifier($table->name());
$constraintName = $this->_driver->quoteIdentifier($name);
$sql[] = sprintf($sqlPattern, $tableName, $constraintName);
}
}

Expand Down
11 changes: 7 additions & 4 deletions src/Database/Schema/PostgresSchema.php
Expand Up @@ -422,13 +422,14 @@ public function columnSql(Table $table, $name)
*/
public function addConstraintSql(Table $table)
{
$sqlPattern = 'ALTER TABLE %s ADD %s';
$sqlPattern = 'ALTER TABLE %s ADD %s;';
$sql = [];

foreach ($table->constraints() as $name) {
$constraint = $table->constraint($name);
if ($constraint['type'] === Table::CONSTRAINT_FOREIGN) {
$sql[] = sprintf($sqlPattern, $table->name(), $this->constraintSql($table, $name));
$tableName = $this->_driver->quoteIdentifier($table->name());
$sql[] = sprintf($sqlPattern, $tableName, $this->constraintSql($table, $name));
}
}

Expand All @@ -440,13 +441,15 @@ public function addConstraintSql(Table $table)
*/
public function dropConstraintSql(Table $table)
{
$sqlPattern = 'ALTER TABLE %s DROP CONSTRAINT %s';
$sqlPattern = 'ALTER TABLE %s DROP CONSTRAINT %s;';
$sql = [];

foreach ($table->constraints() as $name) {
$constraint = $table->constraint($name);
if ($constraint['type'] === Table::CONSTRAINT_FOREIGN) {
$sql[] = sprintf($sqlPattern, $table->name(), $name);
$tableName = $this->_driver->quoteIdentifier($table->name());
$constraintName = $this->_driver->quoteIdentifier($name);
$sql[] = sprintf($sqlPattern, $tableName, $constraintName);
}
}

Expand Down
11 changes: 7 additions & 4 deletions src/Database/Schema/SqlserverSchema.php
Expand Up @@ -370,13 +370,14 @@ public function columnSql(Table $table, $name)
*/
public function addConstraintSql(Table $table)
{
$sqlPattern = 'ALTER TABLE %s ADD %s';
$sqlPattern = 'ALTER TABLE %s ADD %s;';
$sql = [];

foreach ($table->constraints() as $name) {
$constraint = $table->constraint($name);
if ($constraint['type'] === Table::CONSTRAINT_FOREIGN) {
$sql[] = sprintf($sqlPattern, $table->name(), $this->constraintSql($table, $name));
$tableName = $this->_driver->quoteIdentifier($table->name());
$sql[] = sprintf($sqlPattern, $tableName, $this->constraintSql($table, $name));
}
}

Expand All @@ -388,13 +389,15 @@ public function addConstraintSql(Table $table)
*/
public function dropConstraintSql(Table $table)
{
$sqlPattern = 'ALTER TABLE %s DROP CONSTRAINT %s';
$sqlPattern = 'ALTER TABLE %s DROP CONSTRAINT %s;';
$sql = [];

foreach ($table->constraints() as $name) {
$constraint = $table->constraint($name);
if ($constraint['type'] === Table::CONSTRAINT_FOREIGN) {
$sql[] = sprintf($sqlPattern, $table->name(), $name);
$tableName = $this->_driver->quoteIdentifier($table->name());
$constraintName = $this->_driver->quoteIdentifier($name);
$sql[] = sprintf($sqlPattern, $tableName, $constraintName);
}
}

Expand Down
114 changes: 114 additions & 0 deletions tests/TestCase/Database/Schema/MysqlSchemaTest.php
Expand Up @@ -712,6 +712,120 @@ public function testIndexSql($name, $data, $expected)
$this->assertEquals($expected, $schema->indexSql($table, $name));
}

/**
* Test the addConstraintSql method.
*
* @return void
*/
public function testAddConstraintSql()
{
$driver = $this->_getMockedDriver();
$connection = $this->getMock('Cake\Database\Connection', [], [], '', false);
$connection->expects($this->any())->method('driver')
->will($this->returnValue($driver));

$table = (new Table('posts'))
->addColumn('author_id', [
'type' => 'integer',
'null' => false
])
->addColumn('category_id', [
'type' => 'integer',
'null' => false
])
->addColumn('category_name', [
'type' => 'integer',
'null' => false
])
->addConstraint('author_fk', [
'type' => 'foreign',
'columns' => ['author_id'],
'references' => ['authors', 'id'],
'update' => 'cascade',
'delete' => 'cascade'
]);

$expected = <<<SQL
ALTER TABLE `posts` ADD CONSTRAINT `author_fk` FOREIGN KEY (`author_id`) REFERENCES `authors` (`id`) ON UPDATE CASCADE ON DELETE CASCADE;
SQL;
$result = $table->addConstraintSql($connection);
$this->assertCount(1, $result);
$this->assertTextEquals($expected, $result[0]);

$table
->addConstraint('category_fk', [
'type' => 'foreign',
'columns' => ['category_id', 'category_name'],
'references' => ['categories', ['id', 'name']],
'update' => 'cascade',
'delete' => 'cascade'
]);

$expected = <<<SQL
ALTER TABLE `posts` ADD CONSTRAINT `category_fk` FOREIGN KEY (`category_id`, `category_name`) REFERENCES `categories` (`id`, `name`) ON UPDATE CASCADE ON DELETE CASCADE;
SQL;
$result = $table->addConstraintSql($connection);
$this->assertCount(2, $result);
$this->assertTextEquals($expected, $result[1]);
}

/**
* Test the dropConstraintSql method.
*
* @return void
*/
public function testDropConstraintSql()
{
$driver = $this->_getMockedDriver();
$connection = $this->getMock('Cake\Database\Connection', [], [], '', false);
$connection->expects($this->any())->method('driver')
->will($this->returnValue($driver));

$table = (new Table('posts'))
->addColumn('author_id', [
'type' => 'integer',
'null' => false
])
->addColumn('category_id', [
'type' => 'integer',
'null' => false
])
->addColumn('category_name', [
'type' => 'integer',
'null' => false
])
->addConstraint('author_fk', [
'type' => 'foreign',
'columns' => ['author_id'],
'references' => ['authors', 'id'],
'update' => 'cascade',
'delete' => 'cascade'
]);

$expected = <<<SQL
ALTER TABLE `posts` DROP FOREIGN KEY `author_fk`;
SQL;
$result = $table->dropConstraintSql($connection);
$this->assertCount(1, $result);
$this->assertTextEquals($expected, $result[0]);

$table
->addConstraint('category_fk', [
'type' => 'foreign',
'columns' => ['category_id', 'category_name'],
'references' => ['categories', ['id', 'name']],
'update' => 'cascade',
'delete' => 'cascade'
]);

$expected = <<<SQL
ALTER TABLE `posts` DROP FOREIGN KEY `category_fk`;
SQL;
$result = $table->dropConstraintSql($connection);
$this->assertCount(2, $result);
$this->assertTextEquals($expected, $result[1]);
}

/**
* Test generating a column that is a primary key.
*
Expand Down
114 changes: 114 additions & 0 deletions tests/TestCase/Database/Schema/PostgresSchemaTest.php
Expand Up @@ -795,6 +795,120 @@ public function testConstraintSql($name, $data, $expected)
$this->assertTextEquals($expected, $schema->constraintSql($table, $name));
}

/**
* Test the addConstraintSql method.
*
* @return void
*/
public function testAddConstraintSql()
{
$driver = $this->_getMockedDriver();
$connection = $this->getMock('Cake\Database\Connection', [], [], '', false);
$connection->expects($this->any())->method('driver')
->will($this->returnValue($driver));

$table = (new Table('posts'))
->addColumn('author_id', [
'type' => 'integer',
'null' => false
])
->addColumn('category_id', [
'type' => 'integer',
'null' => false
])
->addColumn('category_name', [
'type' => 'integer',
'null' => false
])
->addConstraint('author_fk', [
'type' => 'foreign',
'columns' => ['author_id'],
'references' => ['authors', 'id'],
'update' => 'cascade',
'delete' => 'cascade'
]);

$expected = <<<SQL
ALTER TABLE "posts" ADD CONSTRAINT "author_fk" FOREIGN KEY ("author_id") REFERENCES "authors" ("id") ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE INITIALLY IMMEDIATE;
SQL;
$result = $table->addConstraintSql($connection);
$this->assertCount(1, $result);
$this->assertTextEquals($expected, $result[0]);

$table
->addConstraint('category_fk', [
'type' => 'foreign',
'columns' => ['category_id', 'category_name'],
'references' => ['categories', ['id', 'name']],
'update' => 'cascade',
'delete' => 'cascade'
]);

$expected = <<<SQL
ALTER TABLE "posts" ADD CONSTRAINT "category_fk" FOREIGN KEY ("category_id", "category_name") REFERENCES "categories" ("id", "name") ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE INITIALLY IMMEDIATE;
SQL;
$result = $table->addConstraintSql($connection);
$this->assertCount(2, $result);
$this->assertTextEquals($expected, $result[1]);
}

/**
* Test the dropConstraintSql method.
*
* @return void
*/
public function testDropConstraintSql()
{
$driver = $this->_getMockedDriver();
$connection = $this->getMock('Cake\Database\Connection', [], [], '', false);
$connection->expects($this->any())->method('driver')
->will($this->returnValue($driver));

$table = (new Table('posts'))
->addColumn('author_id', [
'type' => 'integer',
'null' => false
])
->addColumn('category_id', [
'type' => 'integer',
'null' => false
])
->addColumn('category_name', [
'type' => 'integer',
'null' => false
])
->addConstraint('author_fk', [
'type' => 'foreign',
'columns' => ['author_id'],
'references' => ['authors', 'id'],
'update' => 'cascade',
'delete' => 'cascade'
]);

$expected = <<<SQL
ALTER TABLE "posts" DROP CONSTRAINT "author_fk";
SQL;
$result = $table->dropConstraintSql($connection);
$this->assertCount(1, $result);
$this->assertTextEquals($expected, $result[0]);

$table
->addConstraint('category_fk', [
'type' => 'foreign',
'columns' => ['category_id', 'category_name'],
'references' => ['categories', ['id', 'name']],
'update' => 'cascade',
'delete' => 'cascade'
]);

$expected = <<<SQL
ALTER TABLE "posts" DROP CONSTRAINT "category_fk";
SQL;
$result = $table->dropConstraintSql($connection);
$this->assertCount(2, $result);
$this->assertTextEquals($expected, $result[1]);
}

/**
* Integration test for converting a Schema\Table into MySQL table creates.
*
Expand Down

0 comments on commit 8fc6323

Please sign in to comment.