Skip to content

Fix SQLAlchemy "index ix_users_email already exists" error in CI#12

Draft
Copilot wants to merge 6 commits into
mainfrom
copilot/fix-test-database-setup
Draft

Fix SQLAlchemy "index ix_users_email already exists" error in CI#12
Copilot wants to merge 6 commits into
mainfrom
copilot/fix-test-database-setup

Conversation

Copilot AI commented Jan 27, 2026

Copy link
Copy Markdown

Pull Request

Description

CI test runs fail with sqlalchemy.exc.OperationalError: index ix_users_email already exists because the PostgreSQL test database persists between runs. Indexes created in previous runs conflict with subsequent create_all() calls.

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Code refactoring
  • Performance improvement
  • Test coverage improvement

Related Issues

Changes Made

tests/conftest.py (new)

  • Pytest session hooks (pytest_sessionstart, pytest_sessionfinish) drop all tables before/after test sessions
  • Skips cleanup for in-memory SQLite (:memory:) to avoid overhead
  • Provides informative logging for persistent database cleanup

tests/test_database_integration.py

  • test_db fixture now respects DATABASE_URL env var (PostgreSQL in CI, SQLite locally)
  • Explicit drop_all() before create_all() in fixture setup/teardown
  • Engine configuration handles both SQLite and PostgreSQL properly

src/database.py

  • Added reset_db() utility for manual database reset with logging

Testing

  • Unit tests pass locally
  • Integration tests pass locally
  • Manual testing completed
  • Docker build succeeds

Test Commands

# Local (SQLite)
pytest tests/test_database_integration.py -v

# CI simulation (PostgreSQL)
DATABASE_URL=postgresql://user:pass@localhost:5432/testdb pytest tests/test_database_integration.py -v

Screenshots

N/A

Checklist

  • My code follows the project's code style
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published

Additional Notes

The fix operates at two levels:

  1. Session-level: conftest.py hooks ensure clean state between CI runs
  2. Function-level: test_db fixture ensures clean state between individual tests

Example of the cleanup logic:

def _cleanup_database(test_db_url, phase="session"):
    # Skip in-memory databases
    if not test_db_url.startswith('sqlite:///:memory:'):
        engine = _create_cleanup_engine(test_db_url)
        Base.metadata.drop_all(bind=engine)
        engine.dispose()

CodeQL security scan: 0 alerts

Deployment Notes

  • Database migrations required
  • Environment variables added/changed
  • Configuration changes needed
  • Dependencies updated

Reviewer Notes:

Original prompt

The failure is caused by the error:

sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) index ix_users_email already exists

This indicates your test suite (using SQLite) tries to create the unique index "ix_users_email" on the "users" table multiple times. This typically happens when test database schema setup doesn't properly drop and recreate tables/indexes between tests, or model definitions/migrations are being applied repeatedly.

Solution:

  1. Ensure Test Database Cleanup

    • Before each test or test class, drop all tables or run a teardown so the database is fresh.

    • With SQLAlchemy, you can do (example in pytest fixture):

      import pytest
      from sqlalchemy import create_engine, MetaData
      from sqlalchemy.orm import sessionmaker
      
      @pytest.fixture(autouse=True, scope='function')
      def setup_and_teardown_db():
          engine = create_engine('sqlite:///:memory:')
          # metadata is your SQLAlchemy MetaData object
          metadata.create_all(engine)
          yield
          metadata.drop_all(engine)
    • Alternatively, if using file-based SQLite file, delete the DB file before each test run.

  2. Do Not Reapply Models/Indexes If Already Exists

    • If you manage schema manually, guard index creation logic:

      from sqlalchemy import inspect
      
      def create_indexes(engine, metadata):
          inspector = inspect(engine)
          if 'ix_users_email' not in [i['name'] for i in inspector.get_indexes('users')]:
              # code to create index
  3. Verify Test Setup

    • Double-check any migration/initialization code to ensure it doesn’t create indexes multiple times.

Code Fix Example:
If you use Base.metadata.create_all(engine) in tests, be sure to pair it with Base.metadata.drop_all(engine) before re-creating to ensure a clean state:

# At the beginning of your test setup
Base.metadata.drop_all(engine)
Base.metadata.create_all(engine)

Summary:

  • Clean up the test DB schema (drop tables) before creating, or use an in-memory SQLite DB initialized once per test.
  • Avoid creating indexes/tables repeatedly without cleanup.

Making these changes should resolve the repeated “index ... already exists” errors and fix your CI job.


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

@netlify

netlify Bot commented Jan 27, 2026

Copy link
Copy Markdown

Deploy Preview for spectacular-strudel-d144b2 failed.

Name Link
🔨 Latest commit bfdcddc
🔍 Latest deploy log https://app.netlify.com/projects/spectacular-strudel-d144b2/deploys/6978d971185d7a00086fd449

Copilot AI and others added 5 commits January 27, 2026 15:21
…ite and PostgreSQL

Co-authored-by: Hellinferno <142572627+Hellinferno@users.noreply.github.com>
Co-authored-by: Hellinferno <142572627+Hellinferno@users.noreply.github.com>
Co-authored-by: Hellinferno <142572627+Hellinferno@users.noreply.github.com>
Co-authored-by: Hellinferno <142572627+Hellinferno@users.noreply.github.com>
…ation

Co-authored-by: Hellinferno <142572627+Hellinferno@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix test suite to clean up database before running tests Fix SQLAlchemy "index ix_users_email already exists" error in CI Jan 27, 2026
Copilot AI requested a review from Hellinferno January 27, 2026 15:30
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.

2 participants