Skip to content

Commit

Permalink
fix: avoid unreachable code with empty datamodel (#5093)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacek-prisma authored Dec 18, 2024
1 parent 3dc72ed commit acc7a89
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 8 deletions.
8 changes: 8 additions & 0 deletions psl/psl-core/src/datamodel_connector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,14 @@ pub trait Connector: Send + Sync {
) -> prisma_value::PrismaValueResult<Vec<u8>> {
unreachable!("This method is only implemented on connectors with lateral join support.")
}

fn is_sql(&self) -> bool {
self.flavour().is_sql()
}

fn is_mongo(&self) -> bool {
self.flavour().is_mongo()
}
}

#[derive(Copy, Clone, Debug, PartialEq)]
Expand Down
8 changes: 8 additions & 0 deletions psl/psl-core/src/datamodel_connector/empty_connector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@ impl Connector for EmptyDatamodelConnector {
fn flavour(&self) -> Flavour {
unreachable!()
}

fn is_sql(&self) -> bool {
false
}

fn is_mongo(&self) -> bool {
false
}
}

#[cfg(test)]
Expand Down
10 changes: 10 additions & 0 deletions query-engine/core-tests/tests/empty_datamodel.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use std::sync::Arc;

#[test]
fn empty_datamodel_loads() {
let schema = "";
let parsed_schema = psl::parse_schema(schema).unwrap();
let schema = schema::build(Arc::new(parsed_schema), true);
assert!(!schema.is_mongo());
assert!(!schema.is_sql());
}
4 changes: 2 additions & 2 deletions query-engine/schema/src/build/output_types/mutation_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ pub(crate) fn mutation_fields(ctx: &QuerySchema) -> Vec<FieldFn> {
field!(delete_many_field, model);
}

if ctx.enable_raw_queries && ctx.connector_favlour().is_sql() {
if ctx.enable_raw_queries && ctx.is_sql() {
fields.push(Box::new(|_| create_execute_raw_field()));
fields.push(Box::new(|_| create_query_raw_field()));
}

if ctx.enable_raw_queries && ctx.connector_favlour().is_mongo() {
if ctx.enable_raw_queries && ctx.is_mongo() {
fields.push(Box::new(|_| create_mongodb_run_command_raw()));
}

Expand Down
2 changes: 1 addition & 1 deletion query-engine/schema/src/build/output_types/query_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub(crate) fn query_fields(ctx: &QuerySchema) -> Vec<FieldFn> {
field!(find_unique_field, model);
field!(find_unique_or_throw_field, model);

if ctx.enable_raw_queries && ctx.connector_favlour().is_mongo() {
if ctx.enable_raw_queries && ctx.is_mongo() {
let model_cloned = model.clone();
fields.push(Box::new(move |_ctx| mongo_find_raw_field(&model_cloned)));
fields.push(Box::new(move |_ctx| mongo_aggregate_raw_field(&model)));
Expand Down
12 changes: 7 additions & 5 deletions query-engine/schema/src/query_schema.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use crate::{IdentifierType, ObjectType, OutputField};
use psl::{
can_support_relation_load_strategy,
datamodel_connector::{
Connector, ConnectorCapabilities, ConnectorCapability, Flavour, JoinStrategySupport, RelationMode,
},
datamodel_connector::{Connector, ConnectorCapabilities, ConnectorCapability, JoinStrategySupport, RelationMode},
has_capability, parser_database as db, PreviewFeature, PreviewFeatures,
};
use query_structure::InternalDataModel;
Expand Down Expand Up @@ -220,8 +218,12 @@ impl QuerySchema {
.contains(ConnectorCapability::NativeUpsert)
}

pub fn connector_favlour(&self) -> Flavour {
self.connector.flavour()
pub fn is_sql(&self) -> bool {
self.connector.is_sql()
}

pub fn is_mongo(&self) -> bool {
self.connector.is_mongo()
}
}

Expand Down

0 comments on commit acc7a89

Please sign in to comment.