[Pg-kit] Fix malformed array literal error on indexes #2884
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fix #2715, fix #2744
The issue is the expression text to array conversion happening in
(('{' || pg_get_expr(i.indexprs, i.indrelid)|| '}')::text[])[k.i]
. Commas in an expression become delimiters and split the expression up prematurely. Some special characters like double quotes can cause the malformed array literal errors.The postgres function
pg_get_indexdef
does what the snippet above is trying to do, but safely.I tried adding a test to
introspect/pg.test.ts
, but some of the indexes get added to list of "alteredIndexes" as the indexes get transformed a little bit and don't pass the json diff snapshots check (e.g.lower(first_name)
becomeslower(\"first_name\")
and gets flagged as altered).Which is why the test below has a
expect(statements.length).toBe(10);
. I decided to leave the test out of my PR for now. This test fails in the main branch but passes with this fix at the very least.