-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
207 lines (195 loc) · 7.93 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import time
from pymongo import MongoClient
import flet as ft
def main(page: ft.Page) -> None:
VNUM = "2024.01"
def mongomove(
e
# origin_connection: str,
# origin_db: str,
# origin_coll: str,
# destination_connection: str,
# destination_db: str,
# destination_coll: str,
# batch_size: int = 100,
# delete: bool = False
) -> int:
"""
Transfer documents from one MongoDB collection to another.
Args:
origin_connection (str): Connection string for the source MongoDB instance.
origin_db (str): Name of the database containing the source collection.
origin_coll (str): Name of the source collection.
destination_connection (str): Connection string for the destination MongoDB instance.
destination_db (str): Name of the database containing the destination collection.
destination_coll (str): Name of the destination collection.
batch_size (int, optional): Number of documents to transfer in each batch. Defaults to 100.
delete (bool, optional): Whether to delete documents from the source collection after transfer. Defaults to False.
Returns:
int: An integer representing the total number of records transferred.
Notes:
This function connects to both MongoDB instances, transfers documents in batches,
and closes the connections when finished.
"""
if not origin_conn.value:
origin_conn.error_text = "This field is required"
page.update()
if not origin_db.value:
origin_db.error_text = "This field is required"
page.update()
if not origin_coll.value:
origin_coll.error_text = "This field is required"
page.update()
if not dest_conn.value:
dest_conn.error_text = "This field is required"
page.update()
if not dest_db.value:
dest_db.error_text = "This field is required"
page.update()
if not dest_coll.value:
dest_coll.error_text = "This field is required"
page.update()
if not batch_size.value:
batch_size.error_text = "This field is required"
page.update()
# Disable the transfer button
trigger.disabled = True
page.update()
# Connect to the source MongoDB
try:
source_client = MongoClient(origin_conn.value)
source_db = source_client[origin_db.value]
source_col = source_db[origin_coll.value]
total_count = source_col.count_documents({})
except Exception:
trigger.disabled = False
page.update()
source_error = ft.AlertDialog(
title=ft.Text("Source DB Connection Error"),
)
page.open(source_error)
return 1
# Get the total count of documents to be moved
details.controls = []
if total_count == 0:
trigger.disabled = False
details.controls.append(ft.Text("No documents to be moved!"))
source_client.close()
page.update()
return 1
details.controls.append(
ft.Text(f"Total number of documents to be moved: {total_count}"))
pb = ft.ProgressBar(width=400, value = 0)
details.controls.append(pb)
page.update()
n_runs = (total_count // batch_size.value) + 1
run_pct = round(1/n_runs, 4)
# Connect to the destination MongoDB
try:
destination_client = MongoClient(dest_conn.value)
destination_db = destination_client[dest_db.value]
destination_col = destination_db[dest_coll.value]
# Operation needed to check the connection
_ = destination_col.count_documents({})
except Exception:
source_client.close()
trigger.disabled = False
details.controls = []
page.update()
destination_error = ft.AlertDialog(
title=ft.Text("Destination DB Connection Error"),
)
page.open(destination_error)
return 1
# Counter for the number of documents transferred
counter = 0
# Loop to operate 100 documents at a time
while True:
# Wat one sec to relief the server
time.sleep(1)
# Retrieve IDs of documents to be copied
if delete.value:
cursor = source_col.find({}, limit=batch_size.value)
else:
cursor = source_col.find({}, limit=batch_size.value, skip=counter)
# Move date to list
records = [doc for doc in cursor]
# Break if no more records
if len(records) == 0:
break
# separate ids and data
copied_ids = [doc.get("_id") for doc in records]
batch = []
for doc in records:
doc.pop("_id", None)
batch.append(doc)
# Insert documents in target collection
destination_col.insert_many(batch)
# Delete documents from source collection if flagged
if delete.value:
source_col.delete_many({'_id': {'$in': copied_ids}})
# Increment the counter
counter += len(copied_ids)
# Update the progress bar
pb.value += run_pct
page.update()
# Close connections
source_client.close()
destination_client.close()
# Enable the transfer button
trigger.disabled = False
details.controls.append(ft.Text("Transfer complete!"))
page.update()
# return number of records transferred
return counter
origin_conn = ft.TextField(label="Connection string for the source MongoDB instance")
origin_db = ft.TextField(label="Name of the database containing the source collection")
origin_coll = ft.TextField(label="Name of the source collection")
dest_conn = ft.TextField(label="Connection string for the destination MongoDB instance")
dest_db = ft.TextField(label="Name of the database containing the destination collection")
dest_coll = ft.TextField(label="Name of the destination collection")
batch_size = ft.TextField(
label="Number of documents to transfer in each batch",
input_filter=ft.InputFilter(allow=True, regex_string=r"^[0-9]*$", replacement_string=""),
value=100
)
delete = ft.Checkbox(label="Delete documents from the source collection after transfer?")
trigger = ft.ElevatedButton("Transfer", on_click=mongomove)
details = ft.Column([])
disclaimer = ft.Text("This tool is provided AS-IS, without warranty of any kind. Use at your own risk.", size=12)
credits = ft.Row([
ft.Text("Source code:", size=12),
ft.ElevatedButton(
text="GitHub",
tooltip="GitHub",
url="https://github.com/paluigi/mongomover")
])
version = ft.Text(f"Version: {VNUM}", size=12)
page.add(
ft.SafeArea(
ft.Column(
[
ft.Text("Mongomover", size=50, weight=ft.FontWeight.BOLD),
ft.Text("Transfer records between MongoDB instances"),
ft.Text("Source collection", size=25, weight=ft.FontWeight.BOLD),
origin_conn,
origin_db,
origin_coll,
ft.Text(value="Destination collection", size=25, weight=ft.FontWeight.BOLD),
dest_conn,
dest_db,
dest_coll,
batch_size,
delete,
trigger,
details,
disclaimer,
credits,
version
])
)
)
# Make the page scrollable
page.scroll = ft.ScrollMode.ADAPTIVE
page.update()
ft.app(main)