Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Comment out fields with empty names #552

Merged
merged 3 commits into from Mar 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -1,4 +1,4 @@
use crate::comment_out_unhandled_models::comment_out_unhandled_models;
use crate::commenting_out_guardrails::commenting_out_guardrails;
use crate::misc_helpers::*;
use crate::sanitize_datamodel_names::sanitize_datamodel_names;
use crate::SqlIntrospectionResult;
Expand Down Expand Up @@ -141,8 +141,8 @@ pub fn calculate_model(schema: &SqlSchema) -> SqlIntrospectionResult<Datamodel>
model.add_field(field);
}

comment_out_unhandled_models(&mut data_model);
sanitize_datamodel_names(&mut data_model);
commenting_out_guardrails(&mut data_model);
debug!("Done calculating data model {:?}", data_model);

Ok(data_model)
Expand Down
@@ -1,8 +1,9 @@
use datamodel::Datamodel;

pub fn comment_out_unhandled_models(datamodel: &mut Datamodel) {
pub fn commenting_out_guardrails(datamodel: &mut Datamodel) {
let mut commented_model_names = vec![];

//models without uniques / ids
for model in &mut datamodel.models {
if model.id_fields.is_empty()
&& !model.fields.iter().any(|f| f.is_id || f.is_unique)
Expand All @@ -17,6 +18,20 @@ pub fn comment_out_unhandled_models(datamodel: &mut Datamodel) {
}
}

//fields with an empty name
for model in &mut datamodel.models {
for field in &mut model.fields {
if field.name == "".to_string() {
do4gr marked this conversation as resolved.
Show resolved Hide resolved
field.documentation = Some(
"This field was commented out because of an invalid name. Please provide a valid one that matches [a-zA-Z][a-zA-Z0-9_]*"
.to_string(),
);
field.name = field.database_names.first().unwrap().to_string();
field.is_commented_out = true;
}
}
}

for name in &commented_model_names {
for model in &mut datamodel.models {
model.fields.retain(|f| !f.points_to_model(name));
Expand Down
@@ -1,5 +1,5 @@
pub mod calculate_datamodel; // only exported to be able to unit test it
mod comment_out_unhandled_models;
mod commenting_out_guardrails;
mod error;
mod misc_helpers;
mod sanitize_datamodel_names;
Expand Down
Expand Up @@ -71,6 +71,7 @@ pub fn calculate_many_to_many_field(foreign_key: &ForeignKey, relation_name: Str
is_generated: false,
is_updated_at: false,
data_source_fields: vec![],
is_commented_out: false,
}
}

Expand Down Expand Up @@ -128,6 +129,7 @@ pub(crate) fn calculate_scalar_field(
is_generated: false,
is_updated_at: false,
data_source_fields: vec![],
is_commented_out: false,
}
}

Expand Down Expand Up @@ -218,6 +220,7 @@ pub(crate) fn calculate_relation_field(
is_generated: false,
is_updated_at: false,
data_source_fields: vec![],
is_commented_out: false,
}]
}
}
Expand Down Expand Up @@ -280,6 +283,7 @@ pub(crate) fn calculate_backrelation_field(
is_generated: false,
is_updated_at: false,
data_source_fields: vec![],
is_commented_out: false,
}
}

Expand Down
Expand Up @@ -62,6 +62,7 @@ fn a_data_model_can_be_generated_from_a_schema() {
is_generated: false,
is_updated_at: false,
data_source_fields: vec![],
is_commented_out: false,
}
})
.collect(),
Expand Down Expand Up @@ -122,6 +123,7 @@ fn arity_is_preserved_when_generating_data_model_from_a_schema() {
is_generated: false,
is_updated_at: false,
data_source_fields: vec![],
is_commented_out: false,
},
Field {
name: "required".to_string(),
Expand All @@ -135,6 +137,7 @@ fn arity_is_preserved_when_generating_data_model_from_a_schema() {
is_generated: false,
is_updated_at: false,
data_source_fields: vec![],
is_commented_out: false,
},
Field {
name: "list".to_string(),
Expand All @@ -148,6 +151,7 @@ fn arity_is_preserved_when_generating_data_model_from_a_schema() {
is_generated: false,
is_updated_at: false,
data_source_fields: vec![],
is_commented_out: false,
},
],
is_generated: false,
Expand Down Expand Up @@ -229,6 +233,7 @@ fn defaults_are_preserved_when_generating_data_model_from_a_schema() {
is_generated: false,
is_updated_at: false,
data_source_fields: vec![],
is_commented_out: false,
},
Field {
name: "int_default".to_string(),
Expand All @@ -242,6 +247,7 @@ fn defaults_are_preserved_when_generating_data_model_from_a_schema() {
is_generated: false,
is_updated_at: false,
data_source_fields: vec![],
is_commented_out: false,
},
Field {
name: "bool_default".to_string(),
Expand All @@ -255,6 +261,7 @@ fn defaults_are_preserved_when_generating_data_model_from_a_schema() {
is_generated: false,
is_updated_at: false,
data_source_fields: vec![],
is_commented_out: false,
},
Field {
name: "float_default".to_string(),
Expand All @@ -268,6 +275,7 @@ fn defaults_are_preserved_when_generating_data_model_from_a_schema() {
is_generated: false,
is_updated_at: false,
data_source_fields: vec![],
is_commented_out: false,
},
Field {
name: "string_default".to_string(),
Expand All @@ -281,6 +289,7 @@ fn defaults_are_preserved_when_generating_data_model_from_a_schema() {
is_generated: false,
is_updated_at: false,
data_source_fields: vec![],
is_commented_out: false,
},
],
is_generated: false,
Expand Down Expand Up @@ -388,6 +397,7 @@ fn primary_key_is_preserved_when_generating_data_model_from_a_schema() {
is_generated: false,
is_updated_at: false,
data_source_fields: vec![],
is_commented_out: false,
}],
is_generated: false,
indices: vec![],
Expand All @@ -412,6 +422,7 @@ fn primary_key_is_preserved_when_generating_data_model_from_a_schema() {
is_generated: false,
is_updated_at: false,
data_source_fields: vec![],
is_commented_out: false,
}],
is_generated: false,
indices: vec![],
Expand All @@ -436,6 +447,7 @@ fn primary_key_is_preserved_when_generating_data_model_from_a_schema() {
is_generated: false,
is_updated_at: false,
data_source_fields: vec![],
is_commented_out: false,
}],
is_generated: false,
indices: vec![],
Expand Down Expand Up @@ -539,6 +551,7 @@ fn uniqueness_is_preserved_when_generating_data_model_from_a_schema() {
is_generated: false,
is_updated_at: false,
data_source_fields: vec![],
is_commented_out: false,
},
Field {
name: "unique".to_string(),
Expand All @@ -552,6 +565,7 @@ fn uniqueness_is_preserved_when_generating_data_model_from_a_schema() {
is_generated: false,
is_updated_at: false,
data_source_fields: vec![],
is_commented_out: false,
},
],
is_generated: false,
Expand Down Expand Up @@ -626,6 +640,7 @@ fn compound_foreign_keys_are_preserved_when_generating_data_model_from_a_schema(
is_generated: false,
is_updated_at: false,
data_source_fields: vec![],
is_commented_out: false,
},
Field {
name: "name".to_string(),
Expand All @@ -639,6 +654,7 @@ fn compound_foreign_keys_are_preserved_when_generating_data_model_from_a_schema(
is_generated: false,
is_updated_at: false,
data_source_fields: vec![],
is_commented_out: false,
},
],
is_generated: false,
Expand All @@ -664,6 +680,7 @@ fn compound_foreign_keys_are_preserved_when_generating_data_model_from_a_schema(
is_generated: false,
is_updated_at: false,
data_source_fields: vec![],
is_commented_out: false,
},
Field {
name: "city-id".to_string(),
Expand All @@ -682,6 +699,7 @@ fn compound_foreign_keys_are_preserved_when_generating_data_model_from_a_schema(
is_generated: false,
is_updated_at: false,
data_source_fields: vec![],
is_commented_out: false,
},
Field {
name: "city-name".to_string(),
Expand All @@ -700,6 +718,7 @@ fn compound_foreign_keys_are_preserved_when_generating_data_model_from_a_schema(
is_generated: false,
is_updated_at: false,
data_source_fields: vec![],
is_commented_out: false,
},
],
is_generated: false,
Expand Down Expand Up @@ -819,6 +838,7 @@ fn multi_field_uniques_are_preserved_when_generating_data_model_from_a_schema()
is_generated: false,
is_updated_at: false,
data_source_fields: vec![],
is_commented_out: false,
},
Field {
name: "name".to_string(),
Expand All @@ -832,6 +852,7 @@ fn multi_field_uniques_are_preserved_when_generating_data_model_from_a_schema()
is_generated: false,
is_updated_at: false,
data_source_fields: vec![],
is_commented_out: false,
},
Field {
name: "lastname".to_string(),
Expand All @@ -845,6 +866,7 @@ fn multi_field_uniques_are_preserved_when_generating_data_model_from_a_schema()
is_generated: false,
is_updated_at: false,
data_source_fields: vec![],
is_commented_out: false,
},
],
is_generated: false,
Expand Down Expand Up @@ -935,6 +957,7 @@ fn foreign_keys_are_preserved_when_generating_data_model_from_a_schema() {
is_generated: false,
is_updated_at: false,
data_source_fields: vec![],
is_commented_out: false,
},
Field {
name: "name".to_string(),
Expand All @@ -948,6 +971,7 @@ fn foreign_keys_are_preserved_when_generating_data_model_from_a_schema() {
is_generated: false,
is_updated_at: false,
data_source_fields: vec![],
is_commented_out: false,
},
Field {
name: "user".to_string(),
Expand All @@ -966,6 +990,7 @@ fn foreign_keys_are_preserved_when_generating_data_model_from_a_schema() {
is_generated: false,
is_updated_at: false,
data_source_fields: vec![],
is_commented_out: false,
},
],
is_generated: false,
Expand All @@ -991,6 +1016,7 @@ fn foreign_keys_are_preserved_when_generating_data_model_from_a_schema() {
is_generated: false,
is_updated_at: false,
data_source_fields: vec![],
is_commented_out: false,
},
Field {
name: "city_id".to_string(),
Expand All @@ -1009,6 +1035,7 @@ fn foreign_keys_are_preserved_when_generating_data_model_from_a_schema() {
is_generated: false,
is_updated_at: false,
data_source_fields: vec![],
is_commented_out: false,
},
],
is_generated: false,
Expand Down
Expand Up @@ -305,3 +305,20 @@ async fn remapping_enum_default_values_should_work(api: &TestApi) {
let result = dbg!(api.introspect().await);
custom_assert(&result, dm);
}

#[test_each_connector(tags("postgres"))]
async fn remapping_field_names_to_empty_should_comment_them_out(api: &TestApi) {
api.barrel()
.execute(|migration| {
migration.create_table("User", |t| {
t.add_column("1", types::text());
t.add_column("last", types::primary());
});
})
.await;

let dm = "model User {\n /// This field was commented out because of an invalid name. Please provide a valid one that matches [a-zA-Z][a-zA-Z0-9_]*\n // 1 String @map(\"1\")\n last Int @default(autoincrement()) @id\n}";

let result = dbg!(api.introspect().await);
assert_eq!(&result, dm);
}
2 changes: 2 additions & 0 deletions libs/datamodel/core/src/ast/field.rs
Expand Up @@ -16,6 +16,8 @@ pub struct Field {
pub documentation: Option<Comment>,
/// The location of this field in the text representation.
pub span: Span,
/// The location of this field in the text representation.
pub is_commented_out: bool,
}

impl WithIdentifier for Field {
Expand Down
2 changes: 2 additions & 0 deletions libs/datamodel/core/src/ast/parser/mod.rs
Expand Up @@ -228,6 +228,7 @@ fn parse_field(token: &pest::iterators::Pair<'_, Rule>) -> Result<Field, Datamod
directives,
documentation: doc_comments_to_string(&comments),
span: Span::from_pest(token.as_span()),
is_commented_out: false,
}),
_ => panic!(
"Encountered impossible field declaration during parsing: {:?}",
Expand Down Expand Up @@ -453,6 +454,7 @@ fn parse_type(token: &pest::iterators::Pair<'_, Rule>) -> Field {
directives,
documentation: doc_comments_to_string(&comments),
span: Span::from_pest(token.as_span()),
is_commented_out: false,
},
_ => panic!(
"Encountered impossible custom type declaration during parsing: {:?}",
Expand Down
10 changes: 8 additions & 2 deletions libs/datamodel/core/src/ast/renderer/mod.rs
Expand Up @@ -176,7 +176,7 @@ impl<'a> Renderer<'a> {
let mut field_formatter = TableFormat::new();

for field in &model.fields {
Self::render_field(&mut field_formatter, &field, comment_out.clone());
Self::render_field(&mut field_formatter, &field, model.commented_out);
}

field_formatter.render(self);
Expand Down Expand Up @@ -231,9 +231,15 @@ impl<'a> Renderer<'a> {
self.end_line();
}

fn render_field(target: &mut TableFormat, field: &ast::Field, commented_out: String) {
fn render_field(target: &mut TableFormat, field: &ast::Field, is_commented_out: bool) {
Self::render_documentation(&mut target.interleave_writer(), field);

let commented_out = if field.is_commented_out || is_commented_out {
"// ".to_string()
} else {
"".to_string()
};

target.write(format!("{}{}", &commented_out, &field.name.name).as_ref());

// Type
Expand Down