Skip to content

Commit 8edcb05

Browse files
committed
Update according changes in ColumnSchemaInterface
1 parent 72da3f3 commit 8edcb05

File tree

8 files changed

+173
-153
lines changed

8 files changed

+173
-153
lines changed

src/Column/ArrayColumnSchema.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ public function getColumn(): ColumnSchemaInterface
5858
if ($this->column === null) {
5959
$this->column = (new ColumnFactory())->fromDbType($this->getDbType() ?? '');
6060
$this->column->enumValues($this->getEnumValues());
61-
$this->column->precision($this->getPrecision());
6261
$this->column->scale($this->getScale());
6362
$this->column->size($this->getSize());
6463
}

src/Column/ColumnFactory.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@
55
namespace Yiisoft\Db\Pgsql\Column;
66

77
use Yiisoft\Db\Constant\ColumnType;
8+
use Yiisoft\Db\Constraint\ForeignKeyConstraint;
89
use Yiisoft\Db\Schema\Column\AbstractColumnFactory;
910
use Yiisoft\Db\Schema\Column\ColumnSchemaInterface;
1011

1112
use const PHP_INT_SIZE;
1213

1314
/**
1415
* @psalm-type ColumnInfo = array{
15-
* allow_null?: bool|string|null,
1616
* auto_increment?: bool|string,
17+
* check?: string|null,
1718
* columns?: array<string, ColumnSchemaInterface>,
1819
* comment?: string|null,
1920
* computed?: bool|string,
@@ -24,13 +25,15 @@
2425
* extra?: string|null,
2526
* primary_key?: bool|string,
2627
* name?: string,
27-
* precision?: int|string|null,
28+
* not_null?: bool|string|null,
29+
* reference?: ForeignKeyConstraint|null,
2830
* sequence_name?: string|null,
2931
* scale?: int|string|null,
3032
* schema?: string|null,
3133
* size?: int|string|null,
3234
* table?: string|null,
3335
* type?: string,
36+
* unique?: bool|string,
3437
* }
3538
*/
3639
final class ColumnFactory extends AbstractColumnFactory

src/Schema.php

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,14 @@
5050
* type_scheme: string|null,
5151
* character_maximum_length: int,
5252
* column_comment: string|null,
53-
* modifier: int,
5453
* is_nullable: bool,
5554
* column_default: string|null,
5655
* is_autoinc: bool,
5756
* sequence_name: string|null,
5857
* enum_values: string|null,
59-
* numeric_precision: int|null,
60-
* numeric_scale: int|null,
6158
* size: string|null,
62-
* is_pkey: bool|null,
59+
* scale: int|null,
60+
* contype: string|null,
6361
* dimension: int
6462
* }
6563
* @psalm-type ConstraintArray = array<
@@ -623,7 +621,6 @@ protected function findColumns(TableSchemaInterface $table): bool
623621
(SELECT nspname FROM pg_namespace WHERE oid = COALESCE(td.typnamespace, tb.typnamespace, t.typnamespace)) AS type_scheme,
624622
a.attlen AS character_maximum_length,
625623
pg_catalog.col_description(c.oid, a.attnum) AS column_comment,
626-
information_schema._pg_truetypmod(a, t) AS modifier,
627624
NOT (a.attnotnull OR t.typnotnull) AS is_nullable,
628625
COALESCE(t.typdefault, pg_get_expr(ad.adbin, ad.adrelid)) AS column_default,
629626
COALESCE(pg_get_expr(ad.adbin, ad.adrelid) ~ 'nextval', false) $orIdentity AS is_autoinc,
@@ -639,19 +636,25 @@ protected function findColumns(TableSchemaInterface $table): bool
639636
',')
640637
ELSE NULL
641638
END AS enum_values,
642-
information_schema._pg_numeric_precision(
643-
COALESCE(td.oid, tb.oid, a.atttypid),
644-
information_schema._pg_truetypmod(a, t)
645-
) AS numeric_precision,
639+
COALESCE(
640+
information_schema._pg_char_max_length(
641+
COALESCE(td.oid, tb.oid, a.atttypid),
642+
a.atttypmod
643+
),
644+
information_schema._pg_datetime_precision(
645+
COALESCE(td.oid, tb.oid, a.atttypid),
646+
a.atttypmod
647+
),
648+
CASE a.atttypmod
649+
WHEN -1 THEN null
650+
ELSE ((a.atttypmod - 4) >> 16) & 65535
651+
END
652+
) AS size,
646653
information_schema._pg_numeric_scale(
647654
COALESCE(td.oid, tb.oid, a.atttypid),
648-
information_schema._pg_truetypmod(a, t)
649-
) AS numeric_scale,
650-
information_schema._pg_char_max_length(
651-
COALESCE(td.oid, tb.oid, a.atttypid),
652-
information_schema._pg_truetypmod(a, t)
653-
) AS size,
654-
ct.oid IS NOT NULL AS is_pkey,
655+
a.atttypmod
656+
) AS scale,
657+
ct.contype,
655658
COALESCE(NULLIF(a.attndims, 0), NULLIF(t.typndims, 0), (t.typcategory='A')::int) AS dimension
656659
FROM
657660
pg_class c
@@ -663,11 +666,11 @@ protected function findColumns(TableSchemaInterface $table): bool
663666
LEFT JOIN pg_type td ON t.typndims > 0 AND t.typbasetype > 0 AND tb.typelem = td.oid
664667
LEFT JOIN pg_namespace d ON d.oid = c.relnamespace
665668
LEFT JOIN pg_rewrite rw ON c.relkind = 'v' AND rw.ev_class = c.oid AND rw.rulename = '_RETURN'
666-
LEFT JOIN pg_constraint ct ON ct.conrelid = c.oid AND ct.contype = 'p' AND a.attnum = ANY (ct.conkey)
667-
OR rw.ev_action IS NOT NULL AND ct.contype = 'p'
668-
AND (ARRAY(
669+
LEFT JOIN pg_constraint ct ON (ct.contype = 'p' OR ct.contype = 'u' AND cardinality(ct.conkey) = 1)
670+
AND (ct.conrelid = c.oid AND a.attnum = ANY (ct.conkey)
671+
OR rw.ev_action IS NOT NULL AND (ARRAY(
669672
SELECT regexp_matches(rw.ev_action, '{TARGETENTRY .*? :resorigtbl (\d+) :resorigcol (\d+) ', 'g')
670-
))[a.attnum:a.attnum] <@ (ct.conrelid::text || ct.conkey::text[])
673+
))[a.attnum:a.attnum] <@ (ct.conrelid::text || ct.conkey::text[]))
671674
WHERE
672675
a.attnum > 0 AND t.typname != '' AND NOT a.attisdropped
673676
AND c.relname = :tableName
@@ -738,18 +741,19 @@ private function loadColumnSchema(array $info): ColumnSchemaInterface
738741
->fromDbType($dbType, ['dimension' => $info['dimension']]);
739742
}
740743

744+
/** @psalm-suppress DeprecatedMethod */
741745
$column->name($info['column_name']);
742746
$column->dbType($dbType);
743-
$column->allowNull($info['is_nullable']);
747+
$column->notNull(!$info['is_nullable']);
744748
$column->autoIncrement($info['is_autoinc']);
745749
$column->comment($info['column_comment']);
746750
$column->enumValues($info['enum_values'] !== null
747751
? explode(',', str_replace(["''"], ["'"], $info['enum_values']))
748752
: null);
749-
$column->primaryKey((bool) $info['is_pkey']);
750-
$column->precision($info['numeric_precision']);
751-
$column->scale($info['numeric_scale']);
752-
$column->size($info['size'] === null ? null : (int) $info['size']);
753+
$column->primaryKey($info['contype'] === 'p');
754+
$column->unique($info['contype'] === 'u');
755+
$column->scale($info['scale']);
756+
$column->size($info['size']);
753757

754758
/**
755759
* pg_get_serial_sequence() doesn't track DEFAULT value change.
@@ -771,9 +775,8 @@ private function loadColumnSchema(array $info): ColumnSchemaInterface
771775
$arrayColumn = $column->getColumn();
772776
$arrayColumn->dbType($dbType);
773777
$arrayColumn->enumValues($column->getEnumValues());
774-
$arrayColumn->precision($info['numeric_precision']);
775-
$arrayColumn->scale($info['numeric_scale']);
776-
$arrayColumn->size($info['size'] === null ? null : (int) $info['size']);
778+
$arrayColumn->scale($info['scale']);
779+
$arrayColumn->size($info['size']);
777780
}
778781

779782
$column->defaultValue($this->normalizeDefaultValue($defaultValue, $column));

tests/Provider/QueryBuilderProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public static function buildCondition(): array
2727
$buildCondition = parent::buildCondition();
2828

2929
$priceColumns = [
30-
'value' => ColumnSchemaBuilder::numeric(name: 'value', precision: 10, scale: 2),
30+
'value' => ColumnSchemaBuilder::numeric(name: 'value', size: 10, scale: 2),
3131
'currency_code' => ColumnSchemaBuilder::char(name: 'currency_code', size: 3),
3232
];
3333

0 commit comments

Comments
 (0)