-
-
Notifications
You must be signed in to change notification settings - Fork 400
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
A unified, robust and bug-free connection management interface for the ORM #1001
Conversation
Hi! Thank you for your PR Could you provide motivation for this piece?
When I was designing this one on the start I did that so that tortoise could fail fast, and user of orm would know that something gone wrong from the start With lazy connection that could be delayed into future, which could bring more confusion |
Hey @abondar Thanks for responding quickly. While I do agree to your point (failing fast), I see the following concerns which might take higher precedence over this:
Also, most ORMs' including Django and SQLAlchemy have resorted to lazy connection creation for probably the same reasons. Let me know your thoughts on the same. Thanks again 😊 |
Hey @abondar , is there any update on this or are we blocked somewhere ? Any concerns ? |
Hey @abondar , any thoughts/updates on this ? I'd really like this to get merged so that I'd be able to use the ORM with so much more confidence in both my personal as well as professional python projects. Also, I've fixed breaking tests as well. It would be great if either you or someone from the core team can take a look at this so that we can move forward. |
# Conflicts: # tortoise/backends/base/client.py
Pull Request Test Coverage Report for Build 2002631079
💛 - Coveralls |
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.
Hi!
PR seems to be legit to me
Sorry for slow responses, even though project is being maintained, it is done on available free time basis, which could be quite limited at times
@long2ice could you please prepare release for this PR?
Thanks for your contribution! @abondar I will spend some time to review it. Which is a big change and break some api, I have some concerns about that and considering whether we need to accept these changes. And also please see https://github.com/tortoise/tortoise-orm/pull/1001/checks?check_run_id=4581321590 to make that pass. |
Hey @abondar @long2ice , thanks for the response. I do understand that most of the work done is during free time. Its just that I found myself some time to work on this and thought that it would be great if merged quickly so that I could recommend using the ORM at my workplace for a few projects. Personally, from the research I've done, I feel that this project has an edge over other async ORMs' out there in the market with respect to feature set, performance and ease-of-use (especially for Django buffs). Hoping to become a long term contributor in the near future. Also, @long2ice I've made sure that all APIs' that currently exist for fetching DB connections have been made backward compatible with the new design and so this wouldn't cause breaking changes off the bat. These APIs' can be marked as deprecated and probably removed in a future release. Let me know your thoughts. |
# Conflicts: # tests/model_setup/test__models__.py # tests/model_setup/test_init.py # tests/schema/test_generate_schema.py # tests/utils/test_run_async.py # tortoise/contrib/test/__init__.py
# Conflicts: # tests/schema/test_generate_schema.py # tortoise/backends/asyncpg/client.py
@long2ice I've fixed all breaking tests from my end. Also, I've removed the custom |
@long2ice Also, please trigger the pipeline again |
Looks great! Just need add the changelog |
# Conflicts: # CHANGELOG.rst
@long2ice Hey, I've updated the changelog as well. Please do have a look at it. I think it can be merged now. Let me knw if there's anything else that needs to be done. Thanks 🙂 |
@long2ice Hey I think test_bulk_create_mix_specified test is failing since the resulting list from the queryset is not sorted but assertListSortEqual is expecting it to be sorted... Could you please fix this from ur end ? |
@long2ice Also this is similar to the bulk issue which was fixed earlier I believe |
@long2ice Either you could add an |
What about merge develop first? |
@long2ice I've already merged from develop. But the issue still remains... |
async def test_bulk_create_mix_specified(self):
await UniqueName.bulk_create(
[UniqueName(id=id_) for id_ in range(10000, 11000)]
+ [UniqueName() for _ in range(1000)]
)
all_ = await UniqueName.all().values("id", "name") # --> This is gonna return results in a random order
self.assertEqual(len(all_), 2000)
self.assertListSortEqual(
all_[:1000], [{"id": id_, "name": None} for id_ in range(10000, 11000)], sorted_key="id"
) # --> This expectation is gonna fail
inc = all_[1000]["id"]
self.assertListSortEqual(
all_[1000:], [{"id": val + inc, "name": None} for val in range(1000)], sorted_key="id"
) The bottomline is that sometimes the test passes and sometimes it fails becoz of the inconsistency I've highlighted above... |
strange, ci of develop is right |
@long2ice Not really sure but from what I observed the tests seem to pass sometimes and fail during others. Nevertheless could you pls check the specific concern I've highlighted above? What's your take on it? |
@long2ice Also, I'd suggest triggering the pipeline again... If it succeeds then it could be a one off inconsistency which we could potentially ignore. If not, it has to be dealt with. Either which way, triggering the pipeline again could add some clarity to the matter. |
@long2ice I guess my observation is true. Since the pipeline is passing, I believe we can proceed to merge it and then probably look into the inconsistency issue. If not, I believe the merge is getting unnecessarily delayed... Let me knw ur thoughts |
OK, I merge it. Thanks! |
@long2ice thanks! Also, do you plan on creating a separate issue for the inconsistency which I've mentioned above? If so, let me knw. Would be happy to contribute there as well... Also would be looking to solve other issues related to connection management and related behavior. |
0.20.0 ------ Added ^^^^^ - Allow ForeignKeyField(on_delete=NO_ACTION) - Support `pydantic` 2.0. Fixed ^^^^^ - Fix foreign key constraint not generated on MSSQL Server. - Fix testcase error with python3.11 Breaking Changes ^^^^^^^^^^^^^^^^ - Drop support for `pydantic` 1.x. - Drop support for `python` 3.7. - Param `config_class` of `pydantic_model_creator` is renamed to `model_config`. - Attr `config_class` of `PydanticMeta` is renamed to `model_config`. 0.19 ==== 0.19.3 ------ Added ^^^^^ - Added config_class option to pydantic model genator that allows the developer to customize the generated pydantic model's `Config` class. Fixed ^^^^^ - Fastapi example test not working. - Fix create index sql error. - Fix dependencies resolve error. - Fix ignoring zero value of limit. - Fix ForeignKeyField is none when fk is integer 0. - Fix limit ignore zero. - Fix min/max value validators for decimal fields. 0.19.2 ------ Added ^^^^^ - Added `schema` attribute to Model's Meta to specify exact schema to use with the model. Fixed ^^^^^ - Mixin does not work. - `using_db` wrong position in model shortcut methods. - Fixed connection to `Oracle` database by adding database info to DBQ in connection string. - Fixed ORA-01435 error while using `Oracle` database - Fixed processing of `ssl` option in MySQL connection string. - Fixed type hinting for `QuerySetSingle`. 0.19.1 ------ Added ^^^^^ - Added `Postgres`/`SQLite` partial indexes support. - Added `Microsoft SQL Server`/`Oracle` support, powered by `asyncodbc <https://github.com/tortoise/asyncodbc>`_, note that which is **not fully tested**. - Added `optional` parameter to `pydantic_model_creator`. - Added `using_db` parameter to `Model` shortcut methods. Fixed ^^^^^ - `TimeField` for `MySQL` will return `datetime.timedelta` object instead of `datetime.time` object. - Fix on conflict do nothing. - Fix `_custom_generated_pk` attribute not set in `Model._init_from_db` method. 0.19.0 ------ Added ^^^^^ - Added psycopg backend support. - Added a new unified and robust connection management interface to access DB connections which includes support for lazy connection creation and much more. For more details, check out this `PR <https://github.com/tortoise/tortoise-orm/pull/1001>`_ - Added `TimeField`. - Added `ArrayField`. Fixed ^^^^^ - Fix `bulk_create` doesn't work correctly with more than 1 update_fields. - Fix `bulk_update` errors when setting null for a smallint column on postgres. Deprecated ^^^^^^^^^^ - Existing connection management interface and related public APIs which are deprecated: - `Tortoise.get_connection` - `Tortoise.close_connections` Changed ^^^^^^^ - Refactored `tortoise.transactions.get_connection` method to `tortoise.transactions._get_connection`. Note that this method has now been marked **private to this module and is not part of the public API**
Description
This PR provides for a much more robust implementation of the connection management interface of the ORM which is primarily geared towards improving performance and usability.
Motivation and Context
I was really excited that a native asyncIO based ORM was in town and implemented a lot of API constructs similar to the Django ORM since I personally like the way queries are expressed in Django. When I was tinkering around,
Tortoise.get_connection
returns a connection for a given DB alias, there was another construct namedcurrent_transaction_map
and another method namedget_connection
intortoise/transactions.py
that was being used internally for storing and retrieving connections.This PR tries to address the above problems/inconsistencies as follows:
asyncio
native constructs.Tortoise._connections
andcurrent_transaction_map
). Also, the usage of ContextVars has been done in the most optimal way.How Has This Been Tested?
Once the solution has been accepted and reviewed by the core team, I can update the tests, docs and changlog accordingly.
Checklist: