Skip to content

: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.

feat(warehouse_sources): list a source's schemas on its admin page (#90898)

Daniel RC67e78821b5cf
diff --git a/posthog/templates/admin/data_warehouse/externaldatasource/change_form.html b/posthog/templates/admin/data_warehouse/externaldatasource/change_form.html
new file mode 100644
index 000000000000..3f02b0eadc72
--- /dev/null
+++ b/posthog/templates/admin/data_warehouse/externaldatasource/change_form.html
@@ -0,0 +1,98 @@
+{% extends "admin/change_form.html" %}
+
+{% block after_field_sets %}
+ {{ block.super }}
+ <fieldset class="module aligned">
+ <h2>Schemas{% if schema_page %} ({{ schema_page.paginator.count }} total){% endif %}</h2>
+ {% if schema_page and schema_page.object_list %}
+ <table style="width: 100%; border-collapse: collapse;">
+ <thead>
+ <tr style="background: #f5f5f5;">
+ <th style="padding: 8px; text-align: left; border-bottom: 2px solid #ddd;">Schema ID</th>
+ <th style="padding: 8px; text-align: left; border-bottom: 2px solid #ddd;">Name</th>
+ <th style="padding: 8px; text-align: left; border-bottom: 2px solid #ddd;">Status</th>
+ <th style="padding: 8px; text-align: left; border-bottom: 2px solid #ddd;">Sync enabled</th>
+ <th style="padding: 8px; text-align: left; border-bottom: 2px solid #ddd;">Sync type</th>
+ <th style="padding: 8px; text-align: left; border-bottom: 2px solid #ddd;">Frequency</th>
+ <th style="padding: 8px; text-align: left; border-bottom: 2px solid #ddd;">Last synced at</th>
+ <th style="padding: 8px; text-align: right; border-bottom: 2px solid #ddd;">Table rows</th>
+ <th style="padding: 8px; text-align: left; border-bottom: 2px solid #ddd;">Table</th>
+ <th style="padding: 8px; text-align: left; border-bottom: 2px solid #ddd;">Latest error</th>
+ </tr>
+ </thead>
+ <tbody>
+ {% for schema in schema_page %}
+ <tr style="border-bottom: 1px solid #eee;">
+ <td style="padding: 8px; font-family: monospace; font-size: 12px;">
+ <a href="{% url 'admin:warehouse_sources_externaldataschema_change' schema.pk %}">{{ schema.id }}</a>
+ </td>
+ <td style="padding: 8px;">
+ {{ schema.name }}
+ {% if schema.deleted %}<span style="color: red; font-size: 11px;">(deleted)</span>{% endif %}
+ </td>
+ <td style="padding: 8px;">
+ {% if schema.status == "Running" %}
+ <span style="color: blue;">Running</span>
+ {% elif schema.status == "Completed" %}
+ <span style="color: green;">Completed</span>
+ {% elif schema.status == "Failed" %}
+ <span style="color: red;">Failed</span>
+ {% elif schema.status == "BillingLimitReached" %}
+ <span style="color: orange;">Billing limit reached</span>
+ {% elif schema.status == "BillingLimitTooLow" %}
+ <span style="color: orange;">Billing limit too low</span>
+ {% elif schema.status %}
+ <span>{{ schema.status }}</span>
+ {% else %}
+ &mdash;
+ {% endif %}
+ </td>
+ <td style="padding: 8px;">{% if schema.should_sync %}Yes{% else %}No{% endif %}</td>
+ <td style="padding: 8px;">{{ schema.sync_type|default:"&mdash;" }}</td>
+ <td style="padding: 8px;">{{ schema.sync_frequency_interval|default:"&mdash;" }}</td>
+ <td style="padding: 8px;">{% if schema.last_synced_at %}{{ schema.last_synced_at|date:"Y-m-d H:i:s" }}{% else %}&mdash;{% endif %}</td>
+ <td style="padding: 8px; text-align: right;">{% if schema.table and schema.table.row_count is not None %}{{ schema.table.row_count }}{% else %}&mdash;{% endif %}</td>
+ <td style="padding: 8px;">
+ {% if schema.table %}
+ <a href="{% url 'admin:warehouse_sources_datawarehousetable_change' schema.table.pk %}">{{ schema.table.name }}</a>
+ {% else %}
+ &mdash;
+ {% endif %}
+ </td>
+ <td style="padding: 8px;">
+ {% if schema.latest_error %}
+ <span style="color: red; font-size: 12px;" title="{{ schema.latest_error }}">
+ {{ schema.latest_error|truncatechars:80 }}
+ </span>
+ {% else %}
+ &mdash;
+ {% endif %}
+ </td>
+ </tr>
+ {% endfor %}
+ </tbody>
+ </table>
+
+ {% if schema_page.paginator.num_pages > 1 %}
+ <div style="padding: 10px; text-align: center; border-top: 1px solid #ddd;">
+ {% if schema_page.has_previous %}
+ <a href="{{ page_link_prefix }}1">&laquo; First</a>
+ <a href="{{ page_link_prefix }}{{ schema_page.previous_page_number }}" style="margin-left: 8px;">&lsaquo; Previous</a>
+ {% endif %}
+
+ <span style="margin: 0 12px;">
+ Page {{ schema_page.number }} of {{ schema_page.paginator.num_pages }}
+ </span>
+
+ {% if schema_page.has_next %}
+ <a href="{{ page_link_prefix }}{{ schema_page.next_page_number }}">Next &rsaquo;</a>
+ <a href="{{ page_link_prefix }}{{ schema_page.paginator.num_pages }}" style="margin-left: 8px;">Last &raquo;</a>
+ {% endif %}
+ </div>
+ {% endif %}
+
+ {% else %}
+ <div class="form-row" style="padding: 10px; color: #666;">No schemas found for this source.</div>
+ {% endif %}
+ </fieldset>
+{% endblock %}
diff --git a/products/warehouse_sources/backend/admin/external_data_source_admin.py b/products/warehouse_sources/backend/admin/external_data_source_admin.py
index ddfdf5909ded..e108269e6c3d 100644
--- a/products/warehouse_sources/backend/admin/external_data_source_admin.py
+++ b/products/warehouse_sources/backend/admin/external_data_source_admin.py
@@ -1,11 +1,14 @@
+from typing import Any
+
from django.contrib import admin
+from django.core.paginator import Paginator
from django.db.models import Q, QuerySet
-from django.http import HttpRequest
+from django.http import HttpRequest, HttpResponse
from django.urls import reverse
from django.utils.html import format_html
from django.utils.safestring import SafeString
-from products.warehouse_sources.backend.models import ExternalDataSource
+from products.warehouse_sources.backend.models import ExternalDataSchema, ExternalDataSource
type _FilterChoice = tuple[str, str]
@@ -54,6 +57,7 @@ class ExternalDataSourceAdmin(admin.ModelAdmin):
actions = None
show_full_result_count = False
ordering = ("-id",)
+ change_form_template = "admin/data_warehouse/externaldatasource/change_form.html"
list_display = (
"id",
"credential_kind",
@@ -109,6 +113,8 @@ class ExternalDataSourceAdmin(admin.ModelAdmin):
)
fields = readonly_fields
+ SCHEMAS_PER_PAGE = 50
+
def get_queryset(self, request: HttpRequest) -> QuerySet[ExternalDataSource]:
return (
super()
@@ -118,6 +124,34 @@ class ExternalDataSourceAdmin(admin.ModelAdmin):
.defer("job_inputs")
)
+ def change_view(
+ self,
+ request: HttpRequest,
+ object_id: str,
+ form_url: str = "",
+ extra_context: dict[str, Any] | None = None,
+ ) -> HttpResponse:
+ extra_context = extra_context or {}
+ source = self.get_object(request, object_id)
+ if source is not None:
+ # A relational source can carry hundreds of schemas, so the list is paginated
+ # instead of rendered as an admin inline, which loads every row at once.
+ schemas = (
+ ExternalDataSchema.objects.filter(source_id=source.pk)
+ .select_related("table")
+ .defer("sync_type_config", "enabled_columns", "row_filters")
+ .order_by("name", "id")
+ )
+ extra_context["schema_page"] = Paginator(schemas, self.SCHEMAS_PER_PAGE).get_page(request.GET.get("page"))
+ # Django admin puts `_changelist_filters` on the change page URL to remember where the
+ # user came from. A bare `?page=` link drops it, so the breadcrumb back to the
+ # changelist loses the filtered position. Carry the rest of the query string over.
+ page_params = request.GET.copy()
+ page_params.pop("page", None)
+ query = page_params.urlencode()
+ extra_context["page_link_prefix"] = f"?{query}&page=" if query else "?page="
+ return super().change_view(request, object_id, form_url, extra_context=extra_context)
+
def has_add_permission(self, request: HttpRequest) -> bool:
return False
diff --git a/products/warehouse_sources/backend/tests/test_external_data_source_admin.py b/products/warehouse_sources/backend/tests/test_external_data_source_admin.py
index f8e1db4a064f..30135ce6eba8 100644
--- a/products/warehouse_sources/backend/tests/test_external_data_source_admin.py
+++ b/products/warehouse_sources/backend/tests/test_external_data_source_admin.py
@@ -9,7 +9,7 @@ from django.contrib.admin import ModelAdmin
from django.urls import reverse
from products.warehouse_sources.backend.admin.external_data_source_admin import ExternalDataSourceAdmin
-from products.warehouse_sources.backend.models import ExternalDataSource
+from products.warehouse_sources.backend.models import ExternalDataSchema, ExternalDataSource
from products.warehouse_sources.backend.types import ExternalDataSourceType
@@ -100,6 +100,35 @@ class TestExternalDataSourceAdmin(BaseTest):
assert b"metadata-secret-sentinel" not in response.content
assert "job_inputs" in response.context["original"].get_deferred_fields()
+ def test_change_view_lists_only_this_sources_schemas(self) -> None:
+ source = self._source(credential_kind="project_reader")
+ other_source = self._source(credential_kind="project_reader")
+ schema = ExternalDataSchema.objects.create(team_id=self.team.pk, source=source, name="public.users")
+ ExternalDataSchema.objects.create(team_id=self.team.pk, source=other_source, name="public.orders")
+ self.client.force_login(self.user)
+
+ response = self.client.get(reverse("admin:warehouse_sources_externaldatasource_change", args=[source.pk]))
+
+ assert response.status_code == 200
+ assert [listed.pk for listed in response.context["schema_page"].object_list] == [schema.pk]
+ schema_url = reverse("admin:warehouse_sources_externaldataschema_change", args=[schema.pk])
+ assert schema_url.encode() in response.content
+
+ def test_pagination_links_keep_changelist_filters(self) -> None:
+ source = self._source(credential_kind="project_reader")
+ for name in ("public.users", "public.orders"):
+ ExternalDataSchema.objects.create(team_id=self.team.pk, source=source, name=name)
+ self.client.force_login(self.user)
+
+ with patch.object(ExternalDataSourceAdmin, "SCHEMAS_PER_PAGE", 1):
+ response = self.client.get(
+ reverse("admin:warehouse_sources_externaldatasource_change", args=[source.pk]),
+ {"_changelist_filters": "source_type=Postgres"},
+ )
+
+ assert response.status_code == 200
+ assert b"_changelist_filters=source_type%3DPostgres&amp;page=2" in response.content
+
def test_credential_kind_filter_uses_connection_metadata(self) -> None:
project_reader = self._source(credential_kind="project_reader")
dynamic = self._source(credential_kind="duckgres_service")