Skip to content

Commit 44507de

Browse files
hans-thomasTreggats
authored andcommitted
Support renaming columns in migrations (mongodb#2682)
1 parent 36aa80e commit 44507de

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

src/Schema/Blueprint.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace MongoDB\Laravel\Schema;
66

77
use Illuminate\Database\Connection;
8+
use Illuminate\Database\Schema\Blueprint as SchemaBlueprint;
89
use MongoDB\Laravel\Collection;
910

1011
use function array_flip;
@@ -15,7 +16,7 @@
1516
use function is_string;
1617
use function key;
1718

18-
class Blueprint extends \Illuminate\Database\Schema\Blueprint
19+
class Blueprint extends SchemaBlueprint
1920
{
2021
/**
2122
* The MongoConnection object for this blueprint.
@@ -280,6 +281,14 @@ public function drop()
280281
return $this;
281282
}
282283

284+
/** @inheritdoc */
285+
public function renameColumn($from, $to)
286+
{
287+
$this->collection->updateMany([$from => ['$exists' => true]], ['$rename' => [$from => $to]]);
288+
289+
return $this;
290+
}
291+
283292
/** @inheritdoc */
284293
public function addColumn($type, $name, array $parameters = [])
285294
{

tests/SchemaTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,46 @@ public function testSparseUnique(): void
337337
$this->assertEquals(1, $index['unique']);
338338
}
339339

340+
public function testRenameColumn(): void
341+
{
342+
DB::connection()->collection('newcollection')->insert(['test' => 'value']);
343+
DB::connection()->collection('newcollection')->insert(['test' => 'value 2']);
344+
DB::connection()->collection('newcollection')->insert(['column' => 'column value']);
345+
346+
$check = DB::connection()->collection('newcollection')->get();
347+
$this->assertCount(3, $check);
348+
349+
$this->assertArrayHasKey('test', $check[0]);
350+
$this->assertArrayNotHasKey('newtest', $check[0]);
351+
352+
$this->assertArrayHasKey('test', $check[1]);
353+
$this->assertArrayNotHasKey('newtest', $check[1]);
354+
355+
$this->assertArrayHasKey('column', $check[2]);
356+
$this->assertArrayNotHasKey('test', $check[2]);
357+
$this->assertArrayNotHasKey('newtest', $check[2]);
358+
359+
Schema::collection('newcollection', function (Blueprint $collection) {
360+
$collection->renameColumn('test', 'newtest');
361+
});
362+
363+
$check2 = DB::connection()->collection('newcollection')->get();
364+
$this->assertCount(3, $check2);
365+
366+
$this->assertArrayHasKey('newtest', $check2[0]);
367+
$this->assertArrayNotHasKey('test', $check2[0]);
368+
$this->assertSame($check[0]['test'], $check2[0]['newtest']);
369+
370+
$this->assertArrayHasKey('newtest', $check2[1]);
371+
$this->assertArrayNotHasKey('test', $check2[1]);
372+
$this->assertSame($check[1]['test'], $check2[1]['newtest']);
373+
374+
$this->assertArrayHasKey('column', $check2[2]);
375+
$this->assertArrayNotHasKey('test', $check2[2]);
376+
$this->assertArrayNotHasKey('newtest', $check2[2]);
377+
$this->assertSame($check[2]['column'], $check2[2]['column']);
378+
}
379+
340380
protected function getIndex(string $collection, string $name)
341381
{
342382
$collection = DB::getCollection($collection);

0 commit comments

Comments
 (0)