Skip to content

Commit

Permalink
Whoa
Browse files Browse the repository at this point in the history
  • Loading branch information
danieljharvey committed Oct 25, 2024
1 parent 60a96e6 commit b7cb5f0
Show file tree
Hide file tree
Showing 20 changed files with 3,461 additions and 3,401 deletions.
1 change: 1 addition & 0 deletions crates/configuration/src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ pub struct Configuration {
pub connection_uri: String,
pub isolation_level: IsolationLevel,
pub mutations_version: Option<metadata::mutations::MutationsVersion>,
pub mutations_prefix: Option<String>,
}
pub async fn introspect(
input: ParsedConfiguration,
Expand Down
1 change: 1 addition & 0 deletions crates/configuration/src/version3/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,7 @@ pub fn make_runtime_configuration(
isolation_level: configuration.connection_settings.isolation_level,
mutations_version: convert_mutations_version(configuration.mutations_version),
configuration_version_tag: VersionTag::Version3,
mutations_prefix: None,
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub fn make_runtime_configuration(
isolation_level: parsed_config.connection_settings.isolation_level,
mutations_version: convert_mutations_version(parsed_config.mutations_version),
configuration_version_tag: VersionTag::Version4,
mutations_prefix: None,
})
}

Expand Down
5 changes: 5 additions & 0 deletions crates/configuration/src/version5/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ pub struct ParsedConfiguration {
/// Which version of the generated mutation procedures to include in the schema response
#[serde(default)]
pub mutations_version: Option<metadata::mutations::MutationsVersion>,
/// Provide a custom prefix for generated mutation names. Defaults to mutations version.
#[serde(default)]
pub mutations_prefix: Option<String>,
}

#[derive(Clone, PartialEq, Eq, Debug, Deserialize, Serialize, JsonSchema)]
Expand All @@ -68,6 +71,7 @@ impl ParsedConfiguration {
metadata: metadata::Metadata::default(),
introspection_options: options::IntrospectionOptions::default(),
mutations_version: Some(metadata::mutations::MutationsVersion::V2),
mutations_prefix: Some(String::new()),
}
}

Expand Down Expand Up @@ -192,6 +196,7 @@ pub async fn introspect(
},
introspection_options: args.introspection_options,
mutations_version: args.mutations_version,
mutations_prefix: args.mutations_prefix,
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub fn make_runtime_configuration(
isolation_level: parsed_config.connection_settings.isolation_level,
mutations_version: convert_mutations_version(parsed_config.mutations_version),
configuration_version_tag: VersionTag::Version4,
mutations_prefix: parsed_config.mutations_prefix,
})
}

Expand Down
1 change: 1 addition & 0 deletions crates/configuration/src/version5/upgrade_from_v4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub fn upgrade_from_v4(v: version4::ParsedConfiguration) -> super::ParsedConfigu
introspection_options: ugrade_introspection_options(introspection_options),
metadata: upgrade_metadata(metadata),
mutations_version: mutations_version.map(upgrade_mutations_version),
mutations_prefix: Some(String::new()), // default to no prefixes
}
}

Expand Down
1 change: 1 addition & 0 deletions crates/connectors/ndc-postgres/src/mutation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ fn plan_mutation(
operation,
request.collection_relationships.clone(),
configuration.mutations_version,
configuration.mutations_prefix.clone(),
)
})
.collect::<Result<Vec<_>, _>>()?;
Expand Down
8 changes: 7 additions & 1 deletion crates/connectors/ndc-postgres/src/schema/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,13 @@ pub fn get_schema(
.collect();

let mut more_object_types = BTreeMap::new();
let env = Env::new(metadata, BTreeMap::new(), config.mutations_version, None);
let env = Env::new(
metadata,
BTreeMap::new(),
config.mutations_version,
config.mutations_prefix.clone(),
None,
);
let generated_procedures: Vec<models::ProcedureInfo> =
query_engine_translation::translation::mutation::generate::generate(&env)
.iter()
Expand Down
4 changes: 4 additions & 0 deletions crates/query-engine/translation/src/translation/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub struct Env<'request> {
pub(crate) metadata: &'request metadata::Metadata,
relationships: BTreeMap<models::RelationshipName, models::Relationship>,
pub(crate) mutations_version: Option<metadata::mutations::MutationsVersion>,
pub(crate) mutations_prefix: Option<String>,
variables_table: Option<sql::ast::TableReference>,
}

Expand Down Expand Up @@ -208,6 +209,7 @@ impl<'request> Env<'request> {
metadata: &temp_metadata,
relationships: BTreeMap::new(),
mutations_version: None,
mutations_prefix: None,
variables_table: None,
};
f(temp_env)
Expand All @@ -218,12 +220,14 @@ impl<'request> Env<'request> {
metadata: &'request metadata::Metadata,
relationships: BTreeMap<models::RelationshipName, models::Relationship>,
mutations_version: Option<metadata::mutations::MutationsVersion>,
mutations_prefix: Option<String>,
variables_table: Option<sql::ast::TableReference>,
) -> Self {
Env {
metadata,
relationships,
mutations_version,
mutations_prefix,
variables_table,
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ pub fn generate(env: &Env) -> BTreeMap<models::ProcedureName, Mutation> {
.into_iter()
.map(|(name, mutation)| (name, Mutation::V1(mutation)))
.collect(),
Some(mutations::MutationsVersion::V2) => v2::generate(&env.metadata.tables)
.into_iter()
.map(|(name, mutation)| (name, Mutation::V2(mutation)))
.collect(),
Some(mutations::MutationsVersion::V2) => {
v2::generate(&env.metadata.tables, &env.mutations_prefix)
.into_iter()
.map(|(name, mutation)| (name, Mutation::V2(mutation)))
.collect()
}
None => BTreeMap::new(),
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,15 @@ pub fn translate(
operation: models::MutationOperation,
collection_relationships: BTreeMap<models::RelationshipName, models::Relationship>,
mutations_version: Option<metadata::mutations::MutationsVersion>,
mutations_prefix: Option<String>,
) -> Result<sql::execution_plan::Mutation, Error> {
let env = Env::new(metadata, collection_relationships, mutations_version, None);
let env = Env::new(
metadata,
collection_relationships,
mutations_version,
mutations_prefix,
None,
);

match operation {
models::MutationOperation::Procedure {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,25 @@ pub struct CheckArgument {
pub argument_name: models::ArgumentName,
pub description: String,
}

// the old default was to prefix generated mutations with `v2_` or `v1_`
// but now we are able to override this
pub fn get_version_prefix(mutations_prefix: &Option<String>) -> String {
match mutations_prefix {
None => "v2_".to_string(),
Some(str) => match str.as_str() {
"" => String::new(),
_ => format!("{str}_"),
},
}
}

#[test]
fn test_version_prefix() {
assert_eq!(get_version_prefix(&None), "_v2".to_string());
assert_eq!(
get_version_prefix(&Some("horse".into())),
"horse_".to_string()
);
assert_eq!(get_version_prefix(&Some("".into())), "".to_string());
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
//! Auto-generate delete mutations and translate them into sql ast.
use super::common::{self, CheckArgument};
use crate::translation::error::Error;
use crate::translation::helpers::{self, TableSourceAndReference};
use crate::translation::query::filtering;
use crate::translation::query::values;
use ndc_models as models;
use ndc_postgres_configuration::Configuration;
use nonempty::NonEmpty;
use query_engine_metadata::metadata;
use query_engine_metadata::metadata::database;
use query_engine_sql::sql;
use std::collections::BTreeMap;

use super::common::{self, CheckArgument};

/// A representation of an auto-generated delete mutation.
///
/// This can get us `DELETE FROM <table> WHERE column = <column_name_arg>, ...`.
Expand All @@ -33,6 +33,7 @@ pub enum DeleteMutation {
pub fn generate_delete_by_unique(
collection_name: &models::CollectionName,
table_info: &database::TableInfo,
mutations_prefix: &Option<String>,
) -> Vec<(models::ProcedureName, DeleteMutation)> {
table_info
.uniqueness_constraints
Expand All @@ -49,7 +50,8 @@ pub fn generate_delete_by_unique(
)?;

let name = format!(
"delete_{collection_name}_by_{constraint_name}"
"{}delete_{collection_name}_by_{constraint_name}",
common::get_version_prefix(mutations_prefix)
)
.into();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@ pub enum Mutation {
}

/// Given our introspection data, work out all the mutations we can generate
pub fn generate(tables_info: &database::TablesInfo) -> BTreeMap<models::ProcedureName, Mutation> {
pub fn generate(
tables_info: &database::TablesInfo,
mutations_prefix: &Option<String>,
) -> BTreeMap<models::ProcedureName, Mutation> {
let mut mutations = BTreeMap::new();
for (collection_name, table_info) in &tables_info.0 {
// Delete mutations.
let delete_mutations = generate_delete_by_unique(collection_name, table_info);
let delete_mutations =
generate_delete_by_unique(collection_name, table_info, mutations_prefix);
for (name, delete_mutation) in delete_mutations {
mutations.insert(name, Mutation::DeleteMutation(delete_mutation));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::collections::BTreeMap;
use crate::translation::error::Error;
use crate::translation::helpers::{Env, State};
use ndc_models as models;
use ndc_postgres_configuration::Configuration;
use query_engine_sql::sql;

/// Translate a built-in delete mutation into an ExecutionPlan (SQL) to be run against the database.
Expand All @@ -23,7 +24,7 @@ pub fn translate(
),
Error,
> {
let mutation = lookup_generated_mutation(env, procedure_name)?;
let mutation = lookup_generated_mutation(env, procedure_name, &env.mutations_prefix)?;

Ok(match mutation {
super::generate::Mutation::DeleteMutation(delete) => {
Expand Down Expand Up @@ -77,11 +78,12 @@ pub fn translate(
fn lookup_generated_mutation(
env: &Env<'_>,
procedure_name: &models::ProcedureName,
mutations_prefix: &Option<String>,
) -> Result<super::generate::Mutation, Error> {
// this means we generate them on every mutation request
// i don't think this is optimal but I'd like to get this working before working out
// where best to store these
let generated = super::generate::generate(&env.metadata.tables);
let generated = super::generate::generate(&env.metadata.tables, mutations_prefix);

generated
.get(procedure_name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,7 @@ pub fn generate_update_by_unique(
keys,
)?;

let name = format!(
"update_{collection_name}_by_{constraint_name}"
)
.into();
let name = format!("update_{collection_name}_by_{constraint_name}").into();

let description = format!(
"Update any row on the '{collection_name}' collection using the {}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub fn translate(
metadata,
query_request.collection_relationships,
None,
None,
variables_table_ref,
);

Expand Down
1 change: 1 addition & 0 deletions crates/query-engine/translation/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ pub async fn test_mutation_translation(
operation,
request.collection_relationships.clone(),
Some(query_engine_metadata::metadata::mutations::MutationsVersion::V2),
configuration.mutations_prefix.clone(),
)
})
.collect::<Result<Vec<_>, translation::error::Error>>()?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3123,5 +3123,6 @@ expression: default_configuration
"varchar": "string"
}
},
"mutationsVersion": "v2"
"mutationsVersion": "v2",
"mutationsPrefix": ""
}
Loading

0 comments on commit b7cb5f0

Please sign in to comment.