Skip to content

[11.x] Fix modifying columns with default value and other minor schema enhancements #49897

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

Merged
merged 7 commits into from
Jan 30, 2024
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
3 changes: 1 addition & 2 deletions src/Illuminate/Database/Query/Grammars/MySqlGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Illuminate\Database\Query\Builder;
use Illuminate\Support\Str;
use PDO;

class MySqlGrammar extends Grammar
{
Expand Down Expand Up @@ -116,7 +115,7 @@ protected function compileGroupLimit(Builder $query)
*/
public function useLegacyGroupLimit(Builder $query)
{
$version = $query->getConnection()->getReadPdo()->getAttribute(PDO::ATTR_SERVER_VERSION);
$version = $query->getConnection()->getServerVersion();

return ! $query->getConnection()->isMaria() && version_compare($version, '8.0.11') < 0;
}
Expand Down
3 changes: 1 addition & 2 deletions src/Illuminate/Database/Query/Grammars/SQLiteGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use PDO;

class SQLiteGrammar extends Grammar
{
Expand Down Expand Up @@ -193,7 +192,7 @@ protected function compileJsonContainsKey($column)
*/
protected function compileGroupLimit(Builder $query)
{
$version = $query->getConnection()->getReadPdo()->getAttribute(PDO::ATTR_SERVER_VERSION);
$version = $query->getConnection()->getServerVersion();

if (version_compare($version, '3.25.0') >= 0) {
return parent::compileGroupLimit($query);
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Database/Schema/ColumnDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* @method $this autoIncrement() Set INTEGER columns as auto-increment (primary key)
* @method $this change() Change the column
* @method $this charset(string $charset) Specify a character set for the column (MySQL)
* @method $this collation(string $collation) Specify a collation for the column (MySQL/PostgreSQL/SQL Server)
* @method $this collation(string $collation) Specify a collation for the column
* @method $this comment(string $comment) Add a comment to the column (MySQL/PostgreSQL)
* @method $this default(mixed $value) Specify a "default" value for the column
* @method $this first() Place the column "first" in the table (MySQL)
Expand Down
10 changes: 8 additions & 2 deletions src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,9 @@ public function compileRenameColumn(Blueprint $blueprint, Fluent $command, Conne
default => $column['type_name'],
},
'nullable' => $column['nullable'],
'default' => $column['default'],
'default' => $column['default'] && str_starts_with(strtolower($column['default']), 'current_timestamp')
? new Expression($column['default'])
: $column['default'],
'autoIncrement' => $column['auto_increment'],
'collation' => $column['collation'],
'comment' => $column['comment'],
Expand Down Expand Up @@ -1039,7 +1041,11 @@ protected function typeGeometry(Fluent $column)

return sprintf('%s%s',
$subtype ?? 'geometry',
$column->srid ? ' srid '.$column->srid : ''
match (true) {
$column->srid && $this->connection?->isMaria() => ' ref_system_id='.$column->srid,
(bool) $column->srid => ' srid '.$column->srid,
default => '',
}
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Database/Schema/Grammars/SQLiteGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ public function compileChange(Blueprint $blueprint, Fluent $command, Connection
'change' => true,
'type' => $column['type_name'],
'nullable' => $column['nullable'],
'default' => $column['default'],
'default' => $column['default'] ? new Expression($column['default']) : null,
'autoIncrement' => $column['auto_increment'],
'collation' => $column['collation'],
'comment' => $column['comment'],
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Foundation/Testing/DatabaseTruncation.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ protected function truncateTablesForConnection(ConnectionInterface $connection,

$connection->unsetEventDispatcher();

collect(static::$allTables[$name] ??= array_column($connection->getSchemaBuilder()->getTables(), 'name'))
collect(static::$allTables[$name] ??= $connection->getSchemaBuilder()->getTableListing())
->when(
property_exists($this, 'tablesToTruncate'),
fn ($tables) => $tables->intersect($this->tablesToTruncate),
Expand Down
25 changes: 25 additions & 0 deletions tests/Integration/Database/DatabaseSchemaBlueprintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,31 @@ public function testChangingDoubleColumnsWork()
$this->assertEquals($expected, $queries);
}

public function testChangingColumnsWithDefaultWorks()
{
DB::connection()->getSchemaBuilder()->create('products', function (Blueprint $table) {
$table->integer('changed_col');
$table->timestamp('timestamp_col')->useCurrent();
$table->integer('integer_col')->default(123);
$table->string('string_col')->default('value');
});

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

$queries = $blueprint->toSql(DB::connection(), new SQLiteGrammar);

$expected = [
'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\')',
'insert into "__temp__products" ("changed_col", "timestamp_col", "integer_col", "string_col") select "changed_col", "timestamp_col", "integer_col", "string_col" from "products"',
'drop table "products"',
'alter table "__temp__products" rename to "products"',
];

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

public function testRenameIndexWorks()
{
DB::connection()->getSchemaBuilder()->create('users', function ($table) {
Expand Down
20 changes: 20 additions & 0 deletions tests/Integration/Database/SchemaBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,26 @@ public function testChangeTextColumnToTextColumn()
}
}

public function testRenameColumnWithDefault()
{
Schema::create('test', static function (Blueprint $table) {
$table->timestamp('foo')->useCurrent();
$table->string('bar')->default('value');
});

$columns = Schema::getColumns('test');
$defaultFoo = collect($columns)->firstWhere('name', 'foo')['default'];
$defaultBar = collect($columns)->firstWhere('name', 'bar')['default'];

Schema::table('test', static function (Blueprint $table) {
$table->renameColumn('foo', 'new_foo');
$table->renameColumn('bar', 'new_bar');
});

$this->assertEquals(collect(Schema::getColumns('test'))->firstWhere('name', 'new_foo')['default'], $defaultFoo);
$this->assertEquals(collect(Schema::getColumns('test'))->firstWhere('name', 'new_bar')['default'], $defaultBar);
}

public function testGetTables()
{
Schema::create('foo', function (Blueprint $table) {
Expand Down