import os import sys # Set path and environment variables to allow imports sys.path.append(os.path.dirname(os.path.abspath(__file__))) os.environ['PYTHONPATH'] = os.path.dirname(os.path.abspath(__file__)) from app.core.database.db_session import engine_crm, SessionLocal from app.models.SystemConfigModel import CrmLocation, Base from sqlalchemy import text # Create tables in CRM database just in case # Base.metadata.create_all(bind=engine_crm) db = SessionLocal() try: # Check if table exists (raw SQL) try: db.execute(text("SELECT 1 FROM crm_locations LIMIT 1")) except Exception as e: print("Table crm_locations does not exist or error:", e) # Create it manually if SQLAlchemy missed it db.execute(text(""" CREATE TABLE IF NOT EXISTS crm_locations ( location_id VARCHAR(50) PRIMARY KEY, tenant_id VARCHAR(50), code VARCHAR(20), name VARCHAR(100), address VARCHAR(255), city VARCHAR(100), state VARCHAR(100), pincode VARCHAR(20), phone VARCHAR(20), is_active BOOLEAN, is_primary BOOLEAN, created_at DATETIME ) """)) db.commit() locations = db.query(CrmLocation).all() if not locations: loc1 = CrmLocation( location_id="loc_1", tenant_id="tenant_1", code="HQ01", name="Headquarters", city="Bangalore", is_active=True, is_primary=True ) db.add(loc1) db.commit() print("Added default location.") else: print("Locations already exist:", [l.name for l in locations]) except Exception as e: print("Error:", e) finally: db.close()