Skip to content

Support renaming columns in migrations #2682

New issue

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

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

Already on GitHub? # to your account

Merged
merged 5 commits into from
Nov 22, 2023
Merged
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
11 changes: 10 additions & 1 deletion src/Schema/Blueprint.php
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@
namespace MongoDB\Laravel\Schema;

use Illuminate\Database\Connection;
use Illuminate\Database\Schema\Blueprint as SchemaBlueprint;
use MongoDB\Laravel\Collection;

use function array_flip;
@@ -15,7 +16,7 @@
use function is_string;
use function key;

class Blueprint extends \Illuminate\Database\Schema\Blueprint
class Blueprint extends SchemaBlueprint
{
/**
* The MongoConnection object for this blueprint.
@@ -276,6 +277,14 @@ public function drop()
$this->collection->drop();
}

/** @inheritdoc */
public function renameColumn($from, $to)
{
$this->collection->updateMany([$from => ['$exists' => true]], ['$rename' => [$from => $to]]);

return $this;
}

Comment on lines +280 to +287
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First, it finds the documents that have the $from column and then runs a query to rename their column to the $to.

/** @inheritdoc */
public function addColumn($type, $name, array $parameters = [])
{
40 changes: 40 additions & 0 deletions tests/SchemaTest.php
Original file line number Diff line number Diff line change
@@ -337,6 +337,46 @@ public function testSparseUnique(): void
$this->assertEquals(1, $index['unique']);
}

public function testRenameColumn(): void
{
DB::connection()->collection('newcollection')->insert(['test' => 'value']);
DB::connection()->collection('newcollection')->insert(['test' => 'value 2']);
DB::connection()->collection('newcollection')->insert(['column' => 'column value']);

$check = DB::connection()->collection('newcollection')->get();
$this->assertCount(3, $check);

$this->assertArrayHasKey('test', $check[0]);
$this->assertArrayNotHasKey('newtest', $check[0]);

$this->assertArrayHasKey('test', $check[1]);
$this->assertArrayNotHasKey('newtest', $check[1]);

$this->assertArrayHasKey('column', $check[2]);
$this->assertArrayNotHasKey('test', $check[2]);
$this->assertArrayNotHasKey('newtest', $check[2]);

Schema::collection('newcollection', function (Blueprint $collection) {
$collection->renameColumn('test', 'newtest');
});

$check2 = DB::connection()->collection('newcollection')->get();
$this->assertCount(3, $check2);

$this->assertArrayHasKey('newtest', $check2[0]);
$this->assertArrayNotHasKey('test', $check2[0]);
$this->assertSame($check[0]['test'], $check2[0]['newtest']);

$this->assertArrayHasKey('newtest', $check2[1]);
$this->assertArrayNotHasKey('test', $check2[1]);
$this->assertSame($check[1]['test'], $check2[1]['newtest']);

$this->assertArrayHasKey('column', $check2[2]);
$this->assertArrayNotHasKey('test', $check2[2]);
$this->assertArrayNotHasKey('newtest', $check2[2]);
$this->assertSame($check[2]['column'], $check2[2]['column']);
}

protected function getIndex(string $collection, string $name)
{
$collection = DB::getCollection($collection);