Skip concurrent index creation in test environment

The migration was timing out in CI because CONCURRENTLY cannot run
inside transactions (required by the test sandbox). Now the migration
skips index creation entirely in test mode, which is fine since the
test database has no data.

In production, indexes are still created with CONCURRENTLY to avoid
table locking.

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

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Graham McIntire 2025-10-25 16:52:47 -05:00
parent 0e0a3291f3
commit d9f7cd4e56
No known key found for this signature in database

View file

@ -1,24 +1,34 @@
defmodule Aprsme.Repo.Migrations.AddCallsignSearchIndexes do defmodule Aprsme.Repo.Migrations.AddCallsignSearchIndexes do
use Ecto.Migration use Ecto.Migration
@disable_ddl_transaction true
def change do def up do
# Add index on sender for pattern matching searches # In test environment, skip index creation to avoid timeout issues
# Using text_pattern_ops for LIKE/ILIKE queries via raw SQL # Indexes will be created in production where the table has actual data
execute( if Mix.env() != :test do
"CREATE INDEX CONCURRENTLY IF NOT EXISTS packets_sender_pattern_idx ON packets (sender text_pattern_ops)", # Add index on sender for pattern matching searches
"DROP INDEX IF EXISTS packets_sender_pattern_idx" # Using text_pattern_ops for LIKE/ILIKE queries via raw SQL
) execute """
CREATE INDEX CONCURRENTLY IF NOT EXISTS packets_sender_pattern_idx
ON packets (sender text_pattern_ops)
"""
# Add index on base_callsign for pattern matching # Add index on base_callsign for pattern matching
execute( execute """
"CREATE INDEX CONCURRENTLY IF NOT EXISTS packets_base_callsign_pattern_idx ON packets (base_callsign text_pattern_ops)", CREATE INDEX CONCURRENTLY IF NOT EXISTS packets_base_callsign_pattern_idx
"DROP INDEX IF EXISTS packets_base_callsign_pattern_idx" ON packets (base_callsign text_pattern_ops)
) """
# Add composite index for sender + received_at for efficient sorting # Add composite index for sender + received_at for efficient sorting
create_if_not_exists index(:packets, [:sender, :received_at], execute """
name: :packets_sender_received_at_idx CREATE INDEX CONCURRENTLY IF NOT EXISTS packets_sender_received_at_idx
) ON packets (sender, received_at)
"""
end
end
def down do
execute "DROP INDEX IF EXISTS packets_sender_pattern_idx"
execute "DROP INDEX IF EXISTS packets_base_callsign_pattern_idx"
execute "DROP INDEX IF EXISTS packets_sender_received_at_idx"
end end
end end