|
1 | 1 | from functools import partial |
2 | 2 |
|
| 3 | +from biothings import config as btconfig |
3 | 4 | from biothings.hub.manager import BaseManager |
4 | 5 | from biothings.utils.hub_db import get_src_build |
5 | 6 | from config import logger as logging |
@@ -36,22 +37,59 @@ def list_builds(self, build_config=None, build_name=None): |
36 | 37 |
|
37 | 38 | async def delete_builds(self, build_ids): |
38 | 39 | if not build_ids: |
39 | | - return {"deleted_count": 0} |
| 40 | + return { |
| 41 | + "deleted_count": 0, |
| 42 | + "target_collections_deleted_count": 0, |
| 43 | + "target_collections_deleted": [], |
| 44 | + } |
40 | 45 |
|
41 | 46 | from biothings.utils import mongo |
42 | 47 |
|
43 | 48 | conn = mongo.get_hub_db_async_conn() |
44 | 49 | try: |
45 | 50 | src_build = mongo.get_src_build_async(conn) |
| 51 | + |
| 52 | + build_docs = [] |
| 53 | + async for doc in src_build.find({"_id": {"$in": build_ids}}, {"_id": 1, "target_name": 1}): |
| 54 | + build_docs.append(doc) |
| 55 | + |
| 56 | + target_collection_candidates = set() |
| 57 | + for doc in build_docs: |
| 58 | + build_id = doc["_id"] |
| 59 | + target_name = doc.get("target_name") |
| 60 | + target_collection_candidates.add(build_id) |
| 61 | + if target_name and target_name != build_id: |
| 62 | + target_collection_candidates.add(target_name) |
| 63 | + |
| 64 | + target_collections_deleted = [] |
| 65 | + if target_collection_candidates: |
| 66 | + target_db = conn[btconfig.DATA_TARGET_DATABASE] |
| 67 | + existing_collections = await target_db.list_collection_names( |
| 68 | + filter={"name": {"$in": list(target_collection_candidates)}} |
| 69 | + ) |
| 70 | + |
| 71 | + for collection_name in existing_collections: |
| 72 | + await target_db[collection_name].drop() |
| 73 | + target_collections_deleted.append(collection_name) |
| 74 | + |
46 | 75 | result = await src_build.delete_many({"_id": {"$in": build_ids}}) |
47 | | - return {"deleted_count": result.deleted_count} |
| 76 | + return { |
| 77 | + "deleted_count": result.deleted_count, |
| 78 | + "target_collections_deleted_count": len(target_collections_deleted), |
| 79 | + "target_collections_deleted": sorted(target_collections_deleted), |
| 80 | + } |
48 | 81 | finally: |
49 | 82 | await conn.close() |
50 | 83 |
|
51 | 84 | def done(self, future): |
52 | 85 | try: |
53 | 86 | result = future.result() |
54 | | - logging.info("Deleted %d MongoDB builds", result.get("deleted_count", 0), extra={"notify": True}) |
| 87 | + logging.info( |
| 88 | + "Deleted %d MongoDB builds and dropped %d target collections", |
| 89 | + result.get("deleted_count", 0), |
| 90 | + result.get("target_collections_deleted_count", 0), |
| 91 | + extra={"notify": True}, |
| 92 | + ) |
55 | 93 | except Exception as exc: |
56 | 94 | logging.exception("Failed to delete MongoDB builds: %s", exc, extra={"notify": True}) |
57 | 95 |
|
|
0 commit comments