Skip to content

Commit

Permalink
Make introspection FK column order deterministic
Browse files Browse the repository at this point in the history
  • Loading branch information
BenoitRanque committed Oct 3, 2024
1 parent 014c414 commit b0d17ed
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 96 deletions.
2 changes: 2 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

### Fixed

- Make introspection FK column order deterministic, preventing incorrect composite key column mapping

## [v1.1.1] - 2024-08-22

### Added
Expand Down
52 changes: 20 additions & 32 deletions crates/configuration/src/version3/version3.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1142,41 +1142,29 @@ WITH
-- array.
constraint_columns AS
(
SELECT
c_unnest.constraint_id,
array_agg(col.column_name) as key_columns
FROM
(
SELECT
c.oid as constraint_id,
c.conrelid as relation_id,
unnest(c.conkey) as column_number
FROM
pg_catalog.pg_constraint as c
) AS c_unnest
INNER JOIN
columns col
USING (relation_id, column_number)
GROUP BY c_unnest.constraint_id
SELECT c.oid as constraint_id,
array_agg(
col.column_name
ORDER BY k.index
) as key_columns
FROM pg_catalog.pg_constraint as c
CROSS JOIN UNNEST(c.conkey) WITH ORDINALITY k(column_number, index)
INNER JOIN columns col ON c.conrelid = col.relation_id
AND k.column_number = col.column_number
GROUP BY c.oid
),
constraint_referenced_columns AS
(
SELECT
c_unnest.constraint_id,
array_agg(col.column_name) as referenced_columns
FROM
(
SELECT
c.oid as constraint_id,
c.confrelid as relation_id,
unnest(c.confkey) as column_number
FROM
pg_catalog.pg_constraint as c
) AS c_unnest
INNER JOIN
columns col
USING (relation_id, column_number)
GROUP BY c_unnest.constraint_id
SELECT c.oid as constraint_id,
array_agg(
col.column_name
ORDER BY k.index
) as referenced_columns
FROM pg_catalog.pg_constraint as c
CROSS JOIN UNNEST(c.confkey) WITH ORDINALITY k(column_number, index)
INNER JOIN columns col ON c.confrelid = col.relation_id
AND k.column_number = col.column_number
GROUP BY c.oid
)
SELECT
c.oid as constraint_id,
Expand Down
52 changes: 20 additions & 32 deletions crates/configuration/src/version4/introspection.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1346,41 +1346,29 @@ WITH
-- array.
constraint_columns AS
(
SELECT
c_unnest.constraint_id,
array_agg(col.column_name) as key_columns
FROM
(
SELECT
c.oid as constraint_id,
c.conrelid as relation_id,
unnest(c.conkey) as column_number
FROM
pg_catalog.pg_constraint as c
) AS c_unnest
INNER JOIN
columns col
USING (relation_id, column_number)
GROUP BY c_unnest.constraint_id
SELECT c.oid as constraint_id,
array_agg(
col.column_name
ORDER BY k.index
) as key_columns
FROM pg_catalog.pg_constraint as c
CROSS JOIN UNNEST(c.conkey) WITH ORDINALITY k(column_number, index)
INNER JOIN columns col ON c.conrelid = col.relation_id
AND k.column_number = col.column_number
GROUP BY c.oid
),
constraint_referenced_columns AS
(
SELECT
c_unnest.constraint_id,
array_agg(col.column_name) as referenced_columns
FROM
(
SELECT
c.oid as constraint_id,
c.confrelid as relation_id,
unnest(c.confkey) as column_number
FROM
pg_catalog.pg_constraint as c
) AS c_unnest
INNER JOIN
columns col
USING (relation_id, column_number)
GROUP BY c_unnest.constraint_id
SELECT c.oid as constraint_id,
array_agg(
col.column_name
ORDER BY k.index
) as referenced_columns
FROM pg_catalog.pg_constraint as c
CROSS JOIN UNNEST(c.confkey) WITH ORDINALITY k(column_number, index)
INNER JOIN columns col ON c.confrelid = col.relation_id
AND k.column_number = col.column_number
GROUP BY c.oid
)
SELECT
c.oid as constraint_id,
Expand Down
52 changes: 20 additions & 32 deletions crates/configuration/src/version5/introspection.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1346,41 +1346,29 @@ WITH
-- array.
constraint_columns AS
(
SELECT
c_unnest.constraint_id,
array_agg(col.column_name) as key_columns
FROM
(
SELECT
c.oid as constraint_id,
c.conrelid as relation_id,
unnest(c.conkey) as column_number
FROM
pg_catalog.pg_constraint as c
) AS c_unnest
INNER JOIN
columns col
USING (relation_id, column_number)
GROUP BY c_unnest.constraint_id
SELECT c.oid as constraint_id,
array_agg(
col.column_name
ORDER BY k.index
) as key_columns
FROM pg_catalog.pg_constraint as c
CROSS JOIN UNNEST(c.conkey) WITH ORDINALITY k(column_number, index)
INNER JOIN columns col ON c.conrelid = col.relation_id
AND k.column_number = col.column_number
GROUP BY c.oid
),
constraint_referenced_columns AS
(
SELECT
c_unnest.constraint_id,
array_agg(col.column_name) as referenced_columns
FROM
(
SELECT
c.oid as constraint_id,
c.confrelid as relation_id,
unnest(c.confkey) as column_number
FROM
pg_catalog.pg_constraint as c
) AS c_unnest
INNER JOIN
columns col
USING (relation_id, column_number)
GROUP BY c_unnest.constraint_id
SELECT c.oid as constraint_id,
array_agg(
col.column_name
ORDER BY k.index
) as referenced_columns
FROM pg_catalog.pg_constraint as c
CROSS JOIN UNNEST(c.confkey) WITH ORDINALITY k(column_number, index)
INNER JOIN columns col ON c.confrelid = col.relation_id
AND k.column_number = col.column_number
GROUP BY c.oid
)
SELECT
c.oid as constraint_id,
Expand Down

0 comments on commit b0d17ed

Please sign in to comment.