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

[WIP] IE Changes #610

Merged
merged 7 commits into from Mar 24, 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,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