Skip to content

tests: Add method to compile insert statements - #39

Merged
surister merged 1 commit into
masterfrom
tests/insert_compilation
Dec 18, 2025
Merged

tests: Add method to compile insert statements#39
surister merged 1 commit into
masterfrom
tests/insert_compilation

Conversation

@surister

Copy link
Copy Markdown
Contributor

Summary of the changes / Why this is an improvement

Adds an util function to be able to compile insert statements without hitting the db

@surister
surister requested a review from amotl December 17, 2025 19:06

@amotl amotl left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks. I've added just a few suggestions at your disposal.

Comment thread tests/utils.py Outdated
Comment on lines +85 to +87
if isinstance(compiled, tuple):
return [compiled]
return compiled

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This feels strange, but of course I don't have any clues about the API.

Maybe gradually accompany the code base with type hinting? With the SQLAlchemy dialect, it didn't happen from the beginning (the code base is dating back to Python 2.x), that's why we need to bring it in late:

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

You are right, this is wrong, I wrote that way earlier when I didn't understand the return of .as_sql (django internal code is mostly untyped) and forgot about it.

I've push a fix, thanks for the catch 👌

Comment thread cratedb_django/fields.py
Comment thread tests/utils.py Outdated


class SqlExtractor:
def compile_insert_sql(model, objs, using="default"):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Maybe add as a @staticmethod to the SqlCompiler class?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I feel lukewarm about that

Comment thread tests/utils.py
return compiled


class SqlCompiler:

@amotl amotl Dec 17, 2025

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I don't know much about the Django object model: Is the SqlCompiler only suitable for supporting software tests within this package, or would it also have a purpose to be included into the package itself, to support software testing for all users of cratedb-django?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Even if it's not suitable for production purposes, we could ship it in cratedb_django.testing, in the same spirit like other libraries support downstream testing with concise and practical utilities, e.g. numpy.testing, pandas.testing, etc.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It should only be used for internal testing, users don't typically need to check whether the generated SQL is correct or not, that'd be our job.

To me it's just a nice util for us, it's internal API might change and I'm not even sure it's 100% correct, for example the insert compiler is only partially correct, afterwards django might add extra SQL like returning field1,field2,field3..., that's why I'd rather it to be "private" since I know how it works and where to use it.

Maybe in the future we can expose it but again, I highly doubt it. In the end the goal of using an ORM is not to look at SQL and this would defeat that purpose.

@surister
surister force-pushed the tests/insert_compilation branch from 9fa631b to 14effd5 Compare December 18, 2025 10:09
@surister
surister merged commit d2552e0 into master Dec 18, 2025
2 checks passed
@surister
surister deleted the tests/insert_compilation branch December 18, 2025 10:10
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.

2 participants