Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.6] RenameIndex support for migrations #24147

Merged
merged 3 commits into from
May 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/Illuminate/Database/Schema/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,18 @@ public function renameColumn($from, $to)
return $this->addCommand('renameColumn', compact('from', 'to'));
}

/**
* Indicate that the given indexes should be renamed.
*
* @param string $from
* @param string $to
* @return \Illuminate\Support\Fluent
*/
public function renameIndex($from, $to)
{
return $this->addCommand('renameIndex', compact('from', 'to'));
}

/**
* Indicate that the given primary key should be dropped.
*
Expand Down
16 changes: 16 additions & 0 deletions src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,22 @@ public function compileRename(Blueprint $blueprint, Fluent $command)
return "rename table {$from} to ".$this->wrapTable($command->to);
}

/**
* Compile a rename index command.
*
* @param \Illuminate\Database\Schema\Blueprint $blueprint
* @param \Illuminate\Support\Fluent $command
* @return string
*/
public function compileRenameIndex(Blueprint $blueprint, Fluent $command)
{
return sprintf('alter table %s rename index %s to %s',
$this->wrapTable($blueprint),
$this->wrap($command->from),
$this->wrap($command->to)
);
}

/**
* Compile the SQL needed to drop all tables.
*
Expand Down
15 changes: 15 additions & 0 deletions src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,21 @@ public function compileRename(Blueprint $blueprint, Fluent $command)
return "alter table {$from} rename to ".$this->wrapTable($command->to);
}

/**
* Compile a rename index command.
*
* @param \Illuminate\Database\Schema\Blueprint $blueprint
* @param \Illuminate\Support\Fluent $command
* @return string
*/
public function compileRenameIndex(Blueprint $blueprint, Fluent $command)
{
return sprintf('alter index %s rename to %s',
$this->wrap($command->from),
$this->wrap($command->to)
);
}

/**
* Compile the command to enable foreign key constraints.
*
Expand Down
31 changes: 31 additions & 0 deletions src/Illuminate/Database/Schema/Grammars/SQLiteGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use RuntimeException;
use Illuminate\Support\Fluent;
use Doctrine\DBAL\Schema\Index;
use Illuminate\Database\Connection;
use Illuminate\Database\Schema\Blueprint;

Expand Down Expand Up @@ -307,6 +308,36 @@ public function compileRename(Blueprint $blueprint, Fluent $command)
return "alter table {$from} rename to ".$this->wrapTable($command->to);
}

/**
* Compile a rename index command.
*
* @param \Illuminate\Database\Schema\Blueprint $blueprint
* @param \Illuminate\Support\Fluent $command
* @param \Illuminate\Database\Connection $connection
* @return array
*/
public function compileRenameIndex(Blueprint $blueprint, Fluent $command, Connection $connection)
{
$schemaManager = $connection->getDoctrineSchemaManager();
$indexes = $schemaManager->listTableIndexes($this->getTablePrefix().$blueprint->getTable());
$index = array_get($indexes, $command->from);

if (! $index) {
throw new RuntimeException("Index '{$command->from}' doesn't seem to exist");
}

/** @var \Doctrine\DBAL\Schema\Index $index */
$newIndex = new Index($command->to, $index->getColumns(), $index->isUnique(), $index->isPrimary(),
$index->getFlags(), $index->getOptions());

$platform = $schemaManager->getDatabasePlatform();

return [
$platform->getDropIndexSQL($command->from, $this->getTablePrefix().$blueprint->getTable()),
$platform->getCreateIndexSQL($newIndex, $this->getTablePrefix().$blueprint->getTable()),
];
}

/**
* Compile the command to enable foreign key constraints.
*
Expand Down
15 changes: 15 additions & 0 deletions src/Illuminate/Database/Schema/Grammars/SqlServerGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,21 @@ public function compileRename(Blueprint $blueprint, Fluent $command)
return "sp_rename {$from}, ".$this->wrapTable($command->to);
}

/**
* Compile a rename index command.
*
* @param \Illuminate\Database\Schema\Blueprint $blueprint
* @param \Illuminate\Support\Fluent $command
* @return string
*/
public function compileRenameIndex(Blueprint $blueprint, Fluent $command)
{
return sprintf("sp_rename N'%s', %s, N'INDEX'",
$this->wrap($blueprint->getTable().'.'.$command->from),
$this->wrap($command->to)
);
}

/**
* Compile the command to enable foreign key constraints.
*
Expand Down
10 changes: 10 additions & 0 deletions tests/Database/DatabaseMySqlSchemaGrammarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,16 @@ public function testRenameTable()
$this->assertEquals('rename table `users` to `foo`', $statements[0]);
}

public function testRenameIndex()
{
$blueprint = new Blueprint('users');
$blueprint->renameIndex('foo', 'bar');
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());

$this->assertCount(1, $statements);
$this->assertEquals('alter table `users` rename index `foo` to `bar`', $statements[0]);
}

public function testAddingPrimaryKey()
{
$blueprint = new Blueprint('users');
Expand Down
10 changes: 10 additions & 0 deletions tests/Database/DatabasePostgresSchemaGrammarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,16 @@ public function testRenameTable()
$this->assertEquals('alter table "users" rename to "foo"', $statements[0]);
}

public function testRenameIndex()
{
$blueprint = new Blueprint('users');
$blueprint->renameIndex('foo', 'bar');
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());

$this->assertCount(1, $statements);
$this->assertEquals('alter index "foo" rename to "bar"', $statements[0]);
}

public function testAddingPrimaryKey()
{
$blueprint = new Blueprint('users');
Expand Down
41 changes: 41 additions & 0 deletions tests/Database/DatabaseSQLiteSchemaGrammarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,47 @@ public function testRenameTable()
$this->assertEquals('alter table "users" rename to "foo"', $statements[0]);
}

public function testRenameIndex()
{
if (! class_exists('Doctrine\DBAL\Schema\SqliteSchemaManager')) {
$this->markTestSkipped('Doctrine should be installed to run renameIndex tests');
}

$db = new \Illuminate\Database\Capsule\Manager;

$db->addConnection([
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => 'prefix_',
]);

$schema = $db->getConnection()->getSchemaBuilder();

$schema->create('users', function (Blueprint $table) {
$table->string('name');
$table->string('email');
});

$schema->table('users', function (Blueprint $table) {
$table->index(['name', 'email'], 'index1');
});

$manager = $db->getConnection()->getDoctrineSchemaManager();
$details = $manager->listTableDetails('prefix_users');
$this->assertTrue($details->hasIndex('index1'));
$this->assertFalse($details->hasIndex('index2'));

$schema->table('users', function (Blueprint $table) {
$table->renameIndex('index1', 'index2');
});

$details = $manager->listTableDetails('prefix_users');
$this->assertFalse($details->hasIndex('index1'));
$this->assertTrue($details->hasIndex('index2'));

$this->assertEquals(['name', 'email'], $details->getIndex('index2')->getUnquotedColumns());
}

public function testAddingPrimaryKey()
{
$blueprint = new Blueprint('users');
Expand Down
49 changes: 49 additions & 0 deletions tests/Database/DatabaseSchemaBlueprintIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,53 @@ public function testRenamingAndChangingColumnsWork()

$this->assertEquals($expected, $queries);
}

public function testRenameIndexWorks()
{
$this->db->connection()->getSchemaBuilder()->create('users', function ($table) {
$table->string('name');
$table->string('age');
});

$this->db->connection()->getSchemaBuilder()->table('users', function ($table) {
$table->index(['name'], 'index1');
});

$blueprint = new Blueprint('users', function ($table) {
$table->renameIndex('index1', 'index2');
});

$queries = $blueprint->toSql($this->db->connection(), new \Illuminate\Database\Schema\Grammars\SQLiteGrammar);

$expected = [
'DROP INDEX index1',
'CREATE INDEX index2 ON users (name)',
];

$this->assertEquals($expected, $queries);

$queries = $blueprint->toSql($this->db->connection(), new \Illuminate\Database\Schema\Grammars\SqlServerGrammar());

$expected = [
'sp_rename N\'"users"."index1"\', "index2", N\'INDEX\'',
];

$this->assertEquals($expected, $queries);

$queries = $blueprint->toSql($this->db->connection(), new \Illuminate\Database\Schema\Grammars\MySqlGrammar());

$expected = [
'alter table `users` rename index `index1` to `index2`',
];

$this->assertEquals($expected, $queries);

$queries = $blueprint->toSql($this->db->connection(), new \Illuminate\Database\Schema\Grammars\PostgresGrammar());

$expected = [
'alter index "index1" rename to "index2"',
];

$this->assertEquals($expected, $queries);
}
}
10 changes: 10 additions & 0 deletions tests/Database/DatabaseSqlServerSchemaGrammarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,16 @@ public function testRenameTable()
$this->assertEquals('sp_rename "users", "foo"', $statements[0]);
}

public function testRenameIndex()
{
$blueprint = new Blueprint('users');
$blueprint->renameIndex('foo', 'bar');
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());

$this->assertCount(1, $statements);
$this->assertEquals('sp_rename N\'"users"."foo"\', "bar", N\'INDEX\'', $statements[0]);
}

public function testAddingPrimaryKey()
{
$blueprint = new Blueprint('users');
Expand Down