Skip to content

Commit a9547c8

Browse files
authored
#56124 Properly escape column defaults (#56158)
1 parent 9eac15d commit a9547c8

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -478,12 +478,12 @@ protected function getDefaultValue($value)
478478
}
479479

480480
if ($value instanceof BackedEnum) {
481-
return "'{$value->value}'";
481+
return "'".str_replace("'", "''", $value->value)."'";
482482
}
483483

484484
return is_bool($value)
485485
? "'".(int) $value."'"
486-
: "'".(string) $value."'";
486+
: "'".str_replace("'", "''", $value)."'";
487487
}
488488

489489
/**

tests/Database/DatabaseSchemaBlueprintTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,45 @@ public function testTableComment()
616616
$this->assertEquals(['comment on table "posts" is \'Look at my comment, it is amazing\''], $getSql('Postgres'));
617617
}
618618

619+
public function testColumnDefault()
620+
{
621+
// Test a normal string literal column default.
622+
$getSql = function ($grammar) {
623+
return $this->getBlueprint($grammar, 'posts', function ($table) {
624+
$table->tinyText('note')->default('this will work');
625+
})->toSql();
626+
};
627+
628+
$this->assertEquals(['alter table `posts` add `note` tinytext not null default \'this will work\''], $getSql('MySql'));
629+
630+
// Test a string literal column default containing an apostrophe (#56124)
631+
$getSql = function ($grammar) {
632+
return $this->getBlueprint($grammar, 'posts', function ($table) {
633+
$table->tinyText('note')->default('this\'ll work too');
634+
})->toSql();
635+
};
636+
637+
$this->assertEquals(['alter table `posts` add `note` tinytext not null default \'this\'\'ll work too\''], $getSql('MySql'));
638+
639+
// Test a backed enumeration column default
640+
$getSql = function ($grammar) {
641+
return $this->getBlueprint($grammar, 'posts', function ($table) {
642+
$enum = ApostropheBackedEnum::ValueWithoutApostrophe;
643+
$table->tinyText('note')->default($enum);
644+
})->toSql();
645+
};
646+
$this->assertEquals(['alter table `posts` add `note` tinytext not null default \'this will work\''], $getSql('MySql'));
647+
648+
// Test a backed enumeration column default containing an apostrophe (#56124)
649+
$getSql = function ($grammar) {
650+
return $this->getBlueprint($grammar, 'posts', function ($table) {
651+
$enum = ApostropheBackedEnum::ValueWithApostrophe;
652+
$table->tinyText('note')->default($enum);
653+
})->toSql();
654+
};
655+
$this->assertEquals(['alter table `posts` add `note` tinytext not null default \'this\'\'ll work too\''], $getSql('MySql'));
656+
}
657+
619658
protected function getConnection(?string $grammar = null, string $prefix = '')
620659
{
621660
$connection = m::mock(Connection::class)
@@ -652,3 +691,9 @@ protected function getBlueprint(
652691
return new Blueprint($connection, $table, $callback);
653692
}
654693
}
694+
695+
enum ApostropheBackedEnum: string
696+
{
697+
case ValueWithoutApostrophe = 'this will work';
698+
case ValueWithApostrophe = 'this\'ll work too';
699+
}

0 commit comments

Comments
 (0)