Skip to content

Commit 2e00673

Browse files
authored
Fix #295: Fix multiline and single quote in default string value, add support for PostgreSQL 9.4 parentheses around negative numeric default values
1 parent 0dd905b commit 2e00673

4 files changed

Lines changed: 19 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- Chg #288: Typecast refactoring (@Tigrov)
1010
- Chg #291: Update phpTypecast for bool type (@Tigrov)
1111
- Enh #294: Refactoring of `Schema::normalizeDefaultValue()` method (@Tigrov)
12+
- Bug #295: Fix multiline and single quote in default string value, add support for PostgreSQL 9.4 parentheses around negative numeric default values (@Tigrov)
1213
- Bug #296: Prevent posible issues with array default values `('{one,two}'::text[])::varchar[]`, remove `ArrayParser::parseString()` (@Tigrov)
1314

1415
## 1.0.0 April 12, 2023

src/Schema.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -863,7 +863,8 @@ private function normalizeDefaultValue(string|null $defaultValue, ColumnSchemaIn
863863
return new Expression($defaultValue);
864864
}
865865

866-
$value = preg_replace("/^B?['(](.*?)[)']::[^:]+$/", '$1', $defaultValue);
866+
$value = preg_replace("/^B?['(](.*?)[)'](?:::[^:]+)?$/s", '$1', $defaultValue);
867+
$value = str_replace("''", "'", $value);
867868

868869
if ($column->getType() === self::TYPE_BINARY && str_starts_with($value, '\\x')) {
869870
return hex2bin(substr($value, 2));

tests/Provider/SchemaProvider.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public static function columns(): array
8989
'size' => 100,
9090
'precision' => null,
9191
'scale' => null,
92-
'defaultValue' => 'something',
92+
'defaultValue' => 'some\'thing',
9393
],
9494
'char_col3' => [
9595
'type' => 'text',
@@ -104,6 +104,19 @@ public static function columns(): array
104104
'scale' => null,
105105
'defaultValue' => null,
106106
],
107+
'char_col4' => [
108+
'type' => 'string',
109+
'dbType' => 'varchar',
110+
'phpType' => 'string',
111+
'primaryKey' => false,
112+
'allowNull' => true,
113+
'autoIncrement' => false,
114+
'enumValues' => null,
115+
'size' => null,
116+
'precision' => null,
117+
'scale' => null,
118+
'defaultValue' => "first line\nsecond line",
119+
],
107120
'float_col' => [
108121
'type' => 'double',
109122
'dbType' => 'float8',

tests/Support/Fixture/pgsql.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,9 @@ CREATE TABLE "type" (
142142
tinyint_col smallint DEFAULT '1',
143143
smallint_col smallint DEFAULT '1',
144144
char_col char(100) NOT NULL,
145-
char_col2 varchar(100) DEFAULT 'something',
145+
char_col2 varchar(100) DEFAULT 'some''thing',
146146
char_col3 text,
147+
char_col4 character varying DEFAULT E'first line\nsecond line',
147148
float_col double precision NOT NULL,
148149
float_col2 double precision DEFAULT '1.23',
149150
blob_col bytea DEFAULT 'a binary value',

0 commit comments

Comments
 (0)