Files
leocrm/app/models/contact_merge.py
T
Agent Zero 6555655ecf
Check Cross-Plugin Imports / check (push) Has been cancelled
fix: sync all models with production DB schema
- Add OwnedMixin to 29 model files (78 tables that had owner_id in DB but not in model)
- Add search/embedding columns to 9 model files (18 columns: search_tsv, embedding, indexed_at, content_text, content_tsv, body_tsv, company_id, deleted_at)
- Fix import syntax errors in calendar/models.py, mail/models.py, notification.py, contact.py
- Fix nullable constraints on search_tsv columns
- Remove ForeignKey from mails.company_id (companies table not always loaded in test context)
- All 36 tests pass (24 Phase J + 12 Phase K)
- Models now match production DB schema
2026-08-21 11:20:15 +02:00

47 lines
1.8 KiB
Python

"""ContactMergeHistory model — tracks contact deduplication/merge operations."""
from __future__ import annotations
import uuid
from sqlalchemy import ForeignKey, Index, Text, text
from sqlalchemy.dialects.postgresql import JSONB
from sqlalchemy.dialects.postgresql import UUID as PGUUID
from sqlalchemy.orm import Mapped, mapped_column
from app.core.db import Base, TenantMixin
from app.models.owned_mixin import OwnedMixin
class ContactMergeHistory(Base, TenantMixin, OwnedMixin):
"""Records each contact merge operation (source → target).
When two duplicate contacts are merged, the source contact is soft-deleted
and its entity_links, tags, etc. are re-pointed to the target contact.
This table preserves an audit trail of which fields were merged and by whom.
"""
__tablename__ = "contact_merge_history"
__table_args__ = (
Index("ix_contact_merge_history_tenant", "tenant_id"),
Index("ix_contact_merge_history_target", "tenant_id", "target_contact_id"),
Index("ix_contact_merge_history_source", "tenant_id", "source_contact_id"),
)
id: Mapped[uuid.UUID] = mapped_column(
PGUUID(as_uuid=True), primary_key=True, default=uuid.uuid4
)
source_contact_id: Mapped[uuid.UUID] = mapped_column(
PGUUID(as_uuid=True), ForeignKey("contacts.id", ondelete="SET NULL"), nullable=False
)
target_contact_id: Mapped[uuid.UUID] = mapped_column(
PGUUID(as_uuid=True), ForeignKey("contacts.id", ondelete="CASCADE"), nullable=False
)
merged_fields: Mapped[dict] = mapped_column(
JSONB, nullable=False, server_default=text("'{}'::jsonb")
)
merged_by: Mapped[uuid.UUID] = mapped_column(
PGUUID(as_uuid=True), ForeignKey("users.id", ondelete="SET NULL"), nullable=True
)
note: Mapped[str | None] = mapped_column(Text, nullable=True)