Spaces:
Running
Running
File size: 4,711 Bytes
41aafc4 cfd9177 41aafc4 cfd9177 41aafc4 cfd9177 41aafc4 cfd9177 41aafc4 | 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 | """
Database initialization utilities.
Creates indexes and sets up collections for the SCM microservice.
"""
# from insightfy_utils.logging import get_logger # TODO: Uncomment when package is available
import logging
from app.nosql import get_database
from app.constants.collections import (
SCM_MERCHANTS_COLLECTION,
SCM_staff_COLLECTION,
SCM_ROLES_COLLECTION,
SCM_AUTH_LOGS_COLLECTION,
SCM_SALES_ORDERS_COLLECTION,
SCM_RMA_COLLECTION,
SCM_CREDIT_NOTES_COLLECTION
)
# logger = get_logger(__name__) # TODO: Uncomment when insightfy_utils is available
logger = logging.getLogger(__name__)
async def create_indexes():
"""
Create all required database indexes.
Should be called during application startup.
"""
try:
logger.info("Creating database indexes")
# Merchants collection indexes
await get_database()[SCM_MERCHANTS_COLLECTION].create_index("merchant_id", unique=True)
await get_database()[SCM_MERCHANTS_COLLECTION].create_index("business_name", unique=True)
await get_database()[SCM_MERCHANTS_COLLECTION].create_index("merchant_type")
await get_database()[SCM_MERCHANTS_COLLECTION].create_index("contact_email")
logger.info(f"Created indexes for {SCM_MERCHANTS_COLLECTION}")
# staff collection indexes
await get_database()[SCM_staff_COLLECTION].create_index("associate_id", unique=True)
await get_database()[SCM_staff_COLLECTION].create_index("email", unique=True)
await get_database()[SCM_staff_COLLECTION].create_index("mobile", unique=True)
await get_database()[SCM_staff_COLLECTION].create_index("merchant_id")
await get_database()[SCM_staff_COLLECTION].create_index([
("merchant_id", 1),
("role_id", 1)
])
logger.info(f"Created indexes for {SCM_staff_COLLECTION}")
# Roles collection indexes
await get_database()[SCM_ROLES_COLLECTION].create_index([
("merchant_id", 1),
("role_id", 1)
], unique=True)
await get_database()[SCM_ROLES_COLLECTION].create_index("merchant_id")
logger.info(f"Created indexes for {SCM_ROLES_COLLECTION}")
# Auth logs collection indexes
await get_database()[SCM_AUTH_LOGS_COLLECTION].create_index([
("merchant_id", 1),
("timestamp", -1)
])
await get_database()[SCM_AUTH_LOGS_COLLECTION].create_index([
("associate_id", 1),
("timestamp", -1)
])
await get_database()[SCM_AUTH_LOGS_COLLECTION].create_index("event_type")
# TTL index for 90-day retention (7776000 seconds)
await get_database()[SCM_AUTH_LOGS_COLLECTION].create_index(
"timestamp",
expireAfterSeconds=7776000
)
logger.info(f"Created indexes for {SCM_AUTH_LOGS_COLLECTION}")
# Sales orders collection indexes
await get_database()[SCM_SALES_ORDERS_COLLECTION].create_index("sales_order_id", unique=True)
await get_database()[SCM_SALES_ORDERS_COLLECTION].create_index("order_number", unique=True)
await get_database()[SCM_SALES_ORDERS_COLLECTION].create_index("merchant_id")
await get_database()[SCM_SALES_ORDERS_COLLECTION].create_index("branch_id")
await get_database()[SCM_SALES_ORDERS_COLLECTION].create_index("status")
await get_database()[SCM_SALES_ORDERS_COLLECTION].create_index("order_date")
await get_database()[SCM_SALES_ORDERS_COLLECTION].create_index("customer.customer_id")
await get_database()[SCM_SALES_ORDERS_COLLECTION].create_index([
("merchant_id", 1),
("order_date", -1)
])
logger.info(f"Created indexes for {SCM_SALES_ORDERS_COLLECTION}")
logger.info("All database indexes created successfully")
except Exception as e:
logger.error("Failed to create database indexes", exc_info=e)
raise
async def drop_indexes():
"""
Drop all indexes (useful for testing).
"""
try:
logger.warning("Dropping all database indexes")
await get_database()[SCM_MERCHANTS_COLLECTION].drop_indexes()
await get_database()[SCM_staff_COLLECTION].drop_indexes()
await get_database()[SCM_ROLES_COLLECTION].drop_indexes()
await get_database()[SCM_AUTH_LOGS_COLLECTION].drop_indexes()
await get_database()[SCM_SALES_ORDERS_COLLECTION].drop_indexes()
logger.info("All database indexes dropped")
except Exception as e:
logger.error("Failed to drop database indexes", exc_info=e)
raise
|