AutoGPT/autogpt_platform/backend/migrations
Zamil Majdy f81d7e6a56
feat(backend): Add Missing FK indexes and remove unused & redundant indexes (#10412)
### Changes 🏗️

This PR optimizes database performance by adding missing foreign key
indexes, removing unused/redundant indexes, cleaning up all legacy
untracked indexes, and adding performance indexes for materialized views
to achieve 100% optimized database indexing.

**Foreign Key Indexes Added:**
- `AgentGraph`: `[forkedFromId, forkedFromVersion]` - For fork
relationship queries
- `AgentGraphExecution`: `[agentPresetId]` - For preset-based execution
filtering
- `AgentNodeExecution`: `[agentNodeId]` - For node execution lookups
- `AgentNodeExecutionInputOutput`: `[agentPresetId]` - For preset
input/output queries
- `AgentPreset`: `[agentGraphId, agentGraphVersion]` & `[webhookId]` -
For graph and webhook lookups
- `LibraryAgent`: `[agentGraphId, agentGraphVersion]` & `[creatorId]` -
For agent and creator queries
- `StoreListing`: `[agentGraphId, agentGraphVersion]` - For marketplace
agent lookups
- `StoreListingReview`: `[reviewByUserId]` - For user review queries

**Unused/Redundant Indexes Removed:**
- `User.email` - Unused index identified by linter
- `AnalyticsMetrics.userId` - Unused index causing write overhead
- `AnalyticsDetails.type` - Redundant (covered by composite `[userId,
type]`)
- `APIKey.key`, `APIKey.status` - Unused indexes
- Named index `"analyticsDetails"` - Converted to standard composite
index

**All Legacy Untracked Indexes Removed:**
- `idx_store_listing_version_status` - Redundant with Prisma composite
index
- `idx_slv_agent` - Redundant with Prisma-managed `[agentGraphId,
agentGraphVersion]`
- `idx_store_listing_version_approved_listing` - Redundant with unique
constraint
- `StoreListing_agentId_owningUserId_idx` - Legacy index superseded by
current strategy
- `StoreListing_isDeleted_isApproved_idx` - Replaced by optimized
composite index
- `StoreListing_isDeleted_idx` - Redundant with composite index
- `StoreListingVersion_agentId_agentVersion_isDeleted_idx` - Legacy
index replaced
- `idx_store_listing_approved` - Redundant with existing `[owningUserId,
slug]` unique constraint and `[isDeleted, hasApprovedVersion]` index
- `idx_slv_categories_gin` - Specialized array search index removed (can
be re-added if category filtering is implemented)
- `idx_profile_user` - Duplicate of Prisma-managed `Profile_userId_idx`

**Materialized View Performance Indexes Added:**
- `idx_mv_review_stats_rating` on `mv_review_stats(avg_rating DESC)` -
Optimizes sorting agents by rating
- `idx_mv_review_stats_count` on `mv_review_stats(review_count DESC)` -
Optimizes sorting agents by review count

**Result: 100% Optimized Database Indexing**
- All database indexes are now defined and managed through Prisma schema
- No more untracked indexes requiring manual SQL maintenance
- Added performance indexes for materialized views used by marketplace
views
- Improved query performance for agent sorting and filtering
- Enhanced maintainability and consistency across environments

**Schema Comments Updated:**
- Removed all references to dropped untracked indexes
- Simplified documentation to reflect Prisma-only approach for regular
tables
- Added comprehensive documentation for materialized view indexes and
their purposes
- Maintained documentation for materialized view refresh strategy

### Checklist 📋

#### For code changes:
- [x] I have clearly listed my changes in the PR description
- [x] I have made a test plan
- [x] I have tested my changes according to the test plan:
  - [x] Schema changes compile successfully with Prisma
- [x] Migration adds required FK indexes and materialized view
performance indexes
- [x] Migration drops all legacy indexes and redundant untracked indexes
  - [x] All pre-commit hooks pass (linting, formatting, type checking)
  - [x] No breaking changes to existing foreign key relationships
  - [x] Verified existing Prisma indexes cover all query patterns
  - [x] Schema comments comprehensively document all indexing strategy
- [x] Materialized view performance indexes optimize marketplace sorting

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

---------

Co-authored-by: Swifty <craigswift13@gmail.com>
Co-authored-by: Claude <noreply@anthropic.com>
2025-07-21 09:26:46 +00:00
..
20240722143307_migrations
20240726131311_node_input_unique_constraint
20240729061216_static_input_link
20240804040801_add_subgraph
20240805115810_add_user_management
20240807123738_add_index_users
20240808095419_add_required_user_ids
20240902223334_add_stats_column
20240906155206_add_created_at_updated_at
20240914033334_user_credit
20240918163611_add_analytics_tables
20240930151406_reassign_block_ids
20241007090536_add_on_delete_platform
20241007115713_cascade_graph_deletion
20241007175111_move_oauth_creds_to_user_obj
20241007175112_add_oauth_creds_user_trigger
20241017180251_add_webhooks_and_their_relation_to_nodes
20241030014950_move_integration_creds_to_platform.User
20241030061705_encrypt_user_metadata
20241030063332_drop_all_credentials_from_constant_input
20241103133307_remove_subgraph
20241103144418_graph_exec_stats_list_to_obj
20241108170448_add_api_key_support
20241113104933_remove_scheduler
20241115170707_fix_llm_provider_credentials
20241210013740_add_indexes
20241211160646_rename_credit_model_and_add_stripe_customer
20241212141024_agent_store_v2
20241212142024_creator_featured_flag
20241212150828_agent_store_v2_views
20241230102007_update_store_agent_view
20250103143207_add_terminated_execution_status
20250105254106_migrate_brace_to_double_brace_string_format
20250107095812_preset_soft_delete
20250108084101_user_agent_to_library_agent
20250108084305_use_graph_is_active_version
20250110084611_store_index_updates
20250115213557_add_running_balance
20250115382614_reshape_transaction_metadata_column
20250115432618_add_auto_top_up_config
20250124211747_make_store_listing_version_id_unique
20250203133647_add_image_url
20250205100104_add_profile_trigger
20250205110132_add_missing_profiles
20250212215755_add_user_notifications
20250213110232_migrate_string_json
20250214092857_add_refund_request
20250214101759_add_transaction_types
20250218135013_create_library_agents_for_existing_graphs
20250220111649_add_refund_notifications
20250222014114_add_email_verified_flag_to_user_table
20250223110000_add_onboarding_model
20250227140210_fix_library_presets_relations
20250228161607_agent_graph_execution_soft_delete
20250310095931_delete_duplicate_indices
20250316095525_remove_graph_template
20250318043016_update_store_submissions_format
20250325100000_update_user_onboarding
20250407181043_refactor_store_relations
20250411130000_update_onboarding_step
20250416140000_make_arrays_not_null
20250422125822_add_forked_relation
20250507025350_execution_created_at_index
20250512104735_rename_agent_executor_node_input fix(backend): Unbreak existing Agent Executor nodes (#9928) 2025-05-12 11:41:07 +00:00
20250527091422_node_execution_indexes fix(platform): Optimise Query Indexes (#10038) 2025-05-27 09:20:36 +00:00
20250528092000_onboarding_add_runs feat(platform): Add `Run 10 agents` wallet task (#9937) 2025-05-28 07:40:52 +00:00
20250604130249_optimise_store_agent_and_creator_views perf(backend/db): Optimize StoreAgent and Creator views with database indexes and materialized views (#10084) 2025-07-10 14:57:55 +00:00
20250620000924_make_data_nullable feat(blocks): Improve SmartDecisionBlock & AIStructuredResponseGeneratorBlock (#10198) 2025-06-20 14:14:02 +00:00
20250620140815_add_preset_webhook_relation feat(platform/library): Triggered-agent support (#10167) 2025-06-24 20:28:20 +00:00
20250702224504_add_node_exec_kv_data feat(backend): implement KV data storage blocks (#10294) 2025-07-03 14:24:51 +00:00
20250721073830_add_preset_index feat(backend): Add database index for improved query performance (#10411) 2025-07-21 08:19:37 +00:00
20250721081856_add_missing_fk_indexes_remove_unused_indexes feat(backend): Add Missing FK indexes and remove unused & redundant indexes (#10412) 2025-07-21 09:26:46 +00:00
migration_lock.toml