Skip to content

Commit

Permalink
Delete data script
Browse files Browse the repository at this point in the history
  • Loading branch information
avgupta456 committed Nov 19, 2023
1 parent 9c4ddd1 commit 10f3da5
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions backend/delete_old_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
from typing import Any

import asyncio
from dotenv import find_dotenv, load_dotenv
from datetime import datetime

load_dotenv(find_dotenv())

# flake8: noqa E402

from src.constants import API_VERSION
from src.data.mongo.main import USER_MONTHS


def get_filters(cutoff_date: datetime) -> Any:
return {
"$or": [
{"month": {"$lte": cutoff_date}},
{"version": {"$ne": API_VERSION}},
],
}


async def count_old_rows(cutoff_date: datetime) -> int:
filters = get_filters(cutoff_date)
num_rows = len(await USER_MONTHS.find(filters).to_list(length=None)) # type: ignore
return num_rows


async def delete_old_rows(cutoff_date: datetime):
filters = get_filters(cutoff_date)
result = await USER_MONTHS.delete_many(filters) # type: ignore
print(f"Deleted {result.deleted_count} rows")


async def main():
# Replace 'your_date_field' with the actual name of your date field
cutoff_date = datetime(2022, 12, 31)

count = await count_old_rows(cutoff_date)
if count == 0:
print("No rows to delete.")
return

print(f"Found {count} rows to delete.")
print()

confirmation = input("Are you sure you want to delete these rows? (yes/no): ")
if confirmation.lower() != "yes":
print("Operation canceled.")
return

print()
await delete_old_rows(cutoff_date)


if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.run_until_complete(main())

0 comments on commit 10f3da5

Please sign in to comment.