From 9c937f59de97c5f85bca9f7315532f5655b64ee3 Mon Sep 17 00:00:00 2001 From: Florian Goessler Date: Tue, 28 Jan 2025 09:16:30 +0100 Subject: [PATCH 1/2] fix(migrations): on cockroach db wrap migration execution on the shadow db into a transaction to speed it up --- .../sql-schema-connector/src/flavour/postgres.rs | 6 +++++- .../src/flavour/postgres/native/shadow_db.rs | 9 +++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/schema-engine/connectors/sql-schema-connector/src/flavour/postgres.rs b/schema-engine/connectors/sql-schema-connector/src/flavour/postgres.rs index e1c23ed121d7..74fb619390aa 100644 --- a/schema-engine/connectors/sql-schema-connector/src/flavour/postgres.rs +++ b/schema-engine/connectors/sql-schema-connector/src/flavour/postgres.rs @@ -492,7 +492,11 @@ impl SqlFlavour for PostgresFlavour { .params() .and_then(|p| p.connector_params.shadow_database_connection_string.clone()) }); - let mut shadow_database = PostgresFlavour::default(); + let mut shadow_database = if self.is_cockroachdb() { + PostgresFlavour::new_cockroach() + } else { + PostgresFlavour::default() + }; match shadow_database_connection_string { Some(shadow_database_connection_string) => Box::pin(async move { diff --git a/schema-engine/connectors/sql-schema-connector/src/flavour/postgres/native/shadow_db.rs b/schema-engine/connectors/sql-schema-connector/src/flavour/postgres/native/shadow_db.rs index 8e024bbf7b27..83e635a87db8 100644 --- a/schema-engine/connectors/sql-schema-connector/src/flavour/postgres/native/shadow_db.rs +++ b/schema-engine/connectors/sql-schema-connector/src/flavour/postgres/native/shadow_db.rs @@ -1,3 +1,4 @@ +use crate::flavour::postgres::PostgresProvider::CockroachDb; use crate::flavour::{PostgresFlavour, SqlFlavour}; use schema_connector::{migrations_directory::MigrationDirectory, ConnectorResult}; use schema_connector::{ConnectorError, Namespaces}; @@ -8,6 +9,10 @@ pub async fn sql_schema_from_migrations_history( mut shadow_db: PostgresFlavour, namespaces: Option, ) -> ConnectorResult { + if shadow_db.provider == CockroachDb { + shadow_db.raw_cmd("BEGIN;").await?; + } + for migration in migrations { let script = migration.read_migration_script()?; @@ -25,5 +30,9 @@ pub async fn sql_schema_from_migrations_history( })?; } + if shadow_db.provider == CockroachDb { + shadow_db.raw_cmd("COMMIT;").await?; + } + shadow_db.describe_schema(namespaces).await } From f1f05474f94cc9f20ab5fb800f48dc47f4ab4038 Mon Sep 17 00:00:00 2001 From: Florian Goessler Date: Tue, 28 Jan 2025 10:50:59 +0100 Subject: [PATCH 2/2] docu(migrations): add comment --- .../src/flavour/postgres/native/shadow_db.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/schema-engine/connectors/sql-schema-connector/src/flavour/postgres/native/shadow_db.rs b/schema-engine/connectors/sql-schema-connector/src/flavour/postgres/native/shadow_db.rs index 83e635a87db8..771eabbb310c 100644 --- a/schema-engine/connectors/sql-schema-connector/src/flavour/postgres/native/shadow_db.rs +++ b/schema-engine/connectors/sql-schema-connector/src/flavour/postgres/native/shadow_db.rs @@ -10,6 +10,11 @@ pub async fn sql_schema_from_migrations_history( namespaces: Option, ) -> ConnectorResult { if shadow_db.provider == CockroachDb { + // CockroachDB is very slow in applying DDL statements. + // A workaround to it is to run the statements in a transaction block. This comes with some + // drawbacks and limitations though, so we only apply this when creating a shadow db. + // See https://www.cockroachlabs.com/docs/stable/online-schema-changes#limitations + // Original GitHub issue with context: https://github.com/prisma/prisma/issues/12384#issuecomment-1152523689 shadow_db.raw_cmd("BEGIN;").await?; }