Skip to content

Commit 348a55a

Browse files
committed
refactor: unify predicate stripping into normalize_index_predicates, covering external shadow DB path
1 parent a354c1c commit 348a55a

1 file changed

Lines changed: 30 additions & 26 deletions

File tree

  • schema-engine/connectors/sql-schema-connector/src

schema-engine/connectors/sql-schema-connector/src/lib.rs

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,20 @@ impl SqlSchemaConnector {
362362
.datamodel_connector()
363363
.scalar_type_for_native_type(native_type, extension_types)
364364
}
365+
366+
/// Erase index predicates when `partialIndexes` preview feature is off.
367+
fn normalize_index_predicates(&self, db_schema: DatabaseSchema) -> DatabaseSchema {
368+
if self
369+
.inner
370+
.preview_features()
371+
.contains(psl::PreviewFeature::PartialIndexes)
372+
{
373+
return db_schema;
374+
}
375+
let mut inner = SqlDatabaseSchema::from_erased(db_schema);
376+
inner.describer_schema.clear_index_predicates();
377+
DatabaseSchema::new(*inner)
378+
}
365379
}
366380

367381
impl SchemaConnector for SqlSchemaConnector {
@@ -438,15 +452,12 @@ impl SchemaConnector for SqlSchemaConnector {
438452
namespaces: Option<Namespaces>,
439453
) -> BoxFuture<'_, ConnectorResult<DatabaseSchema>> {
440454
Box::pin(async move {
441-
let mut schema = self.inner.describe_schema(namespaces).await?;
442-
if !self
443-
.inner
444-
.preview_features()
445-
.contains(psl::PreviewFeature::PartialIndexes)
446-
{
447-
schema.clear_index_predicates();
448-
}
449-
Ok(DatabaseSchema::new(SqlDatabaseSchema::from(schema)))
455+
self.inner
456+
.describe_schema(namespaces)
457+
.await
458+
.map(SqlDatabaseSchema::from)
459+
.map(DatabaseSchema::new)
460+
.map(|db| self.normalize_index_predicates(db))
450461
})
451462
}
452463

@@ -457,31 +468,24 @@ impl SchemaConnector for SqlSchemaConnector {
457468
filter: &'a SchemaFilter,
458469
) -> BoxFuture<'a, ConnectorResult<DatabaseSchema>> {
459470
Box::pin(async move {
460-
match self.inner.shadow_db_url() {
471+
let db_schema = match self.inner.shadow_db_url() {
461472
Some(connection_string) => {
462473
let target = ExternalShadowDatabase::ConnectionString {
463474
connection_string: connection_string.to_owned(),
464475
preview_features: self.inner.preview_features(),
465476
};
466477
self.schema_dialect()
467478
.schema_from_migrations_with_target(migrations, namespaces, filter, target)
468-
.await
469-
}
470-
None => {
471-
let mut schema = self
472-
.inner
473-
.sql_schema_from_migration_history(migrations, namespaces, filter, UsingExternalShadowDb::No)
474-
.await?;
475-
if !self
476-
.inner
477-
.preview_features()
478-
.contains(psl::PreviewFeature::PartialIndexes)
479-
{
480-
schema.clear_index_predicates();
481-
}
482-
Ok(DatabaseSchema::new(SqlDatabaseSchema::from(schema)))
479+
.await?
483480
}
484-
}
481+
None => self
482+
.inner
483+
.sql_schema_from_migration_history(migrations, namespaces, filter, UsingExternalShadowDb::No)
484+
.await
485+
.map(SqlDatabaseSchema::from)
486+
.map(DatabaseSchema::new)?,
487+
};
488+
Ok(self.normalize_index_predicates(db_schema))
485489
})
486490
}
487491

0 commit comments

Comments
 (0)