Skip to content

Commit

Permalink
fixed migration script #338
Browse files Browse the repository at this point in the history
  • Loading branch information
asuresh-code committed Oct 17, 2024
1 parent f4e5b5e commit 040bbab
Showing 1 changed file with 13 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ class NewCatalogueItemBase(BaseModel):
notes: Optional[str] = None
properties: List[PropertyIn] = []

# pylint: disable=duplicate-code
@field_validator("properties", mode="before")
@classmethod
def validate_properties(cls, properties: Any) -> Any:
Expand All @@ -57,8 +56,6 @@ def validate_properties(cls, properties: Any) -> Any:
properties = []
return properties

# pylint: enable=duplicate-code

@field_serializer("drawing_link")
def serialize_url(self, url: HttpUrl):
"""
Expand Down Expand Up @@ -97,7 +94,6 @@ class OldCatalogueItemBase(BaseModel):
notes: Optional[str] = None
properties: List[PropertyIn] = []

# pylint: disable=duplicate-code
@field_validator("properties", mode="before")
@classmethod
def validate_properties(cls, properties: Any) -> Any:
Expand All @@ -113,8 +109,6 @@ def validate_properties(cls, properties: Any) -> Any:
properties = []
return properties

# pylint: enable=duplicate-code

@field_serializer("drawing_link")
def serialize_url(self, url: HttpUrl):
"""
Expand Down Expand Up @@ -161,16 +155,25 @@ def forward(self, session: ClientSession):
logger.info("expected_lifetime forward migration")
for catalogue_item in catalogue_items:
try:
old_catalogue_item = OldCatalogueItemOut(**catalogue_item.model_dump())
old_catalogue_item = OldCatalogueItemOut(**catalogue_item)

new_catalogue_item = NewCatalogueItemIn(**old_catalogue_item.model_dump())

update_data = {**new_catalogue_item.model_dump(), "modified_time": old_catalogue_item.modified_time}
logger.info(new_catalogue_item.model_dump())

self._catalogue_items_collection.update_one(
{"_id": old_catalogue_item.id}, {"$set": update_data}, session=session
update_data = {
**new_catalogue_item.model_dump(),
"modified_time": old_catalogue_item.modified_time,
}

result = self._catalogue_items_collection.replace_one(
{"_id": catalogue_item["_id"]},
update_data,
session=session,
)

logger.info(result)

except ValidationError as ve:
logger.error("Validation failed for item with id %s: %s", catalogue_item["_id"], ve)

Expand Down

0 comments on commit 040bbab

Please sign in to comment.