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

fix: re-introspection with @@map and relationMode #3335

Merged
merged 12 commits into from Oct 27, 2022

Conversation

jkomyno
Copy link
Contributor

@jkomyno jkomyno commented Oct 26, 2022

Currently, re-introspection with relationMode = "prisma" or referentialIntegrity = "prisma" loses track of @relations in the schema. This PR fixes that.

Fixes prisma/prisma#11022.

…ionMode/referentialIntegrity and with/without '@@Map'
@jkomyno jkomyno added this to the 4.6.0 milestone Oct 26, 2022
@jkomyno jkomyno marked this pull request as ready for review October 26, 2022 14:42
@jkomyno jkomyno requested a review from a team as a code owner October 26, 2022 14:42
@jkomyno
Copy link
Contributor Author

jkomyno commented Oct 26, 2022

Note: mssql tests are failing with seemingly no diff between the "actual" and "expected" snapshot, I suspect there's a spacing issue somewhere in those files. Is there a quick command I can run to fix that? (UPDATE_EXPECT=1 doesn't do the deed in this case).

Comment on lines 145 to 148
let old_model_name_to_final_database_name: HashMap<String, String> = old_data_model
.models()
.map(|m| (m.name.clone(), String::from(m.final_database_name())))
.collect();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any suggestion on how to avoid these clones while still being able to use the function in the following closures is well accepted.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't worry about it here, but you can change line 147 to .map(|m| (m.name.as_str(), m.final_database_name())) and the type ascription to HashMap<&str, &str>. To optimize further, you could make the HashMap potentially a lot smaller if you .filter() for only the models with a mapped name first — then a model missing in the map would mean the model is not remapped, which would be handled with the existing code lower down.

But we shouldn't worry too much here, we're in the process of rewriting this file and the string comparisons will go away, we'll have a big map of integer identifiers for these lookups, since they happen repeatedly during introspection and reintrospection.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I have simply got rid of .clone() in favor of .as_str(). I will skip other optimizations for now, as we expect this file to change significantly already

@tomhoule
Copy link
Contributor

Note: mssql tests are failing with seemingly no diff between the "actual" and "expected" snapshot, I suspect there's a spacing issue somewhere in those files. Is there a quick command I can run to fix that? (UPDATE_EXPECT=1 doesn't do the deed in this case).

Yes it's probably a whitespace issue — I've never encountered it though so I'm a bit puzzled. Maybe it's due to the extra indentation from the inline modules (mod blocks)?

Copy link
Contributor

@tomhoule tomhoule left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That should fix it 👍

Comment on lines 145 to 148
let old_model_name_to_final_database_name: HashMap<String, String> = old_data_model
.models()
.map(|m| (m.name.clone(), String::from(m.final_database_name())))
.collect();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't worry about it here, but you can change line 147 to .map(|m| (m.name.as_str(), m.final_database_name())) and the type ascription to HashMap<&str, &str>. To optimize further, you could make the HashMap potentially a lot smaller if you .filter() for only the models with a mapped name first — then a model missing in the map would mean the model is not remapped, which would be handled with the existing code lower down.

But we shouldn't worry too much here, we're in the process of rewriting this file and the string comparisons will go away, we'll have a big map of integer identifiers for these lookups, since they happen repeatedly during introspection and reintrospection.

Ok(())
}

// referentialIntegrity = "foreignKeys" with @@map loses track of the relation policy ("foreignKeys"), but preserves @relations, which are moved to the bottom.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering whether we should be testing relationIntegrity = "foreignKeys" more than once, since that's the default everywhere. Let's keep these tests since they're already there.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do you mean, "more than once"? I'm not sure I'm following

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

once would be on just sqlite, or just postgres, for example

@jkomyno jkomyno requested a review from tomhoule October 27, 2022 08:24
@jkomyno jkomyno merged commit f90920b into main Oct 27, 2022
@jkomyno jkomyno deleted the fix/relation-mode-re-introspection-map branch October 27, 2022 09:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Re-Introspection: referentialIntegrity = prisma not respected when using @@map()
2 participants