Skip to content

Commit 93e281a

Browse files
authored
Merge pull request #8524 from kenjis/fix-postgre-updateBatch-character
fix: [Postgre] updateBatch() breaks `char` type data
2 parents c7bedbb + c060e01 commit 93e281a

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

system/Database/Postgre/Builder.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,16 @@ private function getFieldType(string $table, string $fieldName): ?string
431431
$this->QBOptions['fieldTypes'][$table] = [];
432432

433433
foreach ($this->db->getFieldData($table) as $field) {
434-
$this->QBOptions['fieldTypes'][$table][$field->name] = $field->type;
434+
$type = $field->type;
435+
436+
// If `character` (or `char`) lacks a specifier, it is equivalent
437+
// to `character(1)`.
438+
// See https://www.postgresql.org/docs/current/datatype-character.html
439+
if ($field->type === 'character') {
440+
$type = $field->type . '(' . $field->max_length . ')';
441+
}
442+
443+
$this->QBOptions['fieldTypes'][$table][$field->name] = $type;
435444
}
436445
}
437446

tests/system/Database/Live/UpdateTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public function testUpdateBatch(string $constraints, array $data, array $expecte
125125
for ($i = 1; $i < 4; $i++) {
126126
$builder->insert([
127127
'type_varchar' => 'test' . $i,
128-
'type_char' => 'char',
128+
'type_char' => 'char' . $i,
129129
'type_text' => 'text',
130130
'type_smallint' => 32767,
131131
'type_integer' => 2_147_483_647,
@@ -159,6 +159,7 @@ public static function provideUpdateBatch(): iterable
159159
[
160160
[
161161
'type_varchar' => 'test1', // Key
162+
'type_char' => 'updated',
162163
'type_text' => 'updated',
163164
'type_smallint' => 9999,
164165
'type_integer' => 9_999_999,
@@ -170,6 +171,7 @@ public static function provideUpdateBatch(): iterable
170171
],
171172
[
172173
'type_varchar' => 'test2', // Key
174+
'type_char' => 'updated',
173175
'type_text' => 'updated',
174176
'type_smallint' => 9999,
175177
'type_integer' => 9_999_999,
@@ -183,6 +185,7 @@ public static function provideUpdateBatch(): iterable
183185
[
184186
[
185187
'type_varchar' => 'test1',
188+
'type_char' => 'updated',
186189
'type_text' => 'updated',
187190
'type_smallint' => 9999,
188191
'type_integer' => 9_999_999,
@@ -193,6 +196,7 @@ public static function provideUpdateBatch(): iterable
193196
],
194197
[
195198
'type_varchar' => 'test2',
199+
'type_char' => 'updated',
196200
'type_text' => 'updated',
197201
'type_smallint' => 9999,
198202
'type_integer' => 9_999_999,

0 commit comments

Comments
 (0)