Skip to content

Commit a5bf1bc

Browse files
[11.x] Fix modifying columns with default value and other minor schema enhancements (#49897)
* add support for srid on mariadb * fix modifying a column with default * fix renaming a column with default on lagacy mysql * use getTableListing * fix phpdoc on column definition * use getServerVersion * formatting
1 parent bc753cf commit a5bf1bc

File tree

8 files changed

+58
-9
lines changed

8 files changed

+58
-9
lines changed

src/Illuminate/Database/Query/Grammars/MySqlGrammar.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Illuminate\Database\Query\Builder;
66
use Illuminate\Support\Str;
7-
use PDO;
87

98
class MySqlGrammar extends Grammar
109
{
@@ -116,7 +115,7 @@ protected function compileGroupLimit(Builder $query)
116115
*/
117116
public function useLegacyGroupLimit(Builder $query)
118117
{
119-
$version = $query->getConnection()->getReadPdo()->getAttribute(PDO::ATTR_SERVER_VERSION);
118+
$version = $query->getConnection()->getServerVersion();
120119

121120
return ! $query->getConnection()->isMaria() && version_compare($version, '8.0.11') < 0;
122121
}

src/Illuminate/Database/Query/Grammars/SQLiteGrammar.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use Illuminate\Database\Query\Builder;
66
use Illuminate\Support\Arr;
77
use Illuminate\Support\Str;
8-
use PDO;
98

109
class SQLiteGrammar extends Grammar
1110
{
@@ -193,7 +192,7 @@ protected function compileJsonContainsKey($column)
193192
*/
194193
protected function compileGroupLimit(Builder $query)
195194
{
196-
$version = $query->getConnection()->getReadPdo()->getAttribute(PDO::ATTR_SERVER_VERSION);
195+
$version = $query->getConnection()->getServerVersion();
197196

198197
if (version_compare($version, '3.25.0') >= 0) {
199198
return parent::compileGroupLimit($query);

src/Illuminate/Database/Schema/ColumnDefinition.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* @method $this autoIncrement() Set INTEGER columns as auto-increment (primary key)
1111
* @method $this change() Change the column
1212
* @method $this charset(string $charset) Specify a character set for the column (MySQL)
13-
* @method $this collation(string $collation) Specify a collation for the column (MySQL/PostgreSQL/SQL Server)
13+
* @method $this collation(string $collation) Specify a collation for the column
1414
* @method $this comment(string $comment) Add a comment to the column (MySQL/PostgreSQL)
1515
* @method $this default(mixed $value) Specify a "default" value for the column
1616
* @method $this first() Place the column "first" in the table (MySQL)

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,9 @@ public function compileRenameColumn(Blueprint $blueprint, Fluent $command, Conne
327327
default => $column['type_name'],
328328
},
329329
'nullable' => $column['nullable'],
330-
'default' => $column['default'],
330+
'default' => $column['default'] && str_starts_with(strtolower($column['default']), 'current_timestamp')
331+
? new Expression($column['default'])
332+
: $column['default'],
331333
'autoIncrement' => $column['auto_increment'],
332334
'collation' => $column['collation'],
333335
'comment' => $column['comment'],
@@ -1039,7 +1041,11 @@ protected function typeGeometry(Fluent $column)
10391041

10401042
return sprintf('%s%s',
10411043
$subtype ?? 'geometry',
1042-
$column->srid ? ' srid '.$column->srid : ''
1044+
match (true) {
1045+
$column->srid && $this->connection?->isMaria() => ' ref_system_id='.$column->srid,
1046+
(bool) $column->srid => ' srid '.$column->srid,
1047+
default => '',
1048+
}
10431049
);
10441050
}
10451051

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ public function compileChange(Blueprint $blueprint, Fluent $command, Connection
264264
'change' => true,
265265
'type' => $column['type_name'],
266266
'nullable' => $column['nullable'],
267-
'default' => $column['default'],
267+
'default' => $column['default'] ? new Expression($column['default']) : null,
268268
'autoIncrement' => $column['auto_increment'],
269269
'collation' => $column['collation'],
270270
'comment' => $column['comment'],

src/Illuminate/Foundation/Testing/DatabaseTruncation.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ protected function truncateTablesForConnection(ConnectionInterface $connection,
8383

8484
$connection->unsetEventDispatcher();
8585

86-
collect(static::$allTables[$name] ??= array_column($connection->getSchemaBuilder()->getTables(), 'name'))
86+
collect(static::$allTables[$name] ??= $connection->getSchemaBuilder()->getTableListing())
8787
->when(
8888
property_exists($this, 'tablesToTruncate'),
8989
fn ($tables) => $tables->intersect($this->tablesToTruncate),

tests/Integration/Database/DatabaseSchemaBlueprintTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,31 @@ public function testChangingDoubleColumnsWork()
320320
$this->assertEquals($expected, $queries);
321321
}
322322

323+
public function testChangingColumnsWithDefaultWorks()
324+
{
325+
DB::connection()->getSchemaBuilder()->create('products', function (Blueprint $table) {
326+
$table->integer('changed_col');
327+
$table->timestamp('timestamp_col')->useCurrent();
328+
$table->integer('integer_col')->default(123);
329+
$table->string('string_col')->default('value');
330+
});
331+
332+
$blueprint = new Blueprint('products', function ($table) {
333+
$table->text('changed_col')->change();
334+
});
335+
336+
$queries = $blueprint->toSql(DB::connection(), new SQLiteGrammar);
337+
338+
$expected = [
339+
'create table "__temp__products" ("changed_col" text not null, "timestamp_col" datetime not null default CURRENT_TIMESTAMP, "integer_col" integer not null default \'123\', "string_col" varchar not null default \'value\')',
340+
'insert into "__temp__products" ("changed_col", "timestamp_col", "integer_col", "string_col") select "changed_col", "timestamp_col", "integer_col", "string_col" from "products"',
341+
'drop table "products"',
342+
'alter table "__temp__products" rename to "products"',
343+
];
344+
345+
$this->assertEquals($expected, $queries);
346+
}
347+
323348
public function testRenameIndexWorks()
324349
{
325350
DB::connection()->getSchemaBuilder()->create('users', function ($table) {

tests/Integration/Database/SchemaBuilderTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,26 @@ public function testChangeTextColumnToTextColumn()
111111
}
112112
}
113113

114+
public function testRenameColumnWithDefault()
115+
{
116+
Schema::create('test', static function (Blueprint $table) {
117+
$table->timestamp('foo')->useCurrent();
118+
$table->string('bar')->default('value');
119+
});
120+
121+
$columns = Schema::getColumns('test');
122+
$defaultFoo = collect($columns)->firstWhere('name', 'foo')['default'];
123+
$defaultBar = collect($columns)->firstWhere('name', 'bar')['default'];
124+
125+
Schema::table('test', static function (Blueprint $table) {
126+
$table->renameColumn('foo', 'new_foo');
127+
$table->renameColumn('bar', 'new_bar');
128+
});
129+
130+
$this->assertEquals(collect(Schema::getColumns('test'))->firstWhere('name', 'new_foo')['default'], $defaultFoo);
131+
$this->assertEquals(collect(Schema::getColumns('test'))->firstWhere('name', 'new_bar')['default'], $defaultBar);
132+
}
133+
114134
public function testGetTables()
115135
{
116136
Schema::create('foo', function (Blueprint $table) {

0 commit comments

Comments
 (0)