Skip to content

Commit cc4070d

Browse files
xdroidteamhdomos
andauthored
add rename table support (#47)
* add rename table support * add test for renaming table Co-authored-by: Domonkos Horvath <domonkos.horvath@xdroid.com>
1 parent b54ca12 commit cc4070d

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

src/Schema/Grammar.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,4 +223,18 @@ protected function modifyIncrement(Blueprint $blueprint, Fluent $column)
223223
: ' auto_increment primary key';
224224
}
225225
}
226+
227+
/**
228+
* Compile a rename table command.
229+
*
230+
* @param \Illuminate\Database\Schema\Blueprint $blueprint
231+
* @param \Illuminate\Support\Fluent $command
232+
* @return string
233+
*/
234+
public function compileRename(Blueprint $blueprint, Fluent $command)
235+
{
236+
$from = $this->wrapTable($blueprint);
237+
238+
return "alter table {$from} rename to ".$this->wrapTable($command->to);
239+
}
226240
}

tests/Hybrid/RenameTableTest.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace SingleStore\Laravel\Tests\Hybrid;
4+
5+
use Illuminate\Support\Facades\DB;
6+
use Illuminate\Support\Facades\Schema;
7+
use SingleStore\Laravel\Schema\Blueprint;
8+
use SingleStore\Laravel\Tests\BaseTest;
9+
10+
class RenameTableTest extends BaseTest
11+
{
12+
use HybridTestHelpers;
13+
14+
protected function setUp(): void
15+
{
16+
parent::setUp();
17+
18+
if ($this->runHybridIntegrations()) {
19+
$this->createTable(function (Blueprint $table) {
20+
$table->id();
21+
});
22+
}
23+
}
24+
25+
protected function tearDown(): void
26+
{
27+
if ($this->runHybridIntegrations()) {
28+
Schema::dropIfExists('test_renamed');
29+
}
30+
31+
parent::tearDown();
32+
}
33+
34+
/** @test */
35+
public function rename_table()
36+
{
37+
if ($this->runHybridIntegrations()) {
38+
$cached = $this->mockDatabaseConnection;
39+
40+
$this->mockDatabaseConnection = false;
41+
42+
$this->assertFalse(Schema::hasTable('test_renamed'));
43+
Schema::rename('test', 'test_renamed');
44+
45+
$this->assertTrue(Schema::hasTable('test_renamed'));
46+
47+
$this->mockDatabaseConnection = $cached;
48+
}
49+
50+
$blueprint = new Blueprint('test');
51+
$blueprint->rename('test_renamed');
52+
53+
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
54+
55+
$this->assertCount(1, $statements);
56+
$this->assertEquals('alter table `test` rename to `test_renamed`', $statements[0]);
57+
58+
}
59+
}

0 commit comments

Comments
 (0)