Crm database management

From NaveedTechLab/skills-library · View interactive version

Crm database management is an AI agent skill from the GitHub repository NaveedTechLab/skills-library. Manages the PostgreSQL database (the 'Internal CRM'). Responsible for schema migrations, customer identity resolution across channels, ticket lifecycle tracking, and vector search implementation via pgvector. Use when Claude needs to work with PostgreSQL database operations, perform schema migration

How to use this skill

Skill file

CRM Database Management Skill

This skill provides guidance for managing the PostgreSQL database that serves as the Internal CRM. It covers schema migrations, customer identity resolution across channels, ticket lifecycle tracking, and vector search implementation via pgvector.

Overview

The CRM database management system handles:

Key Components

1. Database Schema Management

The database follows a normalized schema design with these core entities:

2. Schema Migration Strategy

Use Alembic for database migrations with a version-controlled approach:

3. Customer Identity Resolution

Implement a robust customer identity resolution system:

4. Ticket Lifecycle Tracking

Track tickets through their complete lifecycle:

5. Vector Search Implementation

Implement semantic search using pgvector:

Implementation Guidelines

Database Connection Management

Use connection pooling and proper error handling:

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.pool import QueuePool
import os

DATABASE_URL = os.getenv("DATABASE_URL", "postgresql://user:password@localhost/crm")

engine = create_engine(
    DATABASE_URL,
    poolclass=QueuePool,
    pool_size=10,
    max_overflow=20,
    pool_pre_ping=True,  # Verify connections before use
    pool_recycle=3600    # Recycle connections after 1 hour
)

SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)

def get_db():
    db = SessionLocal()
    try:
        return db
    except Exception:
        db.rollback()
        raise
    finally:
        db.close()

Customer Identity Resolution

Implement a flexible identity resolution system:

from typing import Dict, List, Optional
from sqlalchemy.orm import Session

def resolve_customer_identity(
    db: Session,
    email: Optional[str] = None,
    phone: Optional[str] = None,
    external_id: Optional[str] = None
) -> Optional[Dict]:
    """
    Resolve customer identity across multiple identifiers.
    Returns existing customer or creates new one if not found.
    """
    # Search by email first
    if email:
        customer = db.query(Customer).filter(Customer.email == email).first()
        if customer:
            return customer

    # Search by phone
    if phone:
        customer = db.query(Customer).filter(Customer.phone == phone).first()
        if customer:
            return customer

    # Search by external ID
    if external_id:
        customer = db.query(Customer).filter(Customer.external_id == external_id).first()
        if customer:
            return customer

    # If no match found, create new customer
    return None

Ticket Lifecycle Management

Track tickets through their complete lifecycle:

from enum import Enum
from datetime import datetime

class TicketStatus(Enum):
    NEW = "new"
    IN_PROGRESS = "in_progress"
    WAITING_FOR_CUSTOMER = "waiting_for_customer"
    RESOLVED = "resolved"
    CLOSED = "closed"
    REOPENED = "reopened"

class TicketPriority(Enum):
    LOW = "low"
    MEDIUM = "medium"
    HIGH = "high"
    URGENT = "urgent"

def update_ticket_status(
    db: Session,
    ticket_id: int,
    new_status: TicketStatus,
    updated_by: str,
    comment: Optional[str] = None
):
    """
    Update ticket status with audit trail.
    """
    ticket = db.query(Ticket).filter(Ticket.id == ticket_id).first()

    # Create status change record
    status_change = TicketStatusChange(
        ticket_id=ticket_id,
        old_status=ticket.status,
        new_status=new_status,
        changed_by=updated_by,
        changed_at=datetime.utcnow(),
        comment=comment
    )

    db.add(status_change)
    ticket.status = new_status
    ticket.updated_at = datetime.utcnow()

    db.commit()
    return ticket

Best Practices

Performance Optimization

Security Considerations

Data Integrity

Reference Files

For detailed implementation patterns, see:

When NOT to Use This Skill

Common Mistakes

Related Skills

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.