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.

fix(bigquery): catch jobs.create permission denial during schema discovery (#63709)

Tom Owers187311c5560c
diff --git a/posthog/temporal/data_imports/sources/bigquery/bigquery.py b/posthog/temporal/data_imports/sources/bigquery/bigquery.py
index 8bc07879ab18..d74a72836ab4 100644
--- a/posthog/temporal/data_imports/sources/bigquery/bigquery.py
+++ b/posthog/temporal/data_imports/sources/bigquery/bigquery.py
@@ -552,8 +552,11 @@ class BigQueryImplementation(SQLSourceImplementation[BigQuerySourceConfig, bigqu
schema_list: dict[str, list[tuple[str, str, bool]]] = collections.defaultdict(list)
try:
- # Submitting the job triggers the lazy service-account token refresh, so an
- # auth failure surfaces here rather than at `result()`.
+ # `conn.query()` eagerly creates the BigQuery job (POST .../jobs) and triggers the
+ # lazy service-account token refresh, so a `Forbidden` (e.g. missing
+ # `bigquery.jobs.create`) or auth failure surfaces here rather than at `result()`.
+ # Both calls must sit inside the try so a permission-denied account degrades to
+ # "no new schemas" instead of crashing schema discovery.
query = conn.query(
f"SELECT table_name, column_name, data_type, is_nullable FROM `{_resolve_dataset_id(config)}.INFORMATION_SCHEMA.COLUMNS` ORDER BY table_name ASC",
project=_resolve_query_project(config),
diff --git a/posthog/temporal/data_imports/sources/bigquery/tests/test_source.py b/posthog/temporal/data_imports/sources/bigquery/tests/test_source.py
index 3b4310500343..1472ea0b62fb 100644
--- a/posthog/temporal/data_imports/sources/bigquery/tests/test_source.py
+++ b/posthog/temporal/data_imports/sources/bigquery/tests/test_source.py
@@ -86,6 +86,19 @@ def test_bigquery_get_columns_filters_existing_destination_tables():
assert list(columns.keys()) == ["table"]
+def test_bigquery_get_columns_returns_empty_when_job_create_forbidden():
+ """A service account without `bigquery.jobs.create` raises `Forbidden` from
+ `client.query()` (which eagerly creates the job), not from `result()`. That must
+ degrade to no schemas rather than crashing schema discovery."""
+ fake_client = mock.MagicMock()
+ fake_client.query.side_effect = Forbidden(
+ "Access Denied: Project p: User does not have bigquery.jobs.create permission in project p."
+ )
+
+ columns = BigQueryImplementation().get_columns(fake_client, _make_config(), names=None)
+ assert columns == {}
+
+
@pytest.mark.parametrize(
"error_message,expected_type,is_token_refresh",
[