:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
# Generated by Django 5.2.17 on 2026-09-11 14:10
import django.utils.timezone
import django.db.models.lookups
import django.db.models.deletion
import django.db.models.functions.comparison
from django.db import migrations, models
import posthog.uuidt
import products.conversations.backend.models.delivery
class Migration(migrations.Migration):
dependencies = [
("conversations", "0069_ticket_compose_dedupe_index"),
("posthog", "1352_email_lookup_indexes"),
]
operations = [
migrations.CreateModel(
name="ConversationDelivery",
fields=[
(
"id",
models.UUIDField(
default=posthog.uuidt.uuid7,
editable=False,
primary_key=True,
serialize=False,
),
),
(
"status",
models.CharField(
choices=[
("pending", "Pending"),
("processing", "Processing"),
("accepted", "Accepted"),
("delivered", "Delivered"),
("failed", "Failed"),
],
default="pending",
max_length=32,
),
),
("attempts", models.PositiveIntegerField(default=0)),
("due_at", models.DateTimeField(default=django.utils.timezone.now)),
("lease_expires_at", models.DateTimeField(blank=True, null=True)),
("fencing_token", models.PositiveBigIntegerField(default=0)),
(
"last_error_code",
models.CharField(blank=True, default="", max_length=64),
),
(
"last_error",
models.CharField(blank=True, default="", max_length=1024),
),
(
"provider_message_id",
models.CharField(blank=True, default="", max_length=255),
),
("payload", models.JSONField(blank=True, null=True)),
("route", models.JSONField(blank=True, null=True)),
("created_at", models.DateTimeField(auto_now_add=True)),
("updated_at", models.DateTimeField(auto_now=True)),
("terminal_at", models.DateTimeField(blank=True, null=True)),
("accepted_at", models.DateTimeField(blank=True, null=True)),
("delivered_at", models.DateTimeField(blank=True, null=True)),
("redriven_at", models.DateTimeField(blank=True, null=True)),
(
"channel",
models.CharField(
choices=products.conversations.backend.models.delivery.conversation_delivery_channel_choices,
max_length=32,
),
),
("comment_id", models.UUIDField()),
("ticket_id", models.UUIDField(blank=True, null=True)),
("provider_account_id", models.CharField(max_length=255)),
(
"team",
models.ForeignKey(
db_constraint=False,
db_index=False,
on_delete=django.db.models.deletion.CASCADE,
related_name="+",
to="posthog.team",
),
),
],
options={
"db_table": "posthog_conversations_delivery",
},
),
migrations.CreateModel(
name="ConversationDeliveryPart",
fields=[
(
"id",
models.UUIDField(
default=posthog.uuidt.uuid7,
editable=False,
primary_key=True,
serialize=False,
),
),
(
"status",
models.CharField(
choices=[
("pending", "Pending"),
("processing", "Processing"),
("accepted", "Accepted"),
("delivered", "Delivered"),
("failed", "Failed"),
],
default="pending",
max_length=32,
),
),
("attempts", models.PositiveIntegerField(default=0)),
("due_at", models.DateTimeField(default=django.utils.timezone.now)),
("lease_expires_at", models.DateTimeField(blank=True, null=True)),
("fencing_token", models.PositiveBigIntegerField(default=0)),
(
"last_error_code",
models.CharField(blank=True, default="", max_length=64),
),
(
"last_error",
models.CharField(blank=True, default="", max_length=1024),
),
(
"provider_message_id",
models.CharField(blank=True, default="", max_length=255),
),
("payload", models.JSONField(blank=True, null=True)),
("route", models.JSONField(blank=True, null=True)),
("created_at", models.DateTimeField(auto_now_add=True)),
("updated_at", models.DateTimeField(auto_now=True)),
("terminal_at", models.DateTimeField(blank=True, null=True)),
("accepted_at", models.DateTimeField(blank=True, null=True)),
("delivered_at", models.DateTimeField(blank=True, null=True)),
("redriven_at", models.DateTimeField(blank=True, null=True)),
("part_key", models.CharField(max_length=128)),
(
"client_msg_id",
models.CharField(blank=True, default="", max_length=255),
),
(
"delivery",
models.ForeignKey(
db_index=False,
on_delete=django.db.models.deletion.CASCADE,
related_name="parts",
to="conversations.conversationdelivery",
),
),
(
"team",
models.ForeignKey(
db_constraint=False,
on_delete=django.db.models.deletion.CASCADE,
related_name="+",
to="posthog.team",
),
),
],
options={
"db_table": "posthog_conversations_delivery_part",
},
),
migrations.AddIndex(
model_name="conversationdelivery",
index=models.Index(
condition=models.Q(("status", "pending")),
fields=["due_at"],
name="conv_delivery_pending_idx",
),
),
migrations.AddIndex(
model_name="conversationdelivery",
index=models.Index(
condition=models.Q(("status", "processing")),
fields=["lease_expires_at"],
name="conv_delivery_lease_idx",
),
),
migrations.AddIndex(
model_name="conversationdelivery",
index=models.Index(
condition=models.Q(("status__in", ["accepted", "delivered", "failed"])),
fields=["terminal_at"],
name="conv_delivery_terminal_idx",
),
),
migrations.AddIndex(
model_name="conversationdelivery",
index=models.Index(
condition=models.Q(
("status__in", ["accepted", "delivered", "failed"]),
models.Q(
("payload__isnull", False),
("route__isnull", False),
_connector="OR",
),
),
fields=["terminal_at"],
name="conv_delivery_payload_gc_idx",
),
),
migrations.AddConstraint(
model_name="conversationdelivery",
constraint=models.UniqueConstraint(
fields=("team", "channel", "comment_id"),
name="unique_delivery_per_comment_channel",
),
),
migrations.AddConstraint(
model_name="conversationdelivery",
constraint=models.UniqueConstraint(fields=("id", "team"), name="unique_delivery_id_team"),
),
migrations.AddConstraint(
model_name="conversationdelivery",
constraint=models.CheckConstraint(
condition=models.Q(("channel", ""), _negated=True),
name="delivery_channel_not_empty",
),
),
migrations.AddConstraint(
model_name="conversationdelivery",
constraint=models.CheckConstraint(
condition=models.Q(("provider_account_id", ""), _negated=True),
name="delivery_provider_id_not_empty",
),
),
migrations.AddConstraint(
model_name="conversationdelivery",
constraint=models.CheckConstraint(
condition=models.Q(
("route__isnull", True),
django.db.models.lookups.LessThanOrEqual(
models.Func(
django.db.models.functions.comparison.Cast(
models.F("route"), output_field=models.TextField()
),
function="octet_length",
output_field=models.IntegerField(),
),
models.Value(262144),
),
_connector="OR",
),
name="delivery_route_size",
),
),
migrations.AddConstraint(
model_name="conversationdelivery",
constraint=models.CheckConstraint(
condition=models.Q(
("payload__isnull", True),
django.db.models.lookups.LessThanOrEqual(
models.Func(
django.db.models.functions.comparison.Cast(
models.F("payload"), output_field=models.TextField()
),
function="octet_length",
output_field=models.IntegerField(),
),
models.Value(262144),
),
_connector="OR",
),
name="delivery_payload_size",
),
),
migrations.AddConstraint(
model_name="conversationdelivery",
constraint=models.CheckConstraint(
condition=models.Q(
models.Q(("status", "processing"), _negated=True),
("lease_expires_at__isnull", False),
_connector="OR",
),
name="delivery_processing_has_lease",
),
),
migrations.AddConstraint(
model_name="conversationdelivery",
constraint=models.CheckConstraint(
condition=models.Q(
models.Q(
("status__in", ["accepted", "delivered", "failed"]),
("terminal_at__isnull", False),
),
models.Q(
("status__in", ["pending", "processing"]),
("terminal_at__isnull", True),
),
_connector="OR",
),
name="delivery_terminal_at",
),
),
migrations.AddIndex(
model_name="conversationdeliverypart",
index=models.Index(
condition=models.Q(("status", "pending")),
fields=["due_at"],
name="conv_dpart_pending_idx",
),
),
migrations.AddIndex(
model_name="conversationdeliverypart",
index=models.Index(
condition=models.Q(("status", "processing")),
fields=["lease_expires_at"],
name="conv_dpart_lease_idx",
),
),
migrations.AddIndex(
model_name="conversationdeliverypart",
index=models.Index(
condition=models.Q(("status__in", ["accepted", "delivered", "failed"])),
fields=["terminal_at"],
name="conv_dpart_terminal_idx",
),
),
migrations.AddIndex(
model_name="conversationdeliverypart",
index=models.Index(
condition=models.Q(
("status__in", ["accepted", "delivered", "failed"]),
models.Q(
("payload__isnull", False),
("route__isnull", False),
_connector="OR",
),
),
fields=["terminal_at"],
name="conv_dpart_payload_gc_idx",
),
),
migrations.AddConstraint(
model_name="conversationdeliverypart",
constraint=models.UniqueConstraint(fields=("delivery", "part_key"), name="unique_delivery_part_per_key"),
),
migrations.AddConstraint(
model_name="conversationdeliverypart",
constraint=models.CheckConstraint(
condition=models.Q(("part_key", ""), _negated=True),
name="delivery_part_key_not_empty",
),
),
migrations.AddConstraint(
model_name="conversationdeliverypart",
constraint=models.CheckConstraint(
condition=models.Q(
("route__isnull", True),
django.db.models.lookups.LessThanOrEqual(
models.Func(
django.db.models.functions.comparison.Cast(
models.F("route"), output_field=models.TextField()
),
function="octet_length",
output_field=models.IntegerField(),
),
models.Value(262144),
),
_connector="OR",
),
name="delivery_part_route_size",
),
),
migrations.AddConstraint(
model_name="conversationdeliverypart",
constraint=models.CheckConstraint(
condition=models.Q(
("payload__isnull", True),
django.db.models.lookups.LessThanOrEqual(
models.Func(
django.db.models.functions.comparison.Cast(
models.F("payload"), output_field=models.TextField()
),
function="octet_length",
output_field=models.IntegerField(),
),
models.Value(262144),
),
_connector="OR",
),
name="delivery_part_payload_size",
),
),
migrations.AddConstraint(
model_name="conversationdeliverypart",
constraint=models.CheckConstraint(
condition=models.Q(
models.Q(("status", "processing"), _negated=True),
("lease_expires_at__isnull", False),
_connector="OR",
),
name="delivery_part_processing_has_lease",
),
),
migrations.AddConstraint(
model_name="conversationdeliverypart",
constraint=models.CheckConstraint(
condition=models.Q(
models.Q(
("status__in", ["accepted", "delivered", "failed"]),
("terminal_at__isnull", False),
),
models.Q(
("status__in", ["pending", "processing"]),
("terminal_at__isnull", True),
),
_connector="OR",
),
name="delivery_part_terminal_at",
),
),
# CHECK cannot see the parent row. This composite FK requires part.team_id
# to equal the delivery's team_id so a different part_key cannot attach
# work to another team's delivery. Immediate, because a deferred FK would
# not fire inside Django TestCase (the test transaction never commits).
# ON DELETE CASCADE: a default NO ACTION FK would race Django's deferred
# delivery_id CASCADE and can reject parent deletes while parts still exist.
# NOT VALID skips the existing-row scan (the table is empty here). New rows
# are still checked. VALIDATE in this same operation leaves the constraint
# fully valid; Postgres no-ops VALIDATE if it already is.
migrations.RunSQL(
sql="""
DO $$ BEGIN
IF NOT EXISTS (
SELECT 1 FROM pg_constraint WHERE conname = 'delivery_part_matches_parent_team'
) THEN
ALTER TABLE posthog_conversations_delivery_part
ADD CONSTRAINT delivery_part_matches_parent_team
FOREIGN KEY (delivery_id, team_id)
REFERENCES posthog_conversations_delivery (id, team_id)
ON DELETE CASCADE
NOT VALID;
END IF;
END $$;
ALTER TABLE posthog_conversations_delivery_part
VALIDATE CONSTRAINT delivery_part_matches_parent_team;
""",
reverse_sql="""
ALTER TABLE posthog_conversations_delivery_part
DROP CONSTRAINT IF EXISTS delivery_part_matches_parent_team;
""",
),
]