Skip to content

Commit

Permalink
fix(schema-engine): Fixed introspecting enum array types (#5211)
Browse files Browse the repository at this point in the history
- Remove only a single underscore prefix from the ARRAY type name to get the enum type.
- Regression test cases for PostgreSQL and CocroachDB.
  • Loading branch information
viktor-ferenczi authored Mar 3, 2025
1 parent c4e2705 commit 3f077f0
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 2 deletions.
44 changes: 44 additions & 0 deletions schema-engine/sql-introspection-tests/tests/enums/cockroachdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,3 +287,47 @@ async fn an_enum_with_invalid_value_names_should_have_them_commented_out(api: &m
api.expect_datamodel(&expected).await;
Ok(())
}

// Regression: https://github.com/prisma/prisma/issues/22456
#[test_connector(tags(CockroachDb))]
async fn enum_array_type(api: &mut TestApi) -> TestResult {
let setup = indoc! {r#"
CREATE TYPE "_foo" AS ENUM ('FIRST', 'SECOND');
CREATE TABLE "Post" (
"id" TEXT NOT NULL,
"contentFilters" "_foo"[],
CONSTRAINT "Post_pkey" PRIMARY KEY ("id")
);
"#};

api.raw_cmd(setup).await;

let expectation = expect![[r#"
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "cockroachdb"
url = "env(TEST_DATABASE_URL)"
}
model Post {
id String @id
contentFilters foo[]
}
enum foo {
FIRST
SECOND
@@map("_foo")
}
"#]];

api.expect_datamodel(&expectation).await;
api.expect_no_warnings().await;

Ok(())
}
44 changes: 44 additions & 0 deletions schema-engine/sql-introspection-tests/tests/enums/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,3 +341,47 @@ async fn a_mapped_enum_will_not_warn(api: &mut TestApi) -> TestResult {

Ok(())
}

// Regression: https://github.com/prisma/prisma/issues/22456
#[test_connector(tags(Postgres), exclude(CockroachDb))]
async fn enum_array_type(api: &mut TestApi) -> TestResult {
let setup = indoc! {r#"
CREATE TYPE "_foo" AS ENUM ('FIRST', 'SECOND');
CREATE TABLE "Post" (
"id" TEXT NOT NULL,
"contentFilters" "_foo"[],
CONSTRAINT "Post_pkey" PRIMARY KEY ("id")
);
"#};

api.raw_cmd(setup).await;

let expectation = expect![[r#"
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = "env(TEST_DATABASE_URL)"
}
model Post {
id String @id
contentFilters foo[]
}
enum foo {
FIRST
SECOND
@@map("_foo")
}
"#]];

api.expect_datamodel(&expectation).await;
api.expect_no_warnings().await;

Ok(())
}
4 changes: 2 additions & 2 deletions schema-engine/sql-schema-describer/src/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1615,7 +1615,7 @@ fn get_column_type_postgresql(row: &ResultRow, schema: &SqlSchema) -> ColumnType
let enum_id: Option<_> = match data_type.as_str() {
"ARRAY" if full_data_type.starts_with('_') => {
let namespace = row.get_string("type_schema_name");
schema.find_enum(full_data_type.trim_start_matches('_'), namespace.as_deref())
schema.find_enum(&full_data_type[1..], namespace.as_deref())
}
_ => {
let namespace = row.get_string("type_schema_name");
Expand Down Expand Up @@ -1709,7 +1709,7 @@ fn get_column_type_cockroachdb(row: &ResultRow, schema: &SqlSchema) -> ColumnTyp
let enum_id: Option<_> = match data_type.as_str() {
"ARRAY" if full_data_type.starts_with('_') => {
let namespace = row.get_string("type_schema_name");
schema.find_enum(full_data_type.trim_start_matches('_'), namespace.as_deref())
schema.find_enum(&full_data_type[1..], namespace.as_deref())
}
_ => {
let namespace = row.get_string("type_schema_name");
Expand Down

0 comments on commit 3f077f0

Please sign in to comment.