Skip to content

Commit

Permalink
Use the new renderer in intro
Browse files Browse the repository at this point in the history
  • Loading branch information
Julius de Bruijn committed Oct 27, 2022
1 parent 151905f commit 235b53a
Show file tree
Hide file tree
Showing 49 changed files with 1,191 additions and 2,025 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,12 @@ pub(super) async fn sample(

let data_model = if ctx.render_config {
format!(
"{}\n{}\n{}",
"{}\n{}",
render::Configuration::from_psl(ctx.configuration()),
render::Datamodel::from_dml(ctx.datasource(), &data_model),
psl::render_datamodel_to_string(&data_model, Some(ctx.configuration())),
)
} else {
format!(
"{}\n{}",
render::Datamodel::from_dml(ctx.datasource(), &data_model),
psl::render_datamodel_to_string(&data_model, Some(ctx.configuration())),
)
render::Datamodel::from_dml(ctx.datasource(), &data_model).to_string()
};

Ok(IntrospectionResult {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ use crate::sampler::field_type::FieldType;
use convert_case::{Case, Casing};
use introspection_connector::Warning;
use mongodb_schema_describer::{IndexFieldProperty, IndexWalker};
use psl::dml::{self, WithDatabaseName, WithName};
use psl::{
datamodel_connector::constraint_names::ConstraintNames,
dml::{self, WithDatabaseName, WithName},
};
use std::collections::BTreeMap;

/// Add described indices to the models.
Expand Down Expand Up @@ -64,26 +67,48 @@ pub(super) fn add_to_models(
path,
sort_order: match f.property {
IndexFieldProperty::Text => None,
IndexFieldProperty::Ascending => Some(dml::SortOrder::Asc),
IndexFieldProperty::Ascending if index.r#type().is_fulltext() => Some(dml::SortOrder::Asc),
IndexFieldProperty::Ascending => None,
IndexFieldProperty::Descending => Some(dml::SortOrder::Desc),
},
length: None,
operator_class: None,
}
})
.collect();
.collect::<Vec<_>>();

let tpe = match index.r#type() {
mongodb_schema_describer::IndexType::Normal => dml::IndexType::Normal,
mongodb_schema_describer::IndexType::Unique => dml::IndexType::Unique,
mongodb_schema_describer::IndexType::Fulltext => dml::IndexType::Fulltext,
};

// TOM WE NEED TO TALK ABOUT THIS!
let column_names = fields
.iter()
.flat_map(|f| f.path.iter().map(|p| p.0.as_str()))
.collect::<Vec<_>>();

let default_name = match tpe {
dml::IndexType::Unique => {
ConstraintNames::unique_index_name(model_name, &column_names, psl::builtin_connectors::MONGODB)
}
_ => {
ConstraintNames::non_unique_index_name(model_name, &column_names, psl::builtin_connectors::MONGODB)
}
};

let db_name = if index.name() == default_name {
None
} else {
Some(index.name().to_string())
};

model.add_index(dml::IndexDefinition {
fields,
tpe,
defined_on_field,
db_name: Some(index.name().to_string()),
db_name,
name: None,
algorithm: None,
clustered: None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ fn remapping_model_fields_with_numbers() {
let expected = expect![[r#"
model Outer {
id String @id @default(auto()) @map("_id") @db.ObjectId
// This field was commented out because of an invalid name. Please provide a valid one that matches [a-zA-Z][a-zA-Z0-9_]*
/// This field was commented out because of an invalid name. Please provide a valid one that matches [a-zA-Z][a-zA-Z0-9_]*
// 1 Int @map("1")
}
"#]];
Expand Down Expand Up @@ -159,8 +159,8 @@ fn remapping_model_fields_with_numbers_dirty() {
let expected = expect![[r#"
model Outer {
id String @id @default(auto()) @map("_id") @db.ObjectId
// Multiple data types found: String: 50%, Int: 50% out of 2 sampled entries
// This field was commented out because of an invalid name. Please provide a valid one that matches [a-zA-Z][a-zA-Z0-9_]*
/// Multiple data types found: String: 50%, Int: 50% out of 2 sampled entries
/// This field was commented out because of an invalid name. Please provide a valid one that matches [a-zA-Z][a-zA-Z0-9_]*
// 1 Json @map("1")
}
"#]];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::{
use datamodel_renderer as render;
use introspection_connector::Version;
use psl::{
datamodel_connector::constraint_names::ConstraintNames,
dml::{self, Datamodel, Field, Model, PrimaryKeyDefinition, PrimaryKeyField, RelationField, SortOrder},
parser_database::{ast, walkers},
Configuration,
Expand Down Expand Up @@ -91,9 +92,8 @@ pub(crate) fn introspect(ctx: &mut Context) -> Result<(Version, String, bool), S
};

let rendered = format!(
"{}\n{}\n{}",
"{}\n{}",
config,
psl::render_datamodel_to_string(&datamodel, Some(ctx.config)),
render::Datamodel::from_dml(&ctx.config.datasources[0], &datamodel),
);

Expand Down Expand Up @@ -273,10 +273,7 @@ fn introspect_models(datamodel: &mut Datamodel, ctx: &Context) {
.foreign_keys()
.filter(|fk| !duplicated_foreign_keys.contains(&fk.id))
{
let mut relation_field = calculate_relation_field(foreign_key, &m2m_tables, &duplicated_foreign_keys);

relation_field.supports_restrict_action(!ctx.sql_family().is_mssql());

let relation_field = calculate_relation_field(ctx, foreign_key, &m2m_tables, &duplicated_foreign_keys);
model.add_field(Field::RelationField(relation_field));
}

Expand All @@ -289,15 +286,23 @@ fn introspect_models(datamodel: &mut Datamodel, ctx: &Context) {
if let Some(pk) = table.primary_key() {
let clustered = primary_key_is_clustered(pk.id, ctx);

let db_name = if pk.name() == ConstraintNames::primary_key_name(table.name(), ctx.active_connector())
|| pk.name().is_empty()
{
None
} else {
Some(pk.name().to_owned())
};

model.primary_key = Some(PrimaryKeyDefinition {
name: None,
db_name: Some(pk.name().to_owned()),
db_name,
fields: pk
.columns()
.map(|c| {
let sort_order = c.sort_order().map(|sort| match sort {
SQLSortOrder::Asc => SortOrder::Asc,
SQLSortOrder::Desc => SortOrder::Desc,
let sort_order = c.sort_order().and_then(|sort| match sort {
SQLSortOrder::Asc => None,
SQLSortOrder::Desc => Some(SortOrder::Desc),
});

PrimaryKeyField {
Expand Down

0 comments on commit 235b53a

Please sign in to comment.