Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve loading schemas of views #378

Merged
merged 4 commits into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
- New #373: Override `QueryBuilder::prepareBinary()` method (@Tigrov)
- Chg #375: Update `QueryBuilder` constructor (@Tigrov)
- Enh #374: Use `ColumnDefinitionBuilder` to generate table column SQL representation (@Tigrov)
- Enh #378: Improve loading schemas of views (@Tigrov)

## 1.3.0 March 21, 2024

Expand Down
32 changes: 17 additions & 15 deletions src/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,15 +301,12 @@ protected function loadTableIndexes(string $tableName): array
FROM "pg_class" AS "tc"
INNER JOIN "pg_namespace" AS "tcns"
ON "tcns"."oid" = "tc"."relnamespace"
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_index" AS "i"
ON "i"."indrelid" = "tc"."oid"
OR rw.ev_action IS NOT NULL
AND (SELECT regexp_matches(
rw.ev_action,
'{TARGETENTRY .*? :resorigtbl ' || "i"."indrelid" || ' :resorigcol ' || "i"."indkey"[0] || ' '
)) IS NOT NULL
OR "rw"."ev_action" IS NOT NULL
AND strpos("rw"."ev_action", ':resorigtbl ' || "i"."indrelid" || ' :resorigcol ' || "i"."indkey"[0] || ' ') > 0
INNER JOIN "pg_class" AS "ic"
ON "ic"."oid" = "i"."indexrelid"
INNER JOIN "pg_attribute" AS "ia"
Expand Down Expand Up @@ -665,15 +662,19 @@ protected function findColumns(TableSchemaInterface $table): bool
LEFT JOIN pg_attrdef ad ON a.attrelid = ad.adrelid AND a.attnum = ad.adnum
LEFT JOIN pg_type t ON a.atttypid = t.oid
LEFT JOIN pg_type tb ON (a.attndims > 0 OR t.typcategory='A') AND t.typelem > 0 AND t.typelem = tb.oid
OR t.typbasetype > 0 AND t.typbasetype = tb.oid
OR t.typbasetype > 0 AND t.typbasetype = tb.oid
LEFT JOIN pg_type td ON t.typndims > 0 AND t.typbasetype > 0 AND tb.typelem = td.oid
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.contype = 'p' OR ct.contype = 'u' AND cardinality(ct.conkey) = 1)
AND (ct.conrelid = c.oid AND a.attnum = ANY (ct.conkey)
OR rw.ev_action IS NOT NULL AND (ARRAY(
SELECT regexp_matches(rw.ev_action, '{TARGETENTRY .*? :resorigtbl (\d+) :resorigcol (\d+) ', 'g')
))[a.attnum:a.attnum] <@ (ct.conrelid::text || ct.conkey::text[]))
AND (
ct.conrelid = c.oid AND a.attnum = ANY (ct.conkey)
OR rw.ev_action IS NOT NULL AND ct.conrelid != 0
AND strpos(rw.ev_action, ':resorigtbl ' || ct.conrelid || ' ') > 0
AND rw.ev_action ~ ('.* :resno ' || a.attnum || ' :resname \S+ :ressortgroupref \d+ :resorigtbl '
|| ct.conrelid || ' :resorigcol (?:'
|| replace(substr(ct.conkey::text, 2, length(ct.conkey::text) - 2), ',', '|') || ') .*')
)
WHERE
a.attnum > 0 AND t.typname != '' AND NOT a.attisdropped
AND c.relname = :tableName
Expand Down Expand Up @@ -831,9 +832,10 @@ private function loadTableConstraints(string $tableName, string $returnType): ar
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 "c"."conrelid" != 0
AND (ARRAY(
SELECT regexp_matches("rw"."ev_action", '{TARGETENTRY .*? :resorigtbl (\d+) :resorigcol (\d+) ', 'g')
))["a"."attnum":"a"."attnum"] <@ ("c"."conrelid"::text || "c"."conkey"::text[])
AND strpos("rw"."ev_action", ':resorigtbl ' || "c"."conrelid" || ' ') > 0
AND "rw"."ev_action" ~ ('.* :resno ' || "a"."attnum" || ' :resname \S+ :ressortgroupref \d+ :resorigtbl '
|| "c"."conrelid" || ' :resorigcol (?:'
|| replace(substr("c"."conkey"::text, 2, length("c"."conkey"::text) - 2), ',', '|') || ') .*')
LEFT JOIN "pg_class" AS "ftc"
ON "ftc"."oid" = "c"."confrelid"
LEFT JOIN "pg_namespace" AS "ftcns"
Expand Down