Skip to content

Simplify allow_device_custom_id query - remove unnecessary COALESCE#1627

Merged
riderx merged 4 commits intomainfrom
copilot/fix-stats-broken-in-production
Feb 13, 2026
Merged

Simplify allow_device_custom_id query - remove unnecessary COALESCE#1627
riderx merged 4 commits intomainfrom
copilot/fix-stats-broken-in-production

Conversation

Copy link
Contributor

Copilot AI commented Feb 12, 2026

Summary

The /stats endpoint was failing in production when querying allow_device_custom_id. The initial fix attempted to handle replicas with COALESCE, but since the column is defined as NOT NULL DEFAULT true, we can query it directly without any workarounds.

Root cause:
The column allow_device_custom_id is always set with a default value (NOT NULL DEFAULT true in migration 20260210132811_stats_customid_guard.sql), so COALESCE and to_jsonb workarounds are unnecessary.

Fix:
Direct column access using Drizzle schema:

// Simple and clear
allow_device_custom_id: schema.apps.allow_device_custom_id

Changes:

  • supabase/functions/_backend/plugins/stats.ts - Simplified to direct column query, removed unused sql import
  • supabase/functions/_backend/utils/pg.ts - Reverted to original state (change was based on incorrect assumption)

Test plan

  • Deploy to production and verify /stats endpoint processes telemetry without SQL errors
  • Verify column is always set since it has NOT NULL DEFAULT true

Screenshots

N/A - Backend fix

Checklist

  • My code follows the code style of this project and passes bun run lint:backend && bun run lint.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • My change has adequate E2E test coverage.
  • I have tested my code manually, and I have provided steps how to reproduce my tests
Original prompt

This section details on the original issue you should resolve

<issue_title>Stats broken in production</issue_title>
<issue_description>Describe the bug/issue
We added features to allow restrict customer id set from devices stats.

And this broke totally our system of devices...

image

What we wanted is just if the feature is enabled the write is ignored that it nothing else why there is the coalese ?</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 2 commits February 12, 2026 11:56
The previous query `COALESCE((to_jsonb(apps) ->> 'allow_device_custom_id')::boolean, true)`
would fail when the column doesn't exist because:
1. to_jsonb(apps) ->> 'allow_device_custom_id' returns NULL (as text)
2. NULL::text cast to ::boolean fails with an error
3. COALESCE never gets a chance to provide the default

The fix: `COALESCE(to_jsonb(apps) ->> 'allow_device_custom_id', 'true')::boolean`
1. Provide the default 'true' string BEFORE the boolean cast
2. COALESCE returns either the column value or 'true' as text
3. Cast the resulting text to boolean

Co-authored-by: riderx <4084527+riderx@users.noreply.github.com>
Applied the same fix to the has_usage_credits query in pg.ts to prevent
potential failures when the column doesn't exist on replicas.

Co-authored-by: riderx <4084527+riderx@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix stats functionality in production system Fix COALESCE type cast ordering in replica-safe column queries Feb 12, 2026
Copilot AI requested a review from riderx February 12, 2026 11:59
@riderx
Copy link
Member

riderx commented Feb 12, 2026

@copilot revert the changes of stats endpoint made in 5a4828b

Then the value jn app is boolean and always set so no need of coalescing

@riderx
Copy link
Member

riderx commented Feb 12, 2026

@copilot In stats what need to be done.
If option is true allow to save the custom_id.
If false then just ignore the value

Since the column is NOT NULL DEFAULT true, we can query it directly without
the to_jsonb workaround. Reverted pg.ts change as well since that was based
on the same incorrect assumption.

Co-authored-by: riderx <4084527+riderx@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Feb 12, 2026

@copilot revert the changes of stats endpoint made in 5a4828b

Then the value jn app is boolean and always set so no need of coa...

Reverted the COALESCE approach and simplified to direct column query since allow_device_custom_id is NOT NULL DEFAULT true. Fixed in commit af6c362.

Copilot AI changed the title Fix COALESCE type cast ordering in replica-safe column queries Simplify allow_device_custom_id query - remove unnecessary COALESCE Feb 12, 2026
Copy link
Contributor Author

Copilot AI commented Feb 12, 2026

@copilot In stats what need to be done.
If option is true allow to save the custom_id.
If false then just ignore the value

The logic is already correct:

  • Line 99: Queries allow_device_custom_id flag from database
  • Lines 105-107, 122-124, 137-139: If flag is FALSE and custom_id is provided, sets device.custom_id = undefined (ignores it)

When allow_device_custom_id is TRUE → custom_id is saved
When allow_device_custom_id is FALSE → custom_id is ignored (set to undefined)

The fix in commit af6c362 resolved the query error, so the feature now works as designed.

@sonarqubecloud
Copy link

@riderx riderx marked this pull request as ready for review February 13, 2026 18:18
Copilot AI review requested due to automatic review settings February 13, 2026 18:18
@riderx riderx merged commit 384e02f into main Feb 13, 2026
14 checks passed
@riderx riderx deleted the copilot/fix-stats-broken-in-production branch February 13, 2026 18:18
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a production issue in the /stats endpoint by simplifying the query for the allow_device_custom_id column. The column was added with NOT NULL DEFAULT true constraints in migration 20260210132811_stats_customid_guard.sql, making COALESCE and to_jsonb workarounds unnecessary. The fix removes these workarounds in favor of direct schema column access via Drizzle ORM.

Changes:

  • Simplified allow_device_custom_id query in stats.ts from COALESCE/to_jsonb pattern to direct schema column access
  • Removed unused sql import from drizzle-orm
Comments suppressed due to low confidence (1)

supabase/functions/_backend/plugins/stats.ts:171

  • This use of variable 'old_version_name' always evaluates to true.
        statsActions.push({ action: 'uninstall', versionName: old_version_name ?? 'unknown' })

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Stats broken in production

2 participants