Skip to content

Commit

Permalink
fix(connector-test-kit-rs): fix tests by enabling support for "Featur…
Browse files Browse the repository at this point in the history
…eMapWithProvider"
  • Loading branch information
jkomyno committed Nov 26, 2024
1 parent 28d08fb commit 6328f2e
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 24 deletions.
2 changes: 1 addition & 1 deletion psl/diagnostics/src/warning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl DatamodelWarning {
}

pub fn new_preview_feature_renamed_for_provider(
provider: &'static str,
provider: &str,
deprecated_feature: &str,
renamed_feature: impl Display,
prisly_link_endpoint: &str,
Expand Down
27 changes: 14 additions & 13 deletions psl/psl-core/src/common/preview_features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,12 @@ features!(
);

#[derive(Copy, Clone, Debug, PartialOrd, Ord, PartialEq, Eq)]
struct RenamedFeatureKey {
struct RenamedFeatureKey<'a> {
/// The old, deprecated preview feature that was renamed.
pub from: PreviewFeature,

/// The provider that the feature was renamed for.
pub provider: Option<&'static str>,
pub provider: Option<&'a str>,
}

#[derive(Debug, Copy, Clone)]
Expand All @@ -107,9 +107,9 @@ pub(crate) struct RenamedFeatureValue {
}

#[derive(Debug, Clone)]
pub(crate) enum RenamedFeature {
pub(crate) enum RenamedFeature<'a> {
/// The preview feature was renamed for a specific provider.
ForProvider((&'static str, RenamedFeatureValue)),
ForProvider((&'a str, RenamedFeatureValue)),

/// The preview feature was renamed for all providers.
AllProviders(RenamedFeatureValue),
Expand All @@ -127,26 +127,27 @@ struct FeatureMap {
deprecated: PreviewFeatures,

/// History of renamed deprecated features.
renamed: BTreeMap<RenamedFeatureKey, RenamedFeatureValue>,
renamed: BTreeMap<RenamedFeatureKey<'static>, RenamedFeatureValue>,

/// Hidden preview features are valid features, but are not propagated into the tooling
/// (as autocomplete or similar) or into error messages (eg. showing a list of valid features).
hidden: PreviewFeatures,
}

#[derive(Debug, Clone)]
pub struct FeatureMapWithProvider {
provider: Option<&'static str>,
pub struct FeatureMapWithProvider<'a> {
provider: Option<&'a str>,
feature_map: FeatureMap,
}

/// The default feature map with an unknown provider.
/// This is used for convenience in `prisma/language-tools`, which needs the list of all available preview features
/// before a provider is necessarily known.
pub static ALL_PREVIEW_FEATURES: LazyLock<FeatureMapWithProvider> = LazyLock::new(|| FeatureMapWithProvider::new(None));
pub static ALL_PREVIEW_FEATURES: LazyLock<FeatureMapWithProvider<'static>> =
LazyLock::new(|| FeatureMapWithProvider::new(None));

impl FeatureMapWithProvider {
pub fn new(connector_provider: Option<&'static str>) -> FeatureMapWithProvider {
impl<'a> FeatureMapWithProvider<'a> {
pub fn new(connector_provider: Option<&'a str>) -> FeatureMapWithProvider<'a> {
// Generator preview features (alphabetically sorted)
let feature_map: FeatureMap = FeatureMap {
active: enumflags2::make_bitflags!(PreviewFeature::{
Expand Down Expand Up @@ -252,12 +253,12 @@ impl FeatureMapWithProvider {
}

/// Was the given preview feature deprecated and renamed?
pub(crate) fn is_renamed(&self, flag: PreviewFeature) -> Option<RenamedFeature> {
pub(crate) fn is_renamed(&'a self, flag: PreviewFeature) -> Option<RenamedFeature<'a>> {
// Check for a renamed feature specific to the provider. This is only possible if a provider is not None.
let provider_specific = self.provider.and_then(|provider| {
self.feature_map
.renamed
.get(&RenamedFeatureKey {
.get(&RenamedFeatureKey::<'a> {
from: flag,
provider: Some(provider),
})
Expand All @@ -268,7 +269,7 @@ impl FeatureMapWithProvider {
provider_specific.or_else(|| {
self.feature_map
.renamed
.get(&RenamedFeatureKey {
.get(&RenamedFeatureKey::<'a> {
from: flag,
provider: None,
})
Expand Down
2 changes: 1 addition & 1 deletion psl/psl-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ fn validate_configuration(
// We need to know the active provider to determine which features are active.
// This was originally introduced because the `fullTextSearch` preview feature will hit GA stage
// one connector at a time (Prisma 6 GAs it for MySQL, other connectors may follow in future releases).
let feature_map_with_provider: FeatureMapWithProvider = datasources
let feature_map_with_provider: FeatureMapWithProvider<'_> = datasources
.first()
.map(|ds| Some(ds.active_provider))
.map(FeatureMapWithProvider::new)
Expand Down
6 changes: 3 additions & 3 deletions psl/psl-core/src/validate/generator_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const FIRST_CLASS_PROPERTIES: &[&str] = &[PROVIDER_KEY, OUTPUT_KEY, BINARY_TARGE
pub(crate) fn load_generators_from_ast(
ast_schema: &ast::SchemaAst,
diagnostics: &mut Diagnostics,
feature_map_with_provider: &FeatureMapWithProvider,
feature_map_with_provider: &FeatureMapWithProvider<'_>,
) -> Vec<Generator> {
let mut generators: Vec<Generator> = Vec::new();

Expand All @@ -41,7 +41,7 @@ pub(crate) fn load_generators_from_ast(
fn lift_generator(
ast_generator: &ast::GeneratorConfig,
diagnostics: &mut Diagnostics,
feature_map_with_provider: &FeatureMapWithProvider,
feature_map_with_provider: &FeatureMapWithProvider<'_>,
) -> Option<Generator> {
let generator_name = ast_generator.name.name.as_str();
let args: HashMap<_, &Expression> = ast_generator
Expand Down Expand Up @@ -140,7 +140,7 @@ fn lift_generator(

fn parse_and_validate_preview_features(
preview_features: Vec<&str>,
feature_map_with_provider: &FeatureMapWithProvider,
feature_map_with_provider: &FeatureMapWithProvider<'_>,
span: ast::Span,
diagnostics: &mut Diagnostics,
) -> BitFlags<PreviewFeature> {
Expand Down
1 change: 1 addition & 0 deletions psl/psl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub use psl_core::{
ConnectorRegistry,
Datasource,
DatasourceConnectorData,
FeatureMapWithProvider,
Generator,
GeneratorConfigValue,
PreviewFeature,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl ConnectorTagInterface for PostgresConnectorTag {
}

fn datamodel_provider(&self) -> &'static str {
"postgres"
"postgresql"
}

fn datamodel_renderer(&self) -> Box<dyn DatamodelRenderer> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
use indoc::indoc;
use itertools::Itertools;
use once_cell::sync::Lazy;
use psl::ALL_PREVIEW_FEATURES;
use psl::FeatureMapWithProvider;
use regex::Regex;

/// Test configuration, loaded once at runtime.
Expand Down Expand Up @@ -42,7 +42,7 @@ pub fn render_test_datamodel(
isolation_level: Option<&'static str>,
) -> String {
let (tag, version) = CONFIG.test_connector().unwrap();
let preview_features = render_preview_features(excluded_features);
let preview_features = render_preview_features(tag.datamodel_provider(), excluded_features);

let is_multi_schema = !db_schemas.is_empty();

Expand Down Expand Up @@ -93,13 +93,14 @@ fn process_template(template: String, renderer: Box<dyn DatamodelRenderer>) -> S
})
}

fn render_preview_features(excluded_features: &[&str]) -> String {
fn render_preview_features(provider: &str, excluded_features: &[&str]) -> String {
let excluded_features: Vec<_> = excluded_features.iter().map(|f| format!(r#""{f}""#)).collect();
let feature_map_with_provider = FeatureMapWithProvider::new(Some(provider));

ALL_PREVIEW_FEATURES
feature_map_with_provider
.active_features()
.iter()
.chain(ALL_PREVIEW_FEATURES.hidden_features())
.chain(feature_map_with_provider.hidden_features())
.map(|f| format!(r#""{f}""#))
.filter(|f| !excluded_features.contains(f))
.join(", ")
Expand Down

0 comments on commit 6328f2e

Please sign in to comment.