This repository was archived by the owner on Aug 19, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 96
add support for bulk_update #148
Open
Mng-dev-ai
wants to merge
7
commits into
encode:master
Choose a base branch
from
Mng-dev-ai:add-support-for-bulk-update
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
import enum | ||
import json | ||
import typing | ||
|
||
import databases | ||
|
@@ -20,6 +22,8 @@ | |
"lte": "__le__", | ||
} | ||
|
||
MODEL = typing.TypeVar("MODEL", bound="Model") | ||
|
||
|
||
def _update_auto_now_fields(values, fields): | ||
for key, value in fields.items(): | ||
|
@@ -28,6 +32,15 @@ def _update_auto_now_fields(values, fields): | |
return values | ||
|
||
|
||
def _convert_value(value): | ||
if isinstance(value, dict): | ||
return json.dumps(value) | ||
elif isinstance(value, enum.Enum): | ||
return value.name | ||
else: | ||
return value | ||
|
||
|
||
class ModelRegistry: | ||
def __init__(self, database: databases.Database) -> None: | ||
self.database = database | ||
|
@@ -454,6 +467,43 @@ async def update(self, **kwargs) -> None: | |
|
||
await self.database.execute(expr) | ||
|
||
async def bulk_update( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry I should've noticed this earlier, apologies for that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah I agree with you it needs to be more readable There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @aminalaee Any updates ? |
||
self, objs: typing.List[MODEL], fields: typing.List[str] | ||
) -> None: | ||
fields = { | ||
key: field.validator | ||
for key, field in self.model_cls.fields.items() | ||
if key in fields | ||
} | ||
validator = typesystem.Schema(fields=fields) | ||
new_objs = [ | ||
_update_auto_now_fields(validator.validate(value), self.model_cls.fields) | ||
for value in [ | ||
{ | ||
key: _convert_value(value) | ||
for key, value in obj.__dict__.items() | ||
if key in fields | ||
} | ||
for obj in objs | ||
] | ||
] | ||
expr = ( | ||
self.table.update() | ||
.where( | ||
getattr(self.table.c, self.pkname) == sqlalchemy.bindparam(self.pkname) | ||
) | ||
.values( | ||
{ | ||
field: sqlalchemy.bindparam(field) | ||
for obj in new_objs | ||
for field in obj.keys() | ||
} | ||
) | ||
) | ||
pk_list = [{self.pkname: getattr(obj, self.pkname)} for obj in objs] | ||
joined_list = [{**pk, **value} for pk, value in zip(pk_list, new_objs)] | ||
await self.database.execute_many(str(expr), joined_list) | ||
|
||
async def get_or_create( | ||
self, defaults: typing.Dict[str, typing.Any], **kwargs | ||
) -> typing.Tuple[typing.Any, bool]: | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -278,3 +278,22 @@ async def test_nullable_foreign_key(): | |
|
||
assert member.email == "[email protected]" | ||
assert member.team.pk is None | ||
|
||
|
||
async def test_bulk_update_with_relation(): | ||
album = await Album.objects.create(name="foo") | ||
album2 = await Album.objects.create(name="bar") | ||
|
||
await Track.objects.bulk_create( | ||
[ | ||
{"name": "foo", "album": album, "position": 1, "title": "foo"}, | ||
{"name": "bar", "album": album, "position": 2, "title": "bar"}, | ||
] | ||
) | ||
tracks = await Track.objects.all() | ||
for track in tracks: | ||
track.album = album2 | ||
await Track.objects.bulk_update(tracks, fields=["album"]) | ||
tracks = await Track.objects.all() | ||
assert tracks[0].album.pk == album2.pk | ||
assert tracks[1].album.pk == album2.pk |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
What value does this bring? I mean we could call bulk_update with
Model
itself. right?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.
What do you mean? We use it as a type annotation for obj
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 I understand. I mean you've done this:
What would be the difference if we did:
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 this case Model will be undefined because it has been defined after bulk_update
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.
Maybe "Model" ?