Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit bcadf52

Browse files
Support renaming columns in migrations (#2682)
1 parent 1c2d1fe commit bcadf52

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.
@@ -276,6 +277,14 @@ public function drop()
276277
$this->collection->drop();
277278
}
278279

280+
/** @inheritdoc */
281+
public function renameColumn($from, $to)
282+
{
283+
$this->collection->updateMany([$from => ['$exists' => true]], ['$rename' => [$from => $to]]);
284+
285+
return $this;
286+
}
287+
279288
/** @inheritdoc */
280289
public function addColumn($type, $name, array $parameters = [])
281290
{

‎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)

AltStyle によって変換されたページ (->オリジナル) /