Skip to content

Commit

Permalink
fix(migrations): on cockroach db wrap migration execution on the shad…
Browse files Browse the repository at this point in the history
…ow db into a transaction to speed it up (#5138)
  • Loading branch information
FGoessler authored Jan 29, 2025
1 parent 75c3406 commit 4767cae
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -8,6 +9,15 @@ pub async fn sql_schema_from_migrations_history(
mut shadow_db: PostgresFlavour,
namespaces: Option<Namespaces>,
) -> ConnectorResult<SqlSchema> {
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()?;

Expand All @@ -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
}

0 comments on commit 4767cae

Please sign in to comment.