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

fix(schema-engine): Fixed introspecting enum array types #5211

Merged
merged 1 commit into from
Mar 3, 2025

Conversation

viktor-ferenczi
Copy link
Contributor

@viktor-ferenczi viktor-ferenczi commented Feb 28, 2025

Issue
prisma/prisma#22456

Affected databases

  • PostgreSQL
  • CockroachDB

Root cause

PostgreSQL prepends an underscore to produce ARRAY types from each type defined. (The enum is defined as a type.) The database introspection code trimmed all underscores from the type name of the column, which resulted in a loss of the underscore in the name of the original enum type. As a consequence the enum type was not found and ended up as unsupported. The unsupported type did not match the original type, resulted in a unwanted migration.

Solution

Remove only a single underscore prefix from the type name.

Remarks

Quoting from the PostgreSQL CREATE TYPE documentation:

Whenever a user-defined type is created, PostgreSQL automatically creates an associated array type, whose name consists of the element type's name prepended with an underscore, and truncated if necessary to keep it less than NAMEDATALEN bytes long.

Unrelated to the underscore prefix problem, but will affect the generality of any solution: By default NAMEDATALEN is 63. It can be queried by SHOW max_identifier_length;

If the original type name was NAMEDATALEN long, then the last character would be truncated by PostgreSQL. It would still prevent finding the correct type on introspection. A workaround could be made to search for a matching type. However, it is a very rare corner case, it is is not worth the additional code. Such cases could be fixed by increasing NAMEDATALEN on the database side.

@viktor-ferenczi viktor-ferenczi requested a review from a team as a code owner February 28, 2025 16:32
@viktor-ferenczi viktor-ferenczi requested review from aqrln and removed request for a team February 28, 2025 16:32
@CLAassistant
Copy link

CLAassistant commented Feb 28, 2025

CLA assistant check
All committers have signed the CLA.

@viktor-ferenczi viktor-ferenczi force-pushed the fix/22456-introspect-enum-array branch from 8c44205 to d406a1a Compare February 28, 2025 16:36
Copy link

codspeed-hq bot commented Feb 28, 2025

CodSpeed Performance Report

Merging #5211 will not alter performance

Comparing fix/22456-introspect-enum-array (d406a1a) with main (050af99)

Summary

✅ 11 untouched benchmarks

Copy link
Contributor

WASM Query Engine file Size

Engine This PR Base branch Diff
Postgres 2.135MiB 2.135MiB 0.000B
Postgres (gzip) 857.967KiB 857.968KiB -1.000B
Mysql 2.098MiB 2.098MiB 0.000B
Mysql (gzip) 842.628KiB 842.628KiB 0.000B
Sqlite 2.007MiB 2.007MiB 0.000B
Sqlite (gzip) 806.374KiB 806.374KiB 0.000B

Copy link
Member

@aqrln aqrln left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, nice investigation and fix!

@viktor-ferenczi viktor-ferenczi added the PR: Bug A PR That Fixes a bug label Mar 2, 2025
@viktor-ferenczi viktor-ferenczi merged commit 3f077f0 into main Mar 3, 2025
370 checks passed
@viktor-ferenczi viktor-ferenczi deleted the fix/22456-introspect-enum-array branch March 3, 2025 08:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
PR: Bug A PR That Fixes a bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants