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(se): Empty dbgenerated() breaking for Unsupported() types #4841

Merged
merged 2 commits into from Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 {
Druue marked this conversation as resolved.
Show resolved Hide resolved
Self::new(DefaultKind::DbGenerated(val.into().map(Into::into)))
}

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