From 79f6f09ba683cc77d70f9ea0cf6177a3e35a2525 Mon Sep 17 00:00:00 2001 From: Alberto Schiabel Date: Fri, 26 Jan 2024 17:56:42 +0100 Subject: [PATCH] feat(schema-engine): disabled implicit transaction for batch statements starting from CockroachDB v22.2 (#4632) * feat(schema-engine): disabled implicit transaction for batch statements starting from CockroachDB v22.2 * chore: clippy * feat(schema-engine): add test for https://github.com/prisma/prisma/issues/20851 * chore(docker): update cockroachdb to 23.1.13 * Revert "chore(docker): update cockroachdb to 23.1.13" This reverts commit b027610266d5d3642270fbd12cc4d20da7944f1e. --- .../src/flavour/postgres/connection.rs | 49 ++++++++++---- .../tests/migrations/cockroachdb.rs | 66 +++++++++++++++++++ 2 files changed, 104 insertions(+), 11 deletions(-) diff --git a/schema-engine/connectors/sql-schema-connector/src/flavour/postgres/connection.rs b/schema-engine/connectors/sql-schema-connector/src/flavour/postgres/connection.rs index c5f4c645916e..c3bceb6fb381 100644 --- a/schema-engine/connectors/sql-schema-connector/src/flavour/postgres/connection.rs +++ b/schema-engine/connectors/sql-schema-connector/src/flavour/postgres/connection.rs @@ -29,17 +29,44 @@ impl Connection { let version = quaint.version().await.map_err(quaint_err(&url))?; - if version.map(|v| v.starts_with("CockroachDB CCL v22.2")).unwrap_or(false) { - // first config issue: https://github.com/prisma/prisma/issues/16909 - // second config value: Currently at least version 22.2.5, enums are - // not case-sensitive without this. - quaint - .raw_cmd(indoc! {r#" - SET enable_implicit_transaction_for_batch_statements=false; - SET use_declarative_schema_changer=off - "#}) - .await - .map_err(quaint_err(&url))?; + if let Some(version) = version { + let cockroach_version_prefix = "CockroachDB CCL v"; + + let semver: Option<(u8, u8)> = version.strip_prefix(cockroach_version_prefix).and_then(|v| { + let semver_unparsed: String = v.chars().take_while(|&c| c.is_ascii_digit() || c == '.').collect(); + + // we only consider the major and minor version, as the patch version is not interesting for us + semver_unparsed.split_once('.').and_then(|(major, minor_and_patch)| { + let major = major.parse::().ok(); + + let minor = minor_and_patch + .chars() + .take_while(|&c| c != '.') + .collect::() + .parse::() + .ok(); + + major.zip(minor) + }) + }); + + match semver { + Some((major, minor)) if (major == 22 && minor >= 2) || major >= 23 => { + // we're on 22.2+ or 23+ + // + // first config issue: https://github.com/prisma/prisma/issues/16909 + // second config value: Currently at least version 22.2.5, enums are + // not case-sensitive without this. + quaint + .raw_cmd(indoc! {r#" + SET enable_implicit_transaction_for_batch_statements=false; + SET use_declarative_schema_changer=off + "#}) + .await + .map_err(quaint_err(&url))?; + } + None | Some(_) => (), + }; } Ok(Connection(quaint)) diff --git a/schema-engine/sql-migration-tests/tests/migrations/cockroachdb.rs b/schema-engine/sql-migration-tests/tests/migrations/cockroachdb.rs index 5703a7e5860f..a4a86dcc822f 100644 --- a/schema-engine/sql-migration-tests/tests/migrations/cockroachdb.rs +++ b/schema-engine/sql-migration-tests/tests/migrations/cockroachdb.rs @@ -451,6 +451,72 @@ fn connecting_to_a_cockroachdb_database_with_the_postgresql_connector_fails(_api expected_error.assert_eq(&err); } +// This test follows https://github.com/prisma/prisma-engines/pull/4632. +#[test_connector(tags(CockroachDb))] +fn decimal_to_boolean_migrations_work(api: TestApi) { + let dir = api.create_migrations_directory(); + + let dm1 = r#" + datasource db { + provider = "cockroachdb" + url = env("TEST_DATABASE_URL") + } + + model Cat { + id BigInt @id @default(autoincrement()) + tag Decimal + } + "#; + + api.create_migration("create-cats-decimal", &dm1, &dir) + .send_sync() + .assert_migration_directories_count(1) + .assert_migration("create-cats-decimal", move |migration| { + let expected_script = expect![[r#" + -- CreateTable + CREATE TABLE "Cat" ( + "id" INT8 NOT NULL DEFAULT unique_rowid(), + "tag" DECIMAL(65,30) NOT NULL, + + CONSTRAINT "Cat_pkey" PRIMARY KEY ("id") + ); + "#]]; + + migration.expect_contents(expected_script) + }); + + let dm2 = r#" + datasource db { + provider = "cockroachdb" + url = env("TEST_DATABASE_URL") + } + + model Cat { + id BigInt @id @default(autoincrement()) + tag Boolean + } + "#; + + api.create_migration("migrate-cats-boolean", &dm2, &dir) + .send_sync() + .assert_migration_directories_count(2) + .assert_migration("migrate-cats-boolean", move |migration| { + let expected_script = expect![[r#" + /* + Warnings: + + - Changed the type of `tag` on the `Cat` table. No cast exists, the column would be dropped and recreated, which cannot be done if there is data, since the column is required. + + */ + -- AlterTable + ALTER TABLE "Cat" DROP COLUMN "tag"; + ALTER TABLE "Cat" ADD COLUMN "tag" BOOL NOT NULL; + "#]]; + + migration.expect_contents(expected_script) + }); +} + #[test_connector(tags(CockroachDb))] fn int_to_string_conversions_work(api: TestApi) { let dm1 = r#"