Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
I
index_info_script
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
CI / CD Analytics
Repository Analytics
Value Stream Analytics
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
tarun.madamanchi
index_info_script
Commits
eebb6b5e
Commit
eebb6b5e
authored
Apr 23, 2025
by
OWAIZ MUSTAFA KHAN
😎
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Completed delete_index_not_in_db()
parent
8c663dfa
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
73 additions
and
52 deletions
+73
-52
index_operations_v2.py
index_operations_v2.py
+51
-49
scripts/schemas/__pycache__/mongo_schema.cpython-311.pyc
scripts/schemas/__pycache__/mongo_schema.cpython-311.pyc
+0
-0
scripts/schemas/mongo_schema.py
scripts/schemas/mongo_schema.py
+20
-1
scripts/utils/__pycache__/mongo_utils_v2.cpython-311.pyc
scripts/utils/__pycache__/mongo_utils_v2.cpython-311.pyc
+0
-0
scripts/utils/mongo_utils_v2.py
scripts/utils/mongo_utils_v2.py
+2
-2
No files found.
index_operations_v2.py
View file @
eebb6b5e
...
...
@@ -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
,
UpdateIndexFrom
DB
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
,
DeleteIndexNotIn
DB
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 option
s'
in
str
(
of
):
if
'
already exist
s'
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
)
metadata_collection
=
get_collection
(
collection_name
=
payload
.
collection_name
,
db_name
=
payload
.
db_name
)
index_infos
=
get_index_info
(
payload
.
model_dump
(
))
collections_info
=
list
(
metadata_collection
.
find
({},
{
'_id'
:
0
}
))
# Deleting metadata from mongo
docs
=
collection
.
find
({
"db"
:
payload
.
db_name
,
"collection"
:
payload
.
collection_name
})
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'
))
)
)
for
doc
in
docs
:
indexes
=
doc
.
get
(
"indexes"
,
[])
new_indexes
=
[]
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'
))
for
index
in
indexes
:
index_type
=
index
.
get
(
"type"
)
keys
=
index
.
get
(
"keys"
)
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
isinstance
(
keys
,
list
):
_
=
list
()
for
key
in
keys
:
_
.
append
(
key
[
0
])
if
fields
==
keys
:
continue
keys
=
_
for
field
,
name
in
zip
(
fields
,
idx_name
):
if
field
not
in
keys
:
collection
.
drop_index
(
name
)
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
)
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'
))
scripts/schemas/__pycache__/mongo_schema.cpython-311.pyc
View file @
eebb6b5e
No preview for this file type
scripts/schemas/mongo_schema.py
View file @
eebb6b5e
...
...
@@ -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
,
...
...
scripts/utils/__pycache__/mongo_utils_v2.cpython-311.pyc
View file @
eebb6b5e
No preview for this file type
scripts/utils/mongo_utils_v2.py
View file @
eebb6b5e
...
...
@@ -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
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment