From 4767cae5af70f60ad05e14222e663255fc63a65e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20G=C3=B6=C3=9Fler?= Date: Wed, 29 Jan 2025 17:59:09 +0100 Subject: [PATCH] fix(migrations): on cockroach db wrap migration execution on the shadow db into a transaction to speed it up (#5138) --- .../sql-schema-connector/src/flavour/postgres.rs | 6 +++++- .../src/flavour/postgres/native/shadow_db.rs | 14 ++++++++++++++ 2 files changed, 19 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 e1c23ed121d..74fb619390a 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 8e024bbf7b2..771eabbb310 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,15 @@ pub async fn sql_schema_from_migrations_history( mut shadow_db: PostgresFlavour, 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?; + } + for migration in migrations { let script = migration.read_migration_script()?; @@ -25,5 +35,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 }