Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(migrations): on cockroach db wrap migration execution on the shadow db into a transaction to speed it up #5138

Merged
merged 3 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
Loading