quantum-ai-v3/agents/migrations/0005_auto_20250804_1045.py
Claude f10a097d9c 🔧 Fix Railway migration DuplicateColumn error with smart column check
## Migration Fix for Railway Database
- Railway PostgreSQL already has access_url_name/display_url_name columns
- Previous migration tried to add existing columns → DuplicateColumn error
- New migration checks if columns exist before adding them

## Smart Migration Logic
 Check information_schema for existing columns
 Add columns only if they don't exist
 Skip if columns already present (Railway case)
 Works for both fresh and existing databases

## Error Fixed
 Was: column 'access_url_name' of relation 'agents_agent' already exists
 Now: Migration succeeds regardless of existing schema state

## Result
- Railway deployment will complete successfully
- populate_agents will run and create all 5 agents
- Marketplace will show agents again

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-08-04 16:20:52 +05:30

51 lines
1.7 KiB
Python

# Generated by Django 5.2.4 on 2025-08-04 10:45
from django.db import migrations, models
def check_and_add_fields(apps, schema_editor):
"""Add fields only if they don't already exist"""
db_alias = schema_editor.connection.alias
# Check if columns already exist in the database
with schema_editor.connection.cursor() as cursor:
cursor.execute("""
SELECT column_name
FROM information_schema.columns
WHERE table_name = 'agents_agent'
AND column_name IN ('access_url_name', 'display_url_name')
""")
existing_columns = [row[0] for row in cursor.fetchall()]
# Add access_url_name if it doesn't exist
if 'access_url_name' not in existing_columns:
cursor.execute("""
ALTER TABLE agents_agent
ADD COLUMN access_url_name VARCHAR(100) DEFAULT '' NOT NULL
""")
# Add display_url_name if it doesn't exist
if 'display_url_name' not in existing_columns:
cursor.execute("""
ALTER TABLE agents_agent
ADD COLUMN display_url_name VARCHAR(100) DEFAULT '' NOT NULL
""")
def reverse_check_and_add_fields(apps, schema_editor):
"""Remove fields if they exist"""
with schema_editor.connection.cursor() as cursor:
cursor.execute("ALTER TABLE agents_agent DROP COLUMN IF EXISTS access_url_name")
cursor.execute("ALTER TABLE agents_agent DROP COLUMN IF EXISTS display_url_name")
class Migration(migrations.Migration):
dependencies = [
("agents", "0004_add_message_limit"),
]
operations = [
migrations.RunPython(check_and_add_fields, reverse_check_and_add_fields),
]