Spaces:
Runtime error
Runtime error
File size: 5,196 Bytes
a54b40b c0b58c9 d58df14 4292ec7 a54b40b d58df14 4292ec7 d58df14 4ff53cb a54b40b d58df14 a54b40b 4ff53cb a54b40b d58df14 a54b40b d58df14 4ff53cb a54b40b d58df14 a54b40b d58df14 a54b40b d58df14 a54b40b d58df14 a54b40b 4ff53cb a54b40b 461ab1b d58df14 461ab1b 4ff53cb 461ab1b a54b40b d58df14 a54b40b | 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 | """
Database initialization utilities.
Creates indexes and sets up collections for the SCM microservice.
"""
from app.core.logging import get_logger
from app.nosql import get_database
from app.merchants.constants import SCM_MERCHANTS_COLLECTION
from app.employees.constants import SCM_EMPLOYEES_COLLECTION
from app.access_roles.constants import SCM_ROLES_COLLECTION
from app.core.constants import SCM_AUTH_LOGS_COLLECTION
from app.sales.constants import SCM_SALES_ORDERS_COLLECTION, SCM_RMA_COLLECTION, SCM_CREDIT_NOTES_COLLECTION
logger = get_logger(__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("merchant_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(
"Created indexes for collection",
extra={"event": "db_indexes_created", "collection_name": SCM_MERCHANTS_COLLECTION},
)
# Employees collection indexes
await get_database()[SCM_EMPLOYEES_COLLECTION].create_index("associate_id", unique=True)
await get_database()[SCM_EMPLOYEES_COLLECTION].create_index("email", unique=True)
await get_database()[SCM_EMPLOYEES_COLLECTION].create_index("mobile", unique=True)
await get_database()[SCM_EMPLOYEES_COLLECTION].create_index("merchant_id")
await get_database()[SCM_EMPLOYEES_COLLECTION].create_index([
("merchant_id", 1),
("role_id", 1)
])
logger.info(
"Created indexes for collection",
extra={"event": "db_indexes_created", "collection_name": SCM_EMPLOYEES_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(
"Created indexes for collection",
extra={"event": "db_indexes_created", "collection_name": 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(
"Created indexes for collection",
extra={"event": "db_indexes_created", "collection_name": 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(
"Created indexes for collection",
extra={"event": "db_indexes_created", "collection_name": 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_EMPLOYEES_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
|