Commit eebb6b5e authored by OWAIZ MUSTAFA KHAN's avatar OWAIZ MUSTAFA KHAN 😎

Completed delete_index_not_in_db()

parent 8c663dfa
......@@ -2,14 +2,13 @@
Author: Owaiz Mustafa Khan
Email: owaiz.mustafakhan@rockwellautomation.com
"""
from dataclasses import fields
from pymongo.errors import OperationFailure
from scripts.schemas.mongo_schema import DeleteIndex, UpdateIndexFromDB
from scripts.utils.mongo_utils import get_collection, get_collection_info, get_index_info
from scripts.utils.mongo_utils_v2 import index_exists_v2, make_keys
from scripts.schemas.mongo_schema import UpdateIndexFromDB, DeleteIndexNotInDB
from scripts.utils.mongo_utils import get_collection, get_collection_info
from scripts.utils.mongo_utils_v2 import make_keys, get_index_info
def check_and_create_index(collection_info: dict):
......@@ -42,7 +41,7 @@ def add_new_index(index_info: dict, collection: str, database: str):
background=additional_properties.get('background', False)
)
except OperationFailure as of:
if 'Index with name: email_1 already exists with different options' in str(of):
if 'already exists' in str(of):
print('Cannot add index as it already exists')
return
......@@ -71,53 +70,53 @@ def update_all_index_from_db(payload: UpdateIndexFromDB):
print(f'Exception occurred during update: {e}')
exit(0)
def delete_index(payload: DeleteIndex):
def delete_index_not_in_db(payload: DeleteIndexNotInDB):
try:
i_collection = get_collection(payload.collection_name, payload.db_name)
if not index_exists_v2(payload.model_dump()):
return
collection = get_collection(payload.metadata_collection_name, payload.metadata_db_name)
index_infos = get_index_info(payload.model_dump())
# Deleting metadata from mongo
docs = collection.find({"db": payload.db_name, "collection": payload.collection_name})
for doc in docs:
indexes = doc.get("indexes", [])
new_indexes = []
for index in indexes:
index_type = index.get("type")
keys = index.get("keys")
if isinstance(keys, list):
_ = list()
for key in keys:
_.append(key[0])
metadata_collection = get_collection(
collection_name=payload.collection_name,
db_name=payload.db_name
)
keys = _
collections_info = list(metadata_collection.find({}, {'_id': 0}))
for index_info in index_infos:
if ([keys] if index_type == 'simple' else keys) == index_info.get('fields'):
print(f"Deleting entire document: {doc['_id']}")
collection.delete_one({"_id": doc["_id"]})
break
else:
new_indexes.append(index)
for collection_info in collections_info:
index_infos = get_index_info(collection_info)
collection = get_collection(
collection_info.get(
'collection',
collection_info.get('collection_name')
),
collection_info.get(
'database',
collection_info.get('database_name', collection_info.get('db_name'))
)
)
fields = list()
idx_name = list()
for index_info in index_infos:
if index_info.get('name') == '_id_':
continue
fields.append(index_info.get('fields'))
idx_name.append(index_info.get('name'))
keys = list()
for index in collection_info.get('index'):
key = index.get('keys')
if isinstance(key, str):
keys.append([[key, index.get('additional_properties').get('sort')]])
continue
keys.append(index.get('keys'))
if fields == keys:
continue
for field, name in zip(fields, idx_name):
if field not in keys:
collection.drop_index(name)
else:
# If loop wasn't broken (i.e., not deleted), update or delete based on new index array
if not new_indexes:
collection.delete_one({"_id": doc["_id"]})
else:
collection.update_one(
{"_id": doc["_id"]},
{"$set": {"indexes": new_indexes}}
)
i_collection.drop_index(payload.name)
except Exception as e:
print(f'Some exception occurred while deleting the index: {e}')
......@@ -141,4 +140,7 @@ def delete_index(payload: DeleteIndex):
# ))
update_all_index_from_db(UpdateIndexFromDB(collection_name='index_info_v2'))
\ No newline at end of file
# update_all_index_from_db(UpdateIndexFromDB(collection_name='index_info_v2'))
delete_index_not_in_db(DeleteIndexNotInDB(collection_name='index_info_v2'))
......@@ -10,6 +10,14 @@ from pydantic import BaseModel, Field
class UpdateIndexFromDB(BaseModel):
"""
Pydantic Class For Update Index From Database
Args:
``collection_name`` : [str] The name of collection in which index metadata is present
``db_name`` : [str] The name of database in which the index metadata collection is present
"""
collection_name: str = Field(
default='index_info',
title='Collection Name',
......@@ -19,10 +27,21 @@ class UpdateIndexFromDB(BaseModel):
db_name: str = Field(
default='ilens_default_info',
title='Database Name',
description='The name of database in which the collection is present',
description='The name of database in which the index metadata collection is present',
alias='db_name'
)
class DeleteIndexNotInDB(UpdateIndexFromDB):
"""
Pydantic Class For Delete Index Not Present In Database
Args:
``collection_name`` : [str] The name of collection in which index metadata is present
``db_name`` : [str] The name of database in which the index metadata collection is present
"""
pass
class DeleteIndex(BaseModel):
name: str = Field(
default=None,
......
......@@ -61,8 +61,8 @@ def get_collection_info(index: dict):
db_name = index.get('database', index.get('db', index.get('db_name')))
return collection_name, db_name
def get_index_info(index: dict) -> list[dict]:
collection_name, db_name = get_collection_info(index)
def get_index_info(collection_info: dict) -> list[dict]:
collection_name, db_name = get_collection_info(collection_info)
collection = get_collection(collection_name, db_name)
# Get all index from mongo
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment