-
Notifications
You must be signed in to change notification settings - Fork 10
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
Initial Version of SQL Parameterizer Codemod #90
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is some really impressive work overall. I'm relying heavily on the test cases to help me understand the feature. Most of my comments are minor in themselves but I think they all add up to a requested change.
# research how to detect attribute assigns in libcst | ||
return [node] | ||
|
||
def _find_gparent(self, n: cst.CSTNode) -> Optional[cst.CSTNode]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have some similar logic in use-walrus-if
:
codemodder-python/src/core_codemods/use_walrus_if.py
Lines 93 to 95 in 7112886
if parent := self.get_metadata(ParentNodeProvider, node): | |
if gparent := self.get_metadata(ParentNodeProvider, parent): | |
if (idx := gparent.children.index(parent)) >= 0: |
It might be useful to generalize this into a separate utility function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could go the way of NameResolutionMixin
and make something like ParentProviderMixin
, but this feels like a small enough operation that it does not warrant a whole class for it. Besides I can't think of another operation that would go into that class and be used in other codemods for now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking something more like a simple function/method that returned a grandparent node but it's not that important.
return updated_node | ||
|
||
|
||
class SQLQueryParameterization(BaseCodemod, Codemod): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this ends up being cleaner if we use codemodder.api.BaseCodemod
as the base here instead. It requires a few tweaks to the metadata fields.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I want it to inherit from libcst's Codemod
and not BaseTransformer
-> VisitorBasedCodemodCommand
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah and as long as we inherit from libcst's Codemod
we can't use our current API. We'll have to create api base classes for this later on
612aa3b
to
27e846b
Compare
Codecov Report
@@ Coverage Diff @@
## main #90 +/- ##
==========================================
- Coverage 95.72% 95.18% -0.55%
==========================================
Files 49 51 +2
Lines 2012 2284 +272
==========================================
+ Hits 1926 2174 +248
- Misses 86 110 +24
|
The 100% coverage here is not happening unless I remove the formatted string placeholders and some sanity checks (e.g. the exception in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good except for two minor comments that need to be addressed.
tests/samples/my_db.db
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this was intended to be committed. Can you add a pattern to .gitignore
so we avoid this in the future?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need this to run the integration test without issues.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In general I'd like to avoid committing binary artifacts like this to the source tree. I don't want to hold this PR up any longer but eventually we should create this database as part of the test setup and then remove it at teardown.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just learned that you can create a memory-only database with the ":memory:" argument in sqlite3
. I'll adjust the integration test and remove the extra file.
27e846b
to
53425cb
Compare
Added a couple unit tests
Added separated tests
6b6739c
to
ce44d62
Compare
Overview
Adds a codemod that parameterizes SQL queries.