Skip to content

Commit 2f8f130

Browse files
authored
Fix #284: Add tests for binary type and fix casting of default value
1 parent ada7f05 commit 2f8f130

5 files changed

Lines changed: 15 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## 1.0.1 under development
44

55
- Enh #282: Support `numeric` arrays, improve support of domain types and `int` and `varchar` array types (@Tigrov)
6+
- Enh #284: Add tests for `binary` type and fix casting of default value (@Tigrov)
67

78
## 1.0.0 April 12, 2023
89

src/Schema.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,12 @@
2626
use function array_values;
2727
use function bindec;
2828
use function explode;
29+
use function hex2bin;
30+
use function is_string;
2931
use function preg_match;
3032
use function preg_replace;
3133
use function str_replace;
34+
use function str_starts_with;
3235
use function substr;
3336

3437
/**
@@ -792,7 +795,11 @@ protected function findColumns(TableSchemaInterface $table): bool
792795
} elseif (is_string($defaultValue) && preg_match("/^'(\d+)'::\"bit\"$/", $defaultValue, $matches)) {
793796
$loadColumnSchema->defaultValue(bindec($matches[1]));
794797
} elseif (is_string($defaultValue) && preg_match("/^'(.*?)'::/", $defaultValue, $matches)) {
795-
$loadColumnSchema->defaultValue($loadColumnSchema->phpTypecast($matches[1]));
798+
if ($loadColumnSchema->getType() === 'binary' && str_starts_with($matches[1], '\\x')) {
799+
$loadColumnSchema->defaultValue(hex2bin(substr($matches[1], 2)));
800+
} else {
801+
$loadColumnSchema->defaultValue($loadColumnSchema->phpTypecast($matches[1]));
802+
}
796803
} elseif (
797804
is_string($defaultValue) &&
798805
preg_match('/^(\()?(.*?)(?(1)\))(?:::.+)?$/', $defaultValue, $matches)

tests/ColumnSchemaTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
use Yiisoft\Db\Query\Query;
1717
use Yiisoft\Db\Schema\SchemaInterface;
1818

19+
use function stream_get_contents;
20+
1921
/**
2022
* @group pgsql
2123
*
@@ -64,6 +66,7 @@ public function testPhpTypeCast(): void
6466
$intColPhpTypeCast = $tableSchema->getColumn('int_col')?->phpTypecast($query['int_col']);
6567
$charColPhpTypeCast = $tableSchema->getColumn('char_col')?->phpTypecast($query['char_col']);
6668
$floatColPhpTypeCast = $tableSchema->getColumn('float_col')?->phpTypecast($query['float_col']);
69+
$blobColPhpTypeCast = $tableSchema->getColumn('blob_col')?->phpTypecast($query['blob_col']);
6770
$boolColPhpTypeCast = $tableSchema->getColumn('bool_col')?->phpTypecast($query['bool_col']);
6871
$numericColPhpTypeCast = $tableSchema->getColumn('numeric_col')?->phpTypecast($query['numeric_col']);
6972
$intArrayColPhpType = $tableSchema->getColumn('intarray_col')?->phpTypecast($query['intarray_col']);
@@ -77,6 +80,7 @@ public function testPhpTypeCast(): void
7780
$this->assertSame(1, $intColPhpTypeCast);
7881
$this->assertSame(str_repeat('x', 100), $charColPhpTypeCast);
7982
$this->assertSame(1.234, $floatColPhpTypeCast);
83+
$this->assertSame("\x10\x11\x12", stream_get_contents($blobColPhpTypeCast));
8084
$this->assertFalse($boolColPhpTypeCast);
8185
$this->assertSame(33.22, $numericColPhpTypeCast);
8286
$this->assertSame([1, -2, null, 42], $intArrayColPhpType);

tests/Provider/SchemaProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public static function columns(): array
141141
'size' => null,
142142
'precision' => null,
143143
'scale' => null,
144-
'defaultValue' => null,
144+
'defaultValue' => 'a binary value',
145145
],
146146
'numeric_col' => [
147147
'type' => 'decimal',

tests/Support/Fixture/pgsql.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ CREATE TABLE "type" (
146146
char_col3 text,
147147
float_col double precision NOT NULL,
148148
float_col2 double precision DEFAULT '1.23',
149-
blob_col bytea,
149+
blob_col bytea DEFAULT 'a binary value',
150150
numeric_col decimal(5,2) DEFAULT '33.22',
151151
time timestamp NOT NULL DEFAULT '2002-01-01 00:00:00',
152152
bool_col boolean NOT NULL,

0 commit comments

Comments
 (0)