Skip to content

Commit

Permalink
[9.x] Enhance column modifying (#44101)
Browse files Browse the repository at this point in the history
* enhance column modifying

* revert changes on TimestampType

* revert changes of binary type
  • Loading branch information
hafezdivandari authored Sep 13, 2022
1 parent 2ca2b16 commit 05bdefd
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/Illuminate/Database/Schema/Grammars/ChangeColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ protected static function getDoctrineColumnChangeOptions(Fluent $fluent)
{
$options = ['type' => static::getDoctrineColumnType($fluent['type'])];

if (in_array($fluent['type'], ['text', 'mediumText', 'longText'])) {
if (in_array($fluent['type'], ['tinyText', 'text', 'mediumText', 'longText'])) {
$options['length'] = static::calculateDoctrineTextLength($fluent['type']);
}

Expand Down Expand Up @@ -152,10 +152,11 @@ protected static function getDoctrineColumnType($type)
return Type::getType(match ($type) {
'biginteger' => 'bigint',
'smallinteger' => 'smallint',
'mediumtext', 'longtext' => 'text',
'tinytext', 'mediumtext', 'longtext' => 'text',
'binary' => 'blob',
'uuid' => 'guid',
'char' => 'string',
'double' => 'float',
default => $type,
});
}
Expand All @@ -169,6 +170,7 @@ protected static function getDoctrineColumnType($type)
protected static function calculateDoctrineTextLength($type)
{
return match ($type) {
'tinyText' => 1,
'mediumText' => 65535 + 1,
'longText' => 16777215 + 1,
default => 255 + 1,
Expand Down Expand Up @@ -197,6 +199,7 @@ protected static function doesntNeedCharacterOptions($type)
'mediumInteger',
'smallInteger',
'time',
'timestamp',
'tinyInteger',
]);
}
Expand Down
23 changes: 23 additions & 0 deletions tests/Database/DatabaseSchemaBlueprintIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,29 @@ public function testChangingCharColumnsWork()
$this->assertEquals($expected, $queries);
}

public function testChangingDoubleColumnsWork()
{
$this->db->connection()->getSchemaBuilder()->create('products', function ($table) {
$table->integer('price');
});

$blueprint = new Blueprint('products', function ($table) {
$table->double('price')->change();
});

$queries = $blueprint->toSql($this->db->connection(), new SQLiteGrammar);

$expected = [
'CREATE TEMPORARY TABLE __temp__products AS SELECT price FROM products',
'DROP TABLE products',
'CREATE TABLE products (price DOUBLE PRECISION NOT NULL)',
'INSERT INTO products (price) SELECT price FROM __temp__products',
'DROP TABLE __temp__products',
];

$this->assertEquals($expected, $queries);
}

public function testRenameIndexWorks()
{
$this->db->connection()->getSchemaBuilder()->create('users', function ($table) {
Expand Down
21 changes: 21 additions & 0 deletions tests/Integration/Database/DBAL/TimestampTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,25 @@ public function testChangeTimestampColumnToDatetimeColumn()
? $this->assertSame('timestamp', Schema::getColumnType('test', 'timestamp_to_datetime'))
: $this->assertSame('datetime', Schema::getColumnType('test', 'timestamp_to_datetime'));
}

public function testChangeStringColumnToTimestampColumn()
{
if ($this->driver !== 'mysql') {
$this->markTestSkipped('Test requires a MySQL connection.');
}

Schema::create('test', function (Blueprint $table) {
$table->string('string_to_timestamp');
});

$blueprint = new Blueprint('test', function ($table) {
$table->timestamp('string_to_timestamp')->nullable(true)->change();
});

$queries = $blueprint->toSql($this->getConnection(), $this->getConnection()->getSchemaGrammar());

$expected = ['ALTER TABLE test CHANGE string_to_timestamp string_to_timestamp TIMESTAMP NULL DEFAULT NULL'];

$this->assertEquals($expected, $queries);
}
}
25 changes: 25 additions & 0 deletions tests/Integration/Database/SchemaBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,29 @@ public function testRegisterCustomDoctrineTypeASecondTime()
$this->assertArrayHasKey(TinyInteger::NAME, Type::getTypesMap());
$this->assertSame('tinyinteger', Schema::getColumnType('test', 'test_column'));
}

public function testChangeToTextColumn()
{
if ($this->driver !== 'mysql') {
$this->markTestSkipped('Test requires a MySQL connection.');
}

Schema::create('test', function (Blueprint $table) {
$table->integer('test_column');
});

foreach (['tinyText', 'text', 'mediumText', 'longText'] as $type) {
$blueprint = new Blueprint('test', function ($table) use ($type) {
$table->$type('test_column')->change();
});

$queries = $blueprint->toSql($this->getConnection(), $this->getConnection()->getSchemaGrammar());

$uppercase = strtoupper($type);

$expected = ["ALTER TABLE test CHANGE test_column test_column $uppercase NOT NULL"];

$this->assertEquals($expected, $queries);
}
}
}

0 comments on commit 05bdefd

Please sign in to comment.