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

[7.x] Added renameColumn() enum support #32205

Merged
merged 3 commits into from
Apr 6, 2020
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
8 changes: 6 additions & 2 deletions src/Illuminate/Database/Schema/Grammars/ChangeColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,16 @@ public static function compile($grammar, Blueprint $blueprint, Fluent $command,
));
}

$schema = $connection->getDoctrineSchemaManager();
$databasePlatform = $schema->getDatabasePlatform();
$databasePlatform->registerDoctrineTypeMapping('enum', 'string');

$tableDiff = static::getChangedDiff(
$grammar, $blueprint, $schema = $connection->getDoctrineSchemaManager()
$grammar, $blueprint, $schema
);

if ($tableDiff !== false) {
return (array) $schema->getDatabasePlatform()->getAlterTableSQL($tableDiff);
return (array) $databasePlatform->getAlterTableSQL($tableDiff);
}

return [];
Expand Down
8 changes: 5 additions & 3 deletions src/Illuminate/Database/Schema/Grammars/RenameColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ class RenameColumn
*/
public static function compile(Grammar $grammar, Blueprint $blueprint, Fluent $command, Connection $connection)
{
$schema = $connection->getDoctrineSchemaManager();
$databasePlatform = $schema->getDatabasePlatform();
$databasePlatform->registerDoctrineTypeMapping('enum', 'string');

$column = $connection->getDoctrineColumn(
$grammar->getTablePrefix().$blueprint->getTable(), $command->from
);

$schema = $connection->getDoctrineSchemaManager();

return (array) $schema->getDatabasePlatform()->getAlterTableSQL(static::getRenamedDiff(
return (array) $databasePlatform->getAlterTableSQL(static::getRenamedDiff(
$grammar, $blueprint, $command, $column, $schema
));
}
Expand Down
11 changes: 6 additions & 5 deletions tests/Database/DatabaseSchemaBuilderIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Container\Container;
use Illuminate\Database\Capsule\Manager as DB;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Facade;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -40,12 +41,12 @@ protected function tearDown(): void

public function testDropAllTablesWorksWithForeignKeys()
{
$this->db->connection()->getSchemaBuilder()->create('table1', function ($table) {
$this->db->connection()->getSchemaBuilder()->create('table1', function (Blueprint $table) {
$table->integer('id');
$table->string('name');
});

$this->db->connection()->getSchemaBuilder()->create('table2', function ($table) {
$this->db->connection()->getSchemaBuilder()->create('table2', function (Blueprint $table) {
$table->integer('id');
$table->string('user_id');
$table->foreign('user_id')->references('id')->on('table1');
Expand All @@ -64,7 +65,7 @@ public function testHasColumnWithTablePrefix()
{
$this->db->connection()->setTablePrefix('test_');

$this->db->connection()->getSchemaBuilder()->create('table1', function ($table) {
$this->db->connection()->getSchemaBuilder()->create('table1', function (Blueprint $table) {
$table->integer('id');
$table->string('name');
});
Expand All @@ -81,7 +82,7 @@ public function testHasColumnAndIndexWithPrefixIndexDisabled()
'prefix_indexes' => false,
]);

$this->db->connection()->getSchemaBuilder()->create('table1', function ($table) {
$this->db->connection()->getSchemaBuilder()->create('table1', function (Blueprint $table) {
$table->integer('id');
$table->string('name')->index();
});
Expand All @@ -98,7 +99,7 @@ public function testHasColumnAndIndexWithPrefixIndexEnabled()
'prefix_indexes' => true,
]);

$this->db->connection()->getSchemaBuilder()->create('table1', function ($table) {
$this->db->connection()->getSchemaBuilder()->create('table1', function (Blueprint $table) {
$table->integer('id');
$table->string('name')->index();
});
Expand Down
14 changes: 1 addition & 13 deletions tests/Integration/Database/DatabaseMySqlConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,18 @@
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Orchestra\Testbench\TestCase;

class DatabaseMySqlConnectionTest extends TestCase
class DatabaseMySqlConnectionTest extends DatabaseMySqlTestCase
{
const TABLE = 'player';
const FLOAT_COL = 'float_col';
const JSON_COL = 'json_col';

const FLOAT_VAL = 0.2;

protected function getEnvironmentSetUp($app)
{
$app['config']->set('app.debug', 'true');
$app['config']->set('database.default', 'mysql');
}

protected function setUp(): void
{
parent::setUp();

if (! isset($_SERVER['CI'])) {
$this->markTestSkipped('This test is only executed on CI.');
}

if (! Schema::hasTable(self::TABLE)) {
Schema::create(self::TABLE, function (Blueprint $table) {
$table->json(self::JSON_COL)->nullable();
Expand Down
23 changes: 23 additions & 0 deletions tests/Integration/Database/DatabaseMySqlTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Illuminate\Tests\Integration\Database;

use Orchestra\Testbench\TestCase;

abstract class DatabaseMySqlTestCase extends TestCase
{
protected function getEnvironmentSetUp($app)
{
$app['config']->set('app.debug', 'true');
$app['config']->set('database.default', 'mysql');
}

protected function setUp(): void
{
parent::setUp();

if (! isset($_SERVER['CI'])) {
$this->markTestSkipped('This test is only executed on CI.');
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Illuminate\Tests\Integration\Database;

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class DatabaseSchemaBuilderAlterTableWithEnumTest extends DatabaseMySqlTestCase
{
public function testRenameColumnOnTableWithEnum()
{
Schema::table('users', function (Blueprint $table) {
$table->renameColumn('name', 'username');
});

$this->assertTrue(Schema::hasColumn('users', 'username'));
}

public function testChangeColumnOnTableWithEnum()
{
Schema::table('users', function (Blueprint $table) {
$table->unsignedInteger('age')->charset('')->change();
});

$this->assertEquals('integer', Schema::getColumnType('users', 'age'));
}

protected function setUp(): void
{
parent::setUp();

Schema::create('users', function (Blueprint $table) {
$table->integer('id');
$table->string('name');
$table->string('age');
$table->enum('color', ['red', 'blue']);
});
}

protected function tearDown(): void
{
Schema::drop('users');

parent::tearDown();
}
}