Skip to content

fix(migration): exa env var into db#6366

Merged
jessicasingh7 merged 5 commits intomainfrom
env-var-migration
Nov 21, 2025
Merged

fix(migration): exa env var into db#6366
jessicasingh7 merged 5 commits intomainfrom
env-var-migration

Conversation

@jessicasingh7
Copy link
Copy Markdown
Contributor

@jessicasingh7 jessicasingh7 commented Nov 20, 2025

Description

[Provide a brief description of the changes in this PR]

How Has This Been Tested?

[Describe the tests you ran to verify your changes]

Additional Options

  • [Optional] Override Linear Check

Summary by cubic

Seeds the Exa internet search provider from the EXA_API_KEY environment variable during migration and encrypts the API key. Activates it only if no provider is active; no-op if the env var is missing or the provider already exists.

  • Migration
    • Set EXA_API_KEY before running migrations.
    • Inserts provider named “Exa” with type “exa.”
    • Skips insert if it already exists; safe to rerun.
    • Downgrade is a no-op to preserve existing Exa provider rows.

Written for commit 4e29a17. Summary will update automatically on new commits.

@jessicasingh7 jessicasingh7 requested a review from a team as a code owner November 20, 2025 21:22
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps bot commented Nov 20, 2025

Greptile Overview

Greptile Summary

Adds database migration to seed Exa search provider from EXA_API_KEY environment variable, encrypting the API key and setting provider as active only if no other active provider exists.

Critical Issue:

  • Table definition missing required time_created and time_updated timestamp columns that have NOT NULL constraints in the actual schema

Confidence Score: 1/5

  • This PR contains a critical bug that will cause the migration to fail at runtime
  • The table definition in _get_internet_search_table is missing time_created and time_updated columns that have NOT NULL constraints in the actual schema. This will cause the insert operation to fail because SQLAlchemy won't apply server defaults when using manually-defined Table objects for data manipulation.
  • The migration file requires immediate attention to add missing timestamp columns to the table definition

Important Files Changed

File Analysis

Filename Score Overview
backend/alembic/versions/3c9a65f1207f_seed_exa_provider_from_env.py 2/5 Adds migration to seed Exa provider from EXA_API_KEY env var, but missing time_created and time_updated columns in table definition

Sequence Diagram

sequenceDiagram
    participant Migration as Migration Script
    participant Env as Environment
    participant DB as Database
    participant Encrypt as Encryption Util

    Migration->>Env: Get EXA_API_KEY
    alt No API key found
        Env-->>Migration: None
        Migration->>Migration: Return early
    else API key exists
        Env-->>Migration: API key value
        Migration->>DB: Get bind connection
        Migration->>DB: Check for existing provider
        alt Provider already exists
            DB-->>Migration: Existing record
            Migration->>Migration: Return early
        else No existing provider
            DB-->>Migration: None
            Migration->>Encrypt: encrypt_string_to_bytes(api_key)
            Encrypt-->>Migration: Encrypted key bytes
            Migration->>DB: Check for active providers
            alt Active provider exists
                DB-->>Migration: Active record found
                Migration->>DB: Insert (is_active=False)
            else No active provider
                DB-->>Migration: No active records
                Migration->>DB: Insert (is_active=True)
            end
            DB-->>Migration: Insert complete
        end
    end
Loading

Copy link
Copy Markdown
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

1 file reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

Comment on lines +28 to +38
def _get_internet_search_table(metadata: sa.MetaData) -> sa.Table:
return sa.Table(
"internet_search_provider",
metadata,
sa.Column("id", sa.Integer, primary_key=True),
sa.Column("name", sa.String),
sa.Column("provider_type", sa.String),
sa.Column("api_key", sa.LargeBinary),
sa.Column("config", postgresql.JSONB),
sa.Column("is_active", sa.Boolean),
)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

logic: table definition missing time_created and time_updated columns that exist in actual schema (line 33-42 in migration 1f2a3b4c5d6e). These columns have NOT NULL constraints with server defaults. When manually defining sa.Table for data manipulation, include all NOT NULL columns to ensure insert succeeds.

Prompt To Fix With AI
This is a comment left during a code review.
Path: backend/alembic/versions/3c9a65f1207f_seed_exa_provider_from_env.py
Line: 28:38

Comment:
**logic:** table definition missing `time_created` and `time_updated` columns that exist in actual schema (line 33-42 in migration `1f2a3b4c5d6e`). These columns have NOT NULL constraints with server defaults. When manually defining sa.Table for data manipulation, include all NOT NULL columns to ensure insert succeeds.

How can I resolve this? If you propose a fix, please make it concise.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We need to fix this one.

Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

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

No issues found across 1 file

Copy link
Copy Markdown
Contributor

@justin-tahara justin-tahara left a comment

Choose a reason for hiding this comment

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

Make sure to test this and ensure that it works.

Comment on lines +28 to +38
def _get_internet_search_table(metadata: sa.MetaData) -> sa.Table:
return sa.Table(
"internet_search_provider",
metadata,
sa.Column("id", sa.Integer, primary_key=True),
sa.Column("name", sa.String),
sa.Column("provider_type", sa.String),
sa.Column("api_key", sa.LargeBinary),
sa.Column("config", postgresql.JSONB),
sa.Column("is_active", sa.Boolean),
)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We need to fix this one.

if existing:
return

encrypted_key = encrypt_string_to_bytes(exa_api_key)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is this how you store the key in the DB?

@jessicasingh7 jessicasingh7 changed the title fix(migration):exa env var into db fix(migration): exa env var into db Nov 20, 2025
@jessicasingh7 jessicasingh7 added this pull request to the merge queue Nov 21, 2025
Merged via the queue into main with commit 092dbeb Nov 21, 2025
68 checks passed
@jessicasingh7 jessicasingh7 deleted the env-var-migration branch November 21, 2025 00:26
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.

3 participants