Skip to content

Commit 05bdefd

Browse files
[9.x] Enhance column modifying (#44101)
* enhance column modifying * revert changes on TimestampType * revert changes of binary type
1 parent 2ca2b16 commit 05bdefd

File tree

4 files changed

+74
-2
lines changed

4 files changed

+74
-2
lines changed

src/Illuminate/Database/Schema/Grammars/ChangeColumn.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ protected static function getDoctrineColumnChangeOptions(Fluent $fluent)
121121
{
122122
$options = ['type' => static::getDoctrineColumnType($fluent['type'])];
123123

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

@@ -152,10 +152,11 @@ protected static function getDoctrineColumnType($type)
152152
return Type::getType(match ($type) {
153153
'biginteger' => 'bigint',
154154
'smallinteger' => 'smallint',
155-
'mediumtext', 'longtext' => 'text',
155+
'tinytext', 'mediumtext', 'longtext' => 'text',
156156
'binary' => 'blob',
157157
'uuid' => 'guid',
158158
'char' => 'string',
159+
'double' => 'float',
159160
default => $type,
160161
});
161162
}
@@ -169,6 +170,7 @@ protected static function getDoctrineColumnType($type)
169170
protected static function calculateDoctrineTextLength($type)
170171
{
171172
return match ($type) {
173+
'tinyText' => 1,
172174
'mediumText' => 65535 + 1,
173175
'longText' => 16777215 + 1,
174176
default => 255 + 1,
@@ -197,6 +199,7 @@ protected static function doesntNeedCharacterOptions($type)
197199
'mediumInteger',
198200
'smallInteger',
199201
'time',
202+
'timestamp',
200203
'tinyInteger',
201204
]);
202205
}

tests/Database/DatabaseSchemaBlueprintIntegrationTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,29 @@ public function testChangingCharColumnsWork()
149149
$this->assertEquals($expected, $queries);
150150
}
151151

152+
public function testChangingDoubleColumnsWork()
153+
{
154+
$this->db->connection()->getSchemaBuilder()->create('products', function ($table) {
155+
$table->integer('price');
156+
});
157+
158+
$blueprint = new Blueprint('products', function ($table) {
159+
$table->double('price')->change();
160+
});
161+
162+
$queries = $blueprint->toSql($this->db->connection(), new SQLiteGrammar);
163+
164+
$expected = [
165+
'CREATE TEMPORARY TABLE __temp__products AS SELECT price FROM products',
166+
'DROP TABLE products',
167+
'CREATE TABLE products (price DOUBLE PRECISION NOT NULL)',
168+
'INSERT INTO products (price) SELECT price FROM __temp__products',
169+
'DROP TABLE __temp__products',
170+
];
171+
172+
$this->assertEquals($expected, $queries);
173+
}
174+
152175
public function testRenameIndexWorks()
153176
{
154177
$this->db->connection()->getSchemaBuilder()->create('users', function ($table) {

tests/Integration/Database/DBAL/TimestampTypeTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,25 @@ public function testChangeTimestampColumnToDatetimeColumn()
5959
? $this->assertSame('timestamp', Schema::getColumnType('test', 'timestamp_to_datetime'))
6060
: $this->assertSame('datetime', Schema::getColumnType('test', 'timestamp_to_datetime'));
6161
}
62+
63+
public function testChangeStringColumnToTimestampColumn()
64+
{
65+
if ($this->driver !== 'mysql') {
66+
$this->markTestSkipped('Test requires a MySQL connection.');
67+
}
68+
69+
Schema::create('test', function (Blueprint $table) {
70+
$table->string('string_to_timestamp');
71+
});
72+
73+
$blueprint = new Blueprint('test', function ($table) {
74+
$table->timestamp('string_to_timestamp')->nullable(true)->change();
75+
});
76+
77+
$queries = $blueprint->toSql($this->getConnection(), $this->getConnection()->getSchemaGrammar());
78+
79+
$expected = ['ALTER TABLE test CHANGE string_to_timestamp string_to_timestamp TIMESTAMP NULL DEFAULT NULL'];
80+
81+
$this->assertEquals($expected, $queries);
82+
}
6283
}

tests/Integration/Database/SchemaBuilderTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,29 @@ public function testRegisterCustomDoctrineTypeASecondTime()
8787
$this->assertArrayHasKey(TinyInteger::NAME, Type::getTypesMap());
8888
$this->assertSame('tinyinteger', Schema::getColumnType('test', 'test_column'));
8989
}
90+
91+
public function testChangeToTextColumn()
92+
{
93+
if ($this->driver !== 'mysql') {
94+
$this->markTestSkipped('Test requires a MySQL connection.');
95+
}
96+
97+
Schema::create('test', function (Blueprint $table) {
98+
$table->integer('test_column');
99+
});
100+
101+
foreach (['tinyText', 'text', 'mediumText', 'longText'] as $type) {
102+
$blueprint = new Blueprint('test', function ($table) use ($type) {
103+
$table->$type('test_column')->change();
104+
});
105+
106+
$queries = $blueprint->toSql($this->getConnection(), $this->getConnection()->getSchemaGrammar());
107+
108+
$uppercase = strtoupper($type);
109+
110+
$expected = ["ALTER TABLE test CHANGE test_column test_column $uppercase NOT NULL"];
111+
112+
$this->assertEquals($expected, $queries);
113+
}
114+
}
90115
}

0 commit comments

Comments
 (0)