Spaces:
Running
Running
| """ | |
| Database initialization script for SCM microservice. | |
| Creates all collections, indexes, and validation rules. | |
| """ | |
| import asyncio | |
| from motor.motor_asyncio import AsyncIOMotorClient | |
| from app.core.logging import get_logger | |
| from app.core.config import settings | |
| from app.db_init.merchant_settings_init import initialize_merchant_settings_database | |
| logger = get_logger(__name__) | |
| async def initialize_database(): | |
| """ | |
| Initialize the complete SCM database structure. | |
| This script creates: | |
| - Collections with schema validation | |
| - Performance indexes | |
| - Default data (if needed) | |
| """ | |
| client = None | |
| try: | |
| logger.info("Starting database initialization") | |
| # Connect to MongoDB | |
| logger.info(f"Connecting to MongoDB: {settings.MONGODB_URI}") | |
| client = AsyncIOMotorClient(settings.MONGODB_URI) | |
| db = client[settings.MONGODB_DB_NAME] | |
| # Test connection | |
| await client.admin.command('ping') | |
| logger.info(f"Connected to database: {settings.MONGODB_DB_NAME}") | |
| # Initialize merchant settings | |
| await initialize_merchant_settings_database(db) | |
| # Add other collection initializations here as needed | |
| # await initialize_merchants_database(db) | |
| # await initialize_employees_database(db) | |
| # await initialize_orders_database(db) | |
| logger.info("Database initialization completed successfully") | |
| except Exception as e: | |
| logger.error("Database initialization failed", exc_info=e) | |
| raise | |
| finally: | |
| if client: | |
| client.close() | |
| logger.info("Database connection closed") | |
| async def verify_database_setup(): | |
| """ | |
| Verify that the database setup is correct. | |
| """ | |
| client = None | |
| try: | |
| logger.info("Starting database verification") | |
| # Connect to MongoDB | |
| client = AsyncIOMotorClient(settings.MONGODB_URI) | |
| db = client[settings.MONGODB_DB_NAME] | |
| # Test connection | |
| await client.admin.command('ping') | |
| # List all collections | |
| collections = await db.list_collection_names() | |
| logger.info(f"Found collections: {collections}") | |
| # Check merchant_settings collection | |
| from app.merchants.constants import SCM_MERCHANT_SETTINGS_COLLECTION | |
| if SCM_MERCHANT_SETTINGS_COLLECTION in collections: | |
| collection = db[SCM_MERCHANT_SETTINGS_COLLECTION] | |
| # Check indexes | |
| indexes = await collection.index_information() | |
| logger.info(f"Merchant settings indexes: {list(indexes.keys())}") | |
| # Check collection stats | |
| stats = await db.command("collStats", SCM_MERCHANT_SETTINGS_COLLECTION) | |
| logger.info(f"Merchant settings collection stats:", extra={ | |
| "count": stats.get("count", 0), | |
| "size": stats.get("size", 0), | |
| "indexes": stats.get("nindexes", 0) | |
| }) | |
| logger.info("β Merchant settings collection is properly configured") | |
| else: | |
| logger.warning(f"β Collection {SCM_MERCHANT_SETTINGS_COLLECTION} not found") | |
| logger.info("Database verification completed") | |
| except Exception as e: | |
| logger.error("Database verification failed", exc_info=e) | |
| raise | |
| finally: | |
| if client: | |
| client.close() | |
| if __name__ == "__main__": | |
| import sys | |
| # Check command line arguments | |
| if len(sys.argv) > 1: | |
| command = sys.argv[1] | |
| if command == "init": | |
| print("π Initializing database...") | |
| asyncio.run(initialize_database()) | |
| print("β Database initialization completed") | |
| elif command == "verify": | |
| print("π Verifying database setup...") | |
| asyncio.run(verify_database_setup()) | |
| print("β Database verification completed") | |
| else: | |
| print(f"Unknown command: {command}") | |
| print("Usage: python db_init.py [init|verify]") | |
| sys.exit(1) | |
| else: | |
| print("Usage:") | |
| print(" python db_init.py init - Initialize database structure") | |
| print(" python db_init.py verify - Verify database setup") | |
| sys.exit(1) | |