From 2f076bece482bca9f72ae0ce8f2e1f14787a48d1 Mon Sep 17 00:00:00 2001 From: Sophie <29753584+Druue@users.noreply.github.com> Date: Tue, 30 Apr 2024 13:26:35 +0200 Subject: [PATCH] fix(se): Empty dbgenerated() breaking for Unsupported() types (#4841) * se: support empty `dbgenerated()` in columns of unsupported types Empty `dbgenerated()` was fixed for supported types in https://github.com/prisma/prisma-engines/pull/3153 but that PR lacked the corresponding changes for the unsupported types which this commit adds. --------- Co-authored-by: Alexey Orlenko --- .../src/sql_schema_calculator.rs | 12 ++----- .../tests/migrations/postgres.rs | 32 +++++++++++++++++++ .../postgres/empty_dbgenerated.prisma | 5 +-- .../empty_unsupported_dbgenerated.prisma | 21 ++++++++++++ schema-engine/sql-schema-describer/src/lib.rs | 4 +-- 5 files changed, 61 insertions(+), 13 deletions(-) create mode 100644 schema-engine/sql-migration-tests/tests/single_migration_tests/postgres/empty_unsupported_dbgenerated.prisma diff --git a/schema-engine/connectors/sql-schema-connector/src/sql_schema_calculator.rs b/schema-engine/connectors/sql-schema-connector/src/sql_schema_calculator.rs index 5ef3bb69529a..fcbe39bccf02 100644 --- a/schema-engine/connectors/sql-schema-connector/src/sql_schema_calculator.rs +++ b/schema-engine/connectors/sql-schema-connector/src/sql_schema_calculator.rs @@ -414,16 +414,10 @@ fn push_column_for_model_unsupported_scalar_field( table_id: sql::TableId, ctx: &mut Context<'_>, ) { - let default = field.default_value().and_then(|def| { + let default = field.default_value().map(|def| { // This is validated as @default(dbgenerated("...")), we can unwrap. - let dbgenerated_contents = unwrap_dbgenerated(def.value()); - if let Some(value) = dbgenerated_contents { - let default = - sql::DefaultValue::db_generated(value).with_constraint_name(ctx.flavour.default_constraint_name(def)); - Some(default) - } else { - None - } + sql::DefaultValue::db_generated::(unwrap_dbgenerated(def.value())) + .with_constraint_name(ctx.flavour.default_constraint_name(def)) }); if let Some(default) = default { diff --git a/schema-engine/sql-migration-tests/tests/migrations/postgres.rs b/schema-engine/sql-migration-tests/tests/migrations/postgres.rs index 33825671718d..a31454b8058b 100644 --- a/schema-engine/sql-migration-tests/tests/migrations/postgres.rs +++ b/schema-engine/sql-migration-tests/tests/migrations/postgres.rs @@ -735,3 +735,35 @@ fn dbgenerated_on_generated_columns_is_idempotent(api: TestApi) { api.schema_push(schema).send().assert_green().assert_no_steps(); } + +// https://github.com/prisma/prisma/issues/15654 +#[test_connector(tags(Postgres12), exclude(CockroachDb))] +fn dbgenerated_on_generated_unsupported_columns_is_idempotent(api: TestApi) { + let sql = r#" + CREATE TABLE "table" ( + "id" TEXT NOT NULL, + -- NOTE: Modified to make it a PG generated column + "hereBeDragons" tsvector GENERATED ALWAYS AS ( + to_tsvector('english', id::text) + ) STORED, + + CONSTRAINT "table_pkey" PRIMARY KEY ("id") + ); + "#; + + api.raw_cmd(sql); + + let schema = r#" + datasource db { + provider = "postgresql" + url = env("TEST_DATABASE_URL") + } + + model table { + id String @id + hereBeDragons Unsupported("tsvector")? @default(dbgenerated()) + } + "#; + + api.schema_push(schema).send().assert_green().assert_no_steps(); +} diff --git a/schema-engine/sql-migration-tests/tests/single_migration_tests/postgres/empty_dbgenerated.prisma b/schema-engine/sql-migration-tests/tests/single_migration_tests/postgres/empty_dbgenerated.prisma index 31f119c5dade..951873cce635 100644 --- a/schema-engine/sql-migration-tests/tests/single_migration_tests/postgres/empty_dbgenerated.prisma +++ b/schema-engine/sql-migration-tests/tests/single_migration_tests/postgres/empty_dbgenerated.prisma @@ -3,13 +3,14 @@ datasource testds { provider = "postgresql" - url = env("TEST_DATABASE_URL") + url = env("TEST_DATABASE_URL") } model table { - id String @id + id String @id hereBeDragons String @default(dbgenerated()) } + // Expected Migration: // -- CreateTable // CREATE TABLE "table" ( diff --git a/schema-engine/sql-migration-tests/tests/single_migration_tests/postgres/empty_unsupported_dbgenerated.prisma b/schema-engine/sql-migration-tests/tests/single_migration_tests/postgres/empty_unsupported_dbgenerated.prisma new file mode 100644 index 000000000000..5eab343c99e6 --- /dev/null +++ b/schema-engine/sql-migration-tests/tests/single_migration_tests/postgres/empty_unsupported_dbgenerated.prisma @@ -0,0 +1,21 @@ +// tags=postgres +// exclude=cockroachdb + +datasource testds { + provider = "postgresql" + url = env("TEST_DATABASE_URL") +} + +model table { + id String @id + hereBeDragons Unsupported("tsvector")? @default(dbgenerated()) +} + +// Expected Migration: +// -- CreateTable +// CREATE TABLE "table" ( +// "id" TEXT NOT NULL, +// "hereBeDragons" tsvector, +// +// CONSTRAINT "table_pkey" PRIMARY KEY ("id") +// ); diff --git a/schema-engine/sql-schema-describer/src/lib.rs b/schema-engine/sql-schema-describer/src/lib.rs index c5ae677f0e3b..8f65c175b2a3 100644 --- a/schema-engine/sql-schema-describer/src/lib.rs +++ b/schema-engine/sql-schema-describer/src/lib.rs @@ -825,8 +825,8 @@ pub enum DefaultKind { } impl DefaultValue { - pub fn db_generated(val: impl Into) -> Self { - Self::new(DefaultKind::DbGenerated(Some(val.into()))) + pub fn db_generated>(val: impl Into>) -> Self { + Self::new(DefaultKind::DbGenerated(val.into().map(Into::into))) } pub fn constraint_name(&self) -> Option<&str> {