Skip to content

Commit

Permalink
Add tests and fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov committed Sep 14, 2023
1 parent 8ddc69d commit a0dd827
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 19 deletions.
23 changes: 10 additions & 13 deletions src/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -731,12 +731,10 @@ protected function findColumns(TableSchemaInterface $table): bool
LEFT JOIN pg_namespace d ON d.oid = c.relnamespace
LEFT JOIN pg_rewrite rw ON c.relkind = 'v' AND rw.ev_class = c.oid AND rw.rulename = '_RETURN'
LEFT JOIN pg_constraint ct ON ct.conrelid = c.oid AND ct.contype = 'p' AND a.attnum = ANY (ct.conkey)
OR rw.ev_action IS NOT NULL
AND (ARRAY(SELECT regexp_matches(
rw.ev_action,
'{TARGETENTRY .*? :resorigtbl ' || ct.conrelid || ' :resorigcol (\d+) ',
'g'
)))[a.attnum][1]::smallint = ANY (ct.conkey)
OR rw.ev_action IS NOT NULL AND ct.contype = 'p'
AND (ARRAY(
SELECT regexp_matches(rw.ev_action, '{TARGETENTRY .*? :resorigtbl (\d+) :resorigcol (\d+) ', 'g')
))[a.attnum:a.attnum] <@ (ct.conrelid || ct.conkey)::text[]
WHERE
a.attnum > 0 AND t.typname != '' AND NOT a.attisdropped
AND c.relname = :tableName
Expand Down Expand Up @@ -919,15 +917,14 @@ private function loadTableConstraints(string $tableName, string $returnType): ar
ON "tcns"."oid" = "tc"."relnamespace"
INNER JOIN "pg_attribute" AS "a"
ON "a"."attrelid" = "tc"."oid"
LEFT JOIN pg_rewrite AS rw ON tc.relkind = 'v' AND rw.ev_class = tc.oid AND rw.rulename = '_RETURN'
LEFT JOIN pg_rewrite AS rw
ON "tc"."relkind" = 'v' AND "rw"."ev_class" = "tc"."oid" AND "rw"."rulename" = '_RETURN'
INNER JOIN "pg_constraint" AS "c"
ON "c"."conrelid" = "tc"."oid" AND "a"."attnum" = ANY ("c"."conkey")
OR rw.ev_action IS NOT NULL
AND (ARRAY(SELECT regexp_matches(
rw.ev_action,
'{TARGETENTRY .*? :resorigtbl ' || c.conrelid || ' :resorigcol (\d+) ',
'g'
)))[a.attnum][1]::smallint = ANY (c.conkey)
OR "rw"."ev_action" IS NOT NULL AND "c"."conrelid" != 0
AND (ARRAY(
SELECT regexp_matches("rw"."ev_action", '{TARGETENTRY .*? :resorigtbl (\d+) :resorigcol (\d+) ', 'g')
))["a"."attnum":"a"."attnum"] <@ ("c"."conrelid" || "c"."conkey")::text[]
LEFT JOIN "pg_class" AS "ftc"
ON "ftc"."oid" = "c"."confrelid"
LEFT JOIN "pg_namespace" AS "ftcns"
Expand Down
8 changes: 6 additions & 2 deletions tests/ColumnSchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,12 @@ public function testPrimaryKeyOfView()
{
$db = $this->getConnection(true);
$schema = $db->getSchema();
$tableSchema = $schema->getTableSchema('animal_view');
$tableSchema = $schema->getTableSchema('T_constraints_2_view');

$this->assertTrue($tableSchema->getColumn('id')->isPrimaryKey());
$this->assertTrue($tableSchema->getColumn('C_id_1')->isPrimaryKey());
$this->assertTrue($tableSchema->getColumn('C_id_2')->isPrimaryKey());
$this->assertFalse($tableSchema->getColumn('C_index_1')->isPrimaryKey());
$this->assertFalse($tableSchema->getColumn('C_index_2_1')->isPrimaryKey());
$this->assertFalse($tableSchema->getColumn('C_index_2_2')->isPrimaryKey());
}
}
8 changes: 4 additions & 4 deletions tests/Support/Fixture/pgsql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -417,10 +417,10 @@ CREATE TABLE "T_constraints_6"
CONSTRAINT "CN_constraints_6" FOREIGN KEY ("C_fk_id_1", "C_fk_id_2") REFERENCES "schema1"."T_constraints_5" ("C_id_1", "C_id_2") ON DELETE CASCADE ON UPDATE CASCADE
);

CREATE VIEW "T_constraints_1_view" AS SELECT * FROM "T_constraints_1";
CREATE VIEW "T_constraints_2_view" AS SELECT * FROM "T_constraints_2";
CREATE VIEW "T_constraints_3_view" AS SELECT * FROM "T_constraints_3";
CREATE VIEW "T_constraints_4_view" AS SELECT * FROM "T_constraints_4";
CREATE VIEW "T_constraints_1_view" AS SELECT 'first_value', * FROM "T_constraints_1";
CREATE VIEW "T_constraints_2_view" AS SELECT 'first_value', * FROM "T_constraints_2";
CREATE VIEW "T_constraints_3_view" AS SELECT 'first_value', * FROM "T_constraints_3";
CREATE VIEW "T_constraints_4_view" AS SELECT 'first_value', * FROM "T_constraints_4";

CREATE TABLE "T_upsert"
(
Expand Down

0 comments on commit a0dd827

Please sign in to comment.