Skip to content

Commit

Permalink
fix(se): Empty dbgenerated() breaking for Unsupported() types (#4841)
Browse files Browse the repository at this point in the history
* se: support empty `dbgenerated()` in columns of unsupported types

Empty `dbgenerated()` was fixed for supported types in
#3153 but that PR lacked
the corresponding changes for the unsupported types which this commit
adds.

---------

Co-authored-by: Alexey Orlenko <alex@aqrln.net>
  • Loading branch information
Druue and aqrln committed Apr 30, 2024
1 parent 264f24c commit 2f076be
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 13 deletions.
Expand Up @@ -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::<String>(unwrap_dbgenerated(def.value()))
.with_constraint_name(ctx.flavour.default_constraint_name(def))
});

if let Some(default) = default {
Expand Down
32 changes: 32 additions & 0 deletions schema-engine/sql-migration-tests/tests/migrations/postgres.rs
Expand Up @@ -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();
}
Expand Up @@ -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" (
Expand Down
@@ -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")
// );
4 changes: 2 additions & 2 deletions schema-engine/sql-schema-describer/src/lib.rs
Expand Up @@ -825,8 +825,8 @@ pub enum DefaultKind {
}

impl DefaultValue {
pub fn db_generated(val: impl Into<String>) -> Self {
Self::new(DefaultKind::DbGenerated(Some(val.into())))
pub fn db_generated<S: Into<String>>(val: impl Into<Option<S>>) -> Self {
Self::new(DefaultKind::DbGenerated(val.into().map(Into::into)))
}

pub fn constraint_name(&self) -> Option<&str> {
Expand Down

0 comments on commit 2f076be

Please sign in to comment.