-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BackendIssue1031 Implement RemoveFieldMigration (#160)
Co-authored-by: Ralf Peschke <rpeschke@peschke-it.de>
- Loading branch information
Showing
4 changed files
with
105 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from typing import Any, Dict, List, Optional | ||
|
||
from datastore.shared.util import collection_from_fqid | ||
|
||
from .. import ( | ||
BaseEvent, | ||
BaseMigration, | ||
CreateEvent, | ||
DeleteFieldsEvent, | ||
ListUpdateEvent, | ||
UpdateEvent, | ||
) | ||
|
||
|
||
class RemoveFieldMigration(BaseMigration): | ||
""" | ||
This migration removes a field from all events for one collection. | ||
""" | ||
|
||
collection: str | ||
field: str | ||
|
||
def remove_field(self, object: Dict[str, Any]) -> None: | ||
if self.field in object: | ||
del object[self.field] | ||
|
||
def migrate_event( | ||
self, | ||
event: BaseEvent, | ||
) -> Optional[List[BaseEvent]]: | ||
collection = collection_from_fqid(event.fqid) | ||
if collection != self.collection: | ||
return None | ||
|
||
if isinstance(event, CreateEvent) or isinstance(event, UpdateEvent): | ||
self.remove_field(event.data) | ||
|
||
elif isinstance(event, DeleteFieldsEvent): | ||
if self.field in event.data: | ||
event.data.remove(self.field) | ||
|
||
elif isinstance(event, ListUpdateEvent): | ||
self.remove_field(event.add) | ||
self.remove_field(event.remove) | ||
|
||
return [event] |
This file contains 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 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