Skip to content

Commit

Permalink
[WIP] IE Changes (#610)
Browse files Browse the repository at this point in the history
* switch IE to new relation logic

* new test cases and clean ups
  • Loading branch information
do4gr committed Mar 24, 2020
1 parent 76127e0 commit 1e88273
Show file tree
Hide file tree
Showing 18 changed files with 1,082 additions and 893 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::commenting_out_guardrails::commenting_out_guardrails;
use crate::misc_helpers::columns_match;
use crate::misc_helpers::*;
use crate::sanitize_datamodel_names::sanitize_datamodel_names;
use crate::SqlIntrospectionResult;
Expand All @@ -23,53 +22,21 @@ pub fn calculate_model(schema: &SqlSchema) -> SqlIntrospectionResult<Datamodel>
debug!("Calculating model: {}", table.name);
let mut model = Model::new(table.name.clone(), None);

for column in table
.columns
.iter()
.filter(|column| !is_foreign_key_column(&table, &column))
{
let field = calculate_scalar_field(&schema, &table, &column);
for column in &table.columns {
let field = calculate_scalar_field(&table, &column);
model.add_field(field);
}

for foreign_key in &table.foreign_keys {
model.add_field(calculate_relation_field(
schema,
table,
foreign_key,
&table.foreign_keys,
));
model.add_field(calculate_relation_field(schema, table, foreign_key));
}

for index in &table.indices {
let fk_on_index = table
.foreign_keys
.iter()
.find(|fk| columns_match(&fk.columns, &index.columns));

let compound_name = || {
model
.fields
.iter()
.find(|f| {
!f.database_names.is_empty()
&& columns_match(&f.database_names, &index.columns)
})
.expect("Error finding field matching a compound index.")
.name
.clone()
};

let index_to_add = match (fk_on_index, index.columns.len(), index.is_unique()) {
(Some(_), _, true) => None, // just make the relation 1:1 and dont print the unique index
(Some(_), 1, false) => Some(calculate_index(index)),
(Some(_), _, false) => Some(calculate_compound_index(index, compound_name())),
(None, 1, true) => None, // this is expressed by the @unique already
(None, _, true) => Some(calculate_index(index)),
(None, _, false) => Some(calculate_index(index)),
};

index_to_add.map(|i| model.add_index(i));
for index in table
.indices
.iter()
.filter(|i| !(i.columns.len() == 1 && i.is_unique()))
{
model.add_index(calculate_index(index));
}

if table.primary_key_columns().len() > 1 {
Expand Down Expand Up @@ -140,15 +107,17 @@ pub fn calculate_model(schema: &SqlSchema) -> SqlIntrospectionResult<Datamodel>
}
}

deduplicate_names_of_fields_to_be_added(&mut fields_to_be_added);

for (model, field) in fields_to_be_added {
let model = data_model.find_model_mut(&model).unwrap();
model.add_field(field);
}

//todo sanitizing might need to be adjusted to also change the fields in the RelationInfo
sanitize_datamodel_names(&mut data_model); //todo warnings
commenting_out_guardrails(&mut data_model); //todo warnings

deduplicate_field_names(&mut data_model);

debug!("Done calculating data model {:?}", data_model);

Ok(data_model)
Expand Down
Loading

0 comments on commit 1e88273

Please sign in to comment.