Skip to content

Commit

Permalink
Confirm that prisma/prisma#12095 is fixed
Browse files Browse the repository at this point in the history
...by the combination of the new postgres default parsing code from the
scalar list defaults work and the new string literal code in PSL.

closes prisma/prisma#12095
  • Loading branch information
tomhoule committed Jun 22, 2022
1 parent 71db404 commit be1f1b8
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 21 deletions.
Expand Up @@ -334,7 +334,8 @@ async fn introspecting_json_defaults_on_cockroach(api: &TestApi) -> TestResult {
id INTEGER NOT NULL PRIMARY KEY,
json JSON DEFAULT '[]'::json,
jsonb JSONB DEFAULT '{}'::jsonb,
jsonb_object JSONB DEFAULT '{"a": ["b"], "c": true, "d": null }'
jsonb_string JSONB DEFAULT E'"ab\'c"',
jsonb_object JSONB DEFAULT '{"a": ["b''"], "c": true, "d": null }'
);
"#};
Expand All @@ -345,7 +346,8 @@ async fn introspecting_json_defaults_on_cockroach(api: &TestApi) -> TestResult {
id Int @id
json Json? @default("[]")
jsonb Json? @default("{}")
jsonb_object Json? @default("{\"a\": [\"b\"], \"c\": true, \"d\": null}")
jsonb_string Json? @default("\"ab'c\"")
jsonb_object Json? @default("{\"a\": [\"b'\"], \"c\": true, \"d\": null}")
}
"#]];

Expand Down
@@ -1,8 +1,7 @@
use barrel::types;
use indoc::indoc;
use introspection_engine_tests::test_api::*;

#[test_connector(tags(Postgres))]
#[test_connector(tags(Postgres), exclude(CockroachDb))]
async fn string_defaults_that_need_escaping(api: &TestApi) -> TestResult {
let setup = r#"
CREATE TABLE "stringstest" (
Expand Down Expand Up @@ -213,27 +212,36 @@ async fn introspecting_now_functions(api: &TestApi) -> TestResult {
Ok(())
}

// https://github.com/prisma/prisma/issues/12095
#[test_connector(tags(Postgres), exclude(CockroachDb))]
async fn introspecting_a_table_with_json_type_must_work(api: &TestApi) -> TestResult {
api.barrel()
.execute(|migration| {
migration.create_table("Blog", |t| {
t.add_column("id", types::primary());
t.add_column("json", types::json());
});
})
.await?;

let dm = indoc! {r#"
model Blog {
id Int @id @default(autoincrement())
json Json @db.Json
async fn a_table_with_json_columns(api: &TestApi) -> TestResult {
let setup = r#"
CREATE TABLE "Foo" (
"id" INTEGER NOT NULL,
"bar" JSONB DEFAULT '{"message": "This message includes a quote: Here''s it!"}',
CONSTRAINT "Foo_pkey" PRIMARY KEY ("id")
);
"#;

api.raw_cmd(setup).await;

let expected = expect![[r#"
generator client {
provider = "prisma-client-js"
}
"#};
let result = api.introspect().await?;
datasource db {
provider = "postgresql"
url = "env(TEST_DATABASE_URL)"
}
api.assert_eq_datamodels(dm, &result);
model Foo {
id Int @id
bar Json? @default("{\"message\": \"This message includes a quote: Here's it!\"}")
}
"#]];

api.expect_datamodel(&expected).await;
Ok(())
}
Expand Up @@ -1185,3 +1185,37 @@ fn alter_sequence(api: TestApi) {
// api.schema_push(schema).send().assert_green();
// api.schema_push(schema).send().assert_green().assert_no_steps();
// }

// https://github.com/prisma/prisma/issues/12095
#[test_connector(tags(CockroachDb))]
fn json_defaults_with_escaped_quotes_work(api: TestApi) {
let schema = r#"
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
model Foo {
id Int @id
bar Json? @default("{\"message\": \"This message includes a quote: Here''s it!\"}")
}
"#;

api.schema_push(schema)
.send()
.assert_green()
.assert_has_executed_steps();
api.schema_push(schema).send().assert_green().assert_no_steps();

let sql = expect![[r#"
-- CreateTable
CREATE TABLE "Foo" (
"id" INT4 NOT NULL,
"bar" JSONB DEFAULT '{"message": "This message includes a quote: Here''''s it!"}',
CONSTRAINT "Foo_pkey" PRIMARY KEY ("id")
);
"#]];

api.expect_sql_for_schema(schema, &sql);
}
Expand Up @@ -653,3 +653,37 @@ fn scalar_list_default_diffing(api: TestApi) {
.assert_has_executed_steps();
api.schema_push(schema_2).send().assert_green().assert_no_steps();
}

// https://github.com/prisma/prisma/issues/12095
#[test_connector(tags(Postgres), exclude(CockroachDb))]
fn json_defaults_with_escaped_quotes_work(api: TestApi) {
let schema = r#"
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
model Foo {
id Int @id
bar Json? @default("{\"message\": \"This message includes a quote: Here''s it!\"}")
}
"#;

api.schema_push(schema)
.send()
.assert_green()
.assert_has_executed_steps();
api.schema_push(schema).send().assert_green().assert_no_steps();

let sql = expect![[r#"
-- CreateTable
CREATE TABLE "Foo" (
"id" INTEGER NOT NULL,
"bar" JSONB DEFAULT '{"message": "This message includes a quote: Here''''s it!"}',
CONSTRAINT "Foo_pkey" PRIMARY KEY ("id")
);
"#]];

api.expect_sql_for_schema(schema, &sql);
}

0 comments on commit be1f1b8

Please sign in to comment.