Skip to content

Commit fdbcf10

Browse files
committed
Take 3 at migration
1 parent a860dd9 commit fdbcf10

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

migrate_collections.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,27 @@
1818
db["Migration"].insert_one({"migration_name": "FreeTier", "migration_complete": False})
1919

2020
earliest_timestamp = datetime.now()
21+
latest_timestamp = datetime.min
2122

2223
for collection_name in collections_to_migrate:
2324
collection = db[collection_name]
2425
earliest_document = collection.find_one(sort=[("timestamp", ASCENDING)])
26+
latest_document = collection.find_one(sort=[("timestamp", -1)])
2527
if earliest_document and earliest_document["timestamp"] < earliest_timestamp:
2628
earliest_timestamp = earliest_document["timestamp"]
29+
if latest_document and latest_document["timestamp"] > latest_timestamp:
30+
latest_timestamp = latest_document["timestamp"]
2731

2832
start_date = earliest_timestamp
2933
one_week = timedelta(weeks=1)
3034

31-
while True:
35+
while start_date <= latest_timestamp:
3236
end_date = start_date + one_week
3337
buffer = {}
34-
has_data = False # Flag to check if data exists for the current chunk
3538

3639
for collection_name in collections_to_migrate:
3740
collection = db[collection_name]
3841
for document in collection.find({"timestamp": {"$gte": start_date, "$lt": end_date}}):
39-
has_data = True # Data exists for this chunk
40-
4142
unit = document["metadata"].get("unit")
4243
new_document = {
4344
"metadata": {
@@ -64,15 +65,21 @@
6465
else:
6566
buffer[document["timestamp"]] = new_document
6667

67-
if not has_data: # If no data for the current chunk, stop the loop
68-
break
69-
7068
all_documents = sorted(buffer.values(), key=itemgetter("timestamp"))
7169
free_tier_collection = db["FreeTier"]
72-
7370
for document in all_documents:
7471
free_tier_collection.insert_one(document)
7572

76-
start_date = end_date # Move to the next chunk
73+
# Update the latest_timestamp after processing this chunk, to check if new data has been added.
74+
new_latest_timestamp = datetime.min
75+
for collection_name in collections_to_migrate:
76+
collection = db[collection_name]
77+
latest_document = collection.find_one(sort=[("timestamp", -1)])
78+
if latest_document and latest_document["timestamp"] > new_latest_timestamp:
79+
new_latest_timestamp = latest_document["timestamp"]
80+
81+
# If there are new records added, the while loop will continue until there are no more records.
82+
latest_timestamp = new_latest_timestamp
83+
start_date = end_date
7784

78-
db["Migration"].update_one({"migration_name": "FreeTier"}, {"$set": {"migration_complete": True}})
85+
# db["Migration"].update_one({"migration_name": "FreeTier"}, {"$set": {"migration_complete": True}})

0 commit comments

Comments
 (0)