From 43b1d1c46752ce3fdc07d5d18580b3098bb1265e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=20Houl=C3=A9?= <13155277+tomhoule@users.noreply.github.com> Date: Wed, 9 Nov 2022 12:15:23 +0100 Subject: [PATCH] me: fix regression w/ mapped enum names on postgres (#3368) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * me: fix regression w/ mapped enum names on postgres When calculating a SQL schema from a Prisma schema, the Postgres connector of the migration engine would use the name of the enum instead of its database name (see parser-database docs for the reference on what these mean). To picture this with an example, the following schema. ``` enum PersonType { Dog Cat @@map("pet_preference") } ``` would correspond, before the regression, and after this commit, to: ``` CREATE TYPE "pet_preference" AS ENUM ('Dog', 'Cat'); ``` but the regression caused it to correspond to: ``` CREATE TYPE "PersonType" AS ENUM ('Dog', 'Cat'); ``` This does not only affect newly created enums: we diff the wrong enums, causing bad migrations for existing users of Migrate that have the enums with the correct (mapped) names in their database. Users reported the regression in https://github.com/prisma/prisma/issues/16180 This commit should completely fix the problem. The regression was introduced in an unrelated refactoring in b6400f10407836a032b36e9e084abc7b5f602933. Enum names in column types were replace with enum ids — a win for correctness. As part of this refactoring, the `sql_schema_describer::Enum` type was made private, and adding enums to a schema became possible only through `SqlSchema::push_enum`. In the SQL migration connector What could have caught this: - Testing that the name of the database enum in the migration is the mapped name. We can test this: - Through `.assert_schema()`, which has existed for a long time. It is a gap in our test coverage that we weren't testing this. - The new single migration tests, like the test in this commit. They are new, and they are less work than the previous option. - migrations-ci could have caught this, but it is currently neglected. * me: move enum test to enums directory --- .../sql_schema_calculator_flavour/postgres.rs | 2 +- .../postgres/{ => enums}/enum_basic.prisma | 0 .../postgres/enums/mapped_basic.prisma | 30 +++++++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) rename migration-engine/migration-engine-tests/tests/single_migration_tests/postgres/{ => enums}/enum_basic.prisma (100%) create mode 100644 migration-engine/migration-engine-tests/tests/single_migration_tests/postgres/enums/mapped_basic.prisma diff --git a/migration-engine/connectors/sql-migration-connector/src/sql_schema_calculator/sql_schema_calculator_flavour/postgres.rs b/migration-engine/connectors/sql-migration-connector/src/sql_schema_calculator/sql_schema_calculator_flavour/postgres.rs index a5c396d89455..4c387a29adc9 100644 --- a/migration-engine/connectors/sql-migration-connector/src/sql_schema_calculator/sql_schema_calculator_flavour/postgres.rs +++ b/migration-engine/connectors/sql-migration-connector/src/sql_schema_calculator/sql_schema_calculator_flavour/postgres.rs @@ -19,7 +19,7 @@ impl SqlSchemaCalculatorFlavour for PostgresFlavour { let sql_enum_id = ctx .schema .describer_schema - .push_enum(sql_namespace_id, prisma_enum.name().to_owned()); + .push_enum(sql_namespace_id, prisma_enum.database_name().to_owned()); ctx.enum_ids.insert(prisma_enum.id, sql_enum_id); for value in prisma_enum.values() { diff --git a/migration-engine/migration-engine-tests/tests/single_migration_tests/postgres/enum_basic.prisma b/migration-engine/migration-engine-tests/tests/single_migration_tests/postgres/enums/enum_basic.prisma similarity index 100% rename from migration-engine/migration-engine-tests/tests/single_migration_tests/postgres/enum_basic.prisma rename to migration-engine/migration-engine-tests/tests/single_migration_tests/postgres/enums/enum_basic.prisma diff --git a/migration-engine/migration-engine-tests/tests/single_migration_tests/postgres/enums/mapped_basic.prisma b/migration-engine/migration-engine-tests/tests/single_migration_tests/postgres/enums/mapped_basic.prisma new file mode 100644 index 000000000000..190b25ef2e6f --- /dev/null +++ b/migration-engine/migration-engine-tests/tests/single_migration_tests/postgres/enums/mapped_basic.prisma @@ -0,0 +1,30 @@ +// tags=postgres +// exclude=cockroachdb + +datasource pg { + provider = "postgresql" + url = env("TEST_DATABASE_URL") +} + +model Avocado { + id String @id + status Ripeness +} + +enum Ripeness { + NOT_RIPE_ENOUGH + TOO_RIPE + + @@map("avocado_ripeness_status") +} +// Expected Migration: +// -- CreateEnum +// CREATE TYPE "avocado_ripeness_status" AS ENUM ('NOT_RIPE_ENOUGH', 'TOO_RIPE'); +// +// -- CreateTable +// CREATE TABLE "Avocado" ( +// "id" TEXT NOT NULL, +// "status" "avocado_ripeness_status" NOT NULL, +// +// CONSTRAINT "Avocado_pkey" PRIMARY KEY ("id") +// );