Skip to content

fix(db): migrate failover_db queries to use model_name instead of dropped model_id column#1023

Open
vdimarco wants to merge 1 commit intomainfrom
fix/failover-db-model-name-migration-v2
Open

fix(db): migrate failover_db queries to use model_name instead of dropped model_id column#1023
vdimarco wants to merge 1 commit intomainfrom
fix/failover-db-model-name-migration-v2

Conversation

@vdimarco
Copy link
Contributor

@vdimarco vdimarco commented Feb 2, 2026

Summary

Fixes failover database queries that still reference the dropped model_id column and removed pricing columns from the models table.

This PR addresses the outstanding failover_db.py fixes from PR #1018 which were not merged when that PR was closed.

Sentry Issue Fixed

Root Cause

Migrations removed the model_id column and pricing columns from the models table:

  • Migration 20260131000002: Dropped model_id column (use model_name instead)
  • Migration 20260121000003: Moved pricing to model_pricing table

However, src/db/failover_db.py still contained queries using these dropped columns.

Changes Made

Modified Files (1)

src/db/failover_db.py

Functions Updated:

  • get_providers_for_model():

    • Changed filter from .eq("model_id", model_id).eq("model_name", model_id)
    • Removed direct pricing column selects (were dropped from models table)
    • Added model_pricing relationship JOIN for pricing data
    • Handles PostgREST list response for model_pricing
  • get_provider_model_id():

    • Changed filter from .eq("model_id", ...).eq("model_name", ...)
  • check_model_available_on_provider():

    • Changed filter from .eq("model_id", ...).eq("model_name", ...)

New Test Files (1)

tests/db/test_failover_db_model_name_fix.py

  • Tests for all modified functions
  • Validates correct column usage
  • Tests pricing extraction from model_pricing relationship
  • Tests list handling for PostgREST responses

Test plan

  • Syntax validation passed
  • Ruff linting passed
  • Module imports correctly
  • CI checks pass
  • Verify in staging: failover routing works correctly
  • Monitor Sentry for error count reduction

🤖 Generated with Claude Code

Greptile Overview

Greptile Summary

This PR completes the migration from the dropped models.model_id column to model_name for failover database queries, fixing a PostgreSQL error (GATEWAYZ-BACKEND-86) that was occurring in production.

Key Changes:

  • Updated get_providers_for_model() to filter by model_name instead of model_id and retrieve pricing from the model_pricing relationship table
  • Updated get_provider_model_id() to query using model_name
  • Updated check_model_available_on_provider() to filter by model_name
  • Added proper handling for PostgREST's list response format for the model_pricing relationship
  • Added comprehensive test coverage validating the migration

Context:
This follows migration 20260131000002 which dropped the redundant model_id column, and migration 20260121000003 which moved pricing data to a separate model_pricing table. PR #1021 fixed similar issues in pricing.py and pricing_lookup.py, while this PR addresses the remaining references in failover_db.py.

Confidence Score: 5/5

  • This PR is safe to merge with no issues found
  • All changes correctly implement the migration from model_id to model_name column, properly handle the model_pricing relationship JOIN, include defensive coding for missing data, and have comprehensive test coverage
  • No files require special attention

Important Files Changed

Filename Overview
src/db/failover_db.py Correctly migrated three functions from model_id column to model_name column and added model_pricing relationship JOIN for pricing data
tests/db/test_failover_db_model_name_fix.py New test file with comprehensive tests validating model_name column usage and model_pricing relationship handling

Sequence Diagram

sequenceDiagram
    participant Client as Failover Service
    participant DB as failover_db.py
    participant Supabase as Supabase/PostgREST
    participant Models as models table
    participant Pricing as model_pricing table
    participant Providers as providers table

    Note over Client,Providers: Failover Query Flow (After Migration)

    Client->>DB: get_providers_for_model("gpt-4")
    DB->>Supabase: SELECT with JOINs
    Note right of DB: Query uses model_name<br/>(not dropped model_id)
    
    Supabase->>Models: .eq("model_name", "gpt-4")
    Models-->>Supabase: Returns model rows
    
    Supabase->>Pricing: JOIN model_pricing (relationship)
    Pricing-->>Supabase: Returns pricing data
    Note right of Pricing: price_per_input_token<br/>price_per_output_token<br/>price_per_image_token<br/>price_per_request
    
    Supabase->>Providers: JOIN providers!inner
    Providers-->>Supabase: Returns provider metadata
    
    Supabase-->>DB: Combined result set
    
    DB->>DB: Extract pricing from model_pricing
    Note right of DB: Handle list format<br/>from PostgREST
    
    DB->>DB: Build provider dicts
    Note right of DB: Map model_name → model_id<br/>Extract pricing fields
    
    DB->>DB: Sort by health, speed, cost
    
    DB-->>Client: Sorted provider list
    Note left of Client: Ready for failover routing
Loading

…pped model_id column

Updates all failover database queries to use the correct column references
after migrations 20260131000002 (dropped model_id) and 20260121000003
(moved pricing to model_pricing table).

Changes:
- get_providers_for_model(): Use model_name for filtering, join with
  model_pricing table for pricing data
- get_provider_model_id(): Use model_name for filtering
- check_model_available_on_provider(): Use model_name for filtering

Handles PostgREST list response for model_pricing relationship.

Fixes Sentry issue GATEWAYZ-BACKEND-86 - PostgreSQL error 42703:
"column models.model_id does not exist"

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@chatgpt-codex-connector
Copy link

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@supabase
Copy link

supabase bot commented Feb 2, 2026

This pull request has been ignored for the connected project ynleroehyrmaafkgjgmr because there are no changes detected in supabase/supabase directory. You can change this behaviour in Project Integrations Settings ↗︎.


Preview Branches by Supabase.
Learn more about Supabase Branching ↗︎.

@coderabbitai
Copy link

coderabbitai bot commented Feb 2, 2026

Warning

Rate limit exceeded

@vdimarco has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 8 minutes and 20 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/failover-db-model-name-migration-v2

Comment @coderabbitai help to get the list of available commands and usage tips.

Comment on lines 207 to 217
try:
supabase = get_supabase_client()

# Note: model_id column was dropped from models table - now using model_name
response = supabase.table("models").select(
"provider_model_id"
).eq(
"model_id", canonical_model_id
"model_name", canonical_model_id
).eq(
"providers.slug", provider_slug
).single().execute()
Copy link

Choose a reason for hiding this comment

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

Bug: The get_provider_model_id and check_model_available_on_provider functions filter on providers table columns without including providers!inner() in the select() clause, which will cause the queries to fail.
Severity: CRITICAL

Suggested Fix

Update the queries in get_provider_model_id and check_model_available_on_provider to include the providers!inner() relationship in the select() clause before applying filters on the providers table. For example, change .select("provider_model_id") to .select("provider_model_id, providers!inner(slug)").

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent.
Verify if this is a real issue. If it is, propose a fix; if not, explain why it's not
valid.

Location: src/db/failover_db.py#L207-L217

Potential issue: The functions `get_provider_model_id` and
`check_model_available_on_provider` in `src/db/failover_db.py` build PostgREST queries
that filter on columns from the related `providers` table, such as `providers.slug` and
`providers.is_active`. However, the `select()` clause in these queries omits the
required `providers!inner()` relationship. This will cause PostgREST to fail when trying
to resolve the filter columns, likely resulting in an error or an empty result set. This
will break the critical failover logic that relies on these database lookups to
determine model availability on different providers.

Did we get this right? 👍 / 👎 to inform future reviews.

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.

1 participant