Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
Sequel.migration do
no_transaction # required for concurrently option on postgres

up do
if database_type == :postgres
VCAP::Migration.with_concurrent_timeout(self) do
add_index :events, %i[type created_at id],
name: :events_type_created_at_id_index,
if_not_exists: true,
concurrently: true
end
else
alter_table(:events) do
# rubocop:disable Sequel/ConcurrentIndex
unless @db.indexes(:events).key?(:events_type_created_at_id_index)
add_index %i[type created_at id],
name: :events_type_created_at_id_index
end
# rubocop:enable Sequel/ConcurrentIndex
end
end
end

down do
if database_type == :postgres
VCAP::Migration.with_concurrent_timeout(self) do
drop_index :events, nil,
name: :events_type_created_at_id_index,
if_exists: true,
concurrently: true
end
else
alter_table(:events) do
# rubocop:disable Sequel/ConcurrentIndex
drop_index nil, name: :events_type_created_at_id_index if @db.indexes(:events).key?(:events_type_created_at_id_index)
# rubocop:enable Sequel/ConcurrentIndex
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
require 'spec_helper'
require 'migrations/helpers/migration_shared_context'

RSpec.describe 'migration to add type/created_at composite index to events table', isolation: :truncation, type: :migration do
include_context 'migration' do
let(:migration_filename) { '20261124120000_add_type_created_at_index_to_events.rb' }
end

describe 'events table' do
it 'adds the composite index and handles idempotency gracefully' do
# Before migration: composite index should not exist
expect(db.indexes(:events)).not_to include(:events_type_created_at_id_index)

# Test up migration
expect { Sequel::Migrator.run(db, migrations_path, target: current_migration_index, allow_missing_migration_files: true) }.not_to raise_error

expect(db.indexes(:events)).to include(:events_type_created_at_id_index)

# Verify index column order
expect(db.indexes(:events)[:events_type_created_at_id_index][:columns]).to eq(%i[type created_at id])

# Test up migration idempotency: running again should not fail
expect { Sequel::Migrator.run(db, migrations_path, target: current_migration_index, allow_missing_migration_files: true) }.not_to raise_error
expect(db.indexes(:events)).to include(:events_type_created_at_id_index)

# Test down migration
expect { Sequel::Migrator.run(db, migrations_path, target: current_migration_index - 1, allow_missing_migration_files: true) }.not_to raise_error
expect(db.indexes(:events)).not_to include(:events_type_created_at_id_index)

# Test down migration idempotency: running again should not fail
expect { Sequel::Migrator.run(db, migrations_path, target: current_migration_index - 1, allow_missing_migration_files: true) }.not_to raise_error
expect(db.indexes(:events)).not_to include(:events_type_created_at_id_index)
end
end
end
39 changes: 39 additions & 0 deletions spec/request/events_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,45 @@
end
end

context 'filtering by type and created_at range' do
# Mirrors the GET /v3/audit_events?types=...&created_ats[gt]=...&created_ats[lt]=...
# access pattern that the (type, created_at, id) composite index supports.
let!(:in_window_event) do
create(:event, type: 'audit.app.update', actee: app_model.guid,
space_guid: space.guid, organization_guid: org.guid,
created_at: Time.utc(2026, 11, 4, 12, 0, 0))
end
let!(:before_window_event) do
create(:event, type: 'audit.app.update', actee: app_model.guid,
space_guid: space.guid, organization_guid: org.guid,
created_at: Time.utc(2026, 11, 1, 12, 0, 0))
end
let!(:after_window_event) do
create(:event, type: 'audit.app.update', actee: app_model.guid,
space_guid: space.guid, organization_guid: org.guid,
created_at: Time.utc(2026, 11, 10, 12, 0, 0))
end
# Same window, different type: must be excluded by the type filter.
let!(:other_type_in_window_event) do
create(:event, type: 'audit.app.restart', actee: app_model.guid,
space_guid: space.guid, organization_guid: org.guid,
created_at: Time.utc(2026, 11, 4, 13, 0, 0))
end

it 'returns only events of the given type within the created_at window' do
get '/v3/audit_events?types=audit.app.update' \
'&created_ats[gt]=2026-11-03T00:00:00Z' \
'&created_ats[lt]=2026-11-05T00:00:00Z', nil, admin_header

expect(last_response).to have_status_code(200)
returned_guids = parsed_response['resources'].pluck('guid')
expect(returned_guids).to contain_exactly(in_window_event.guid)
expect(returned_guids).not_to include(before_window_event.guid)
expect(returned_guids).not_to include(after_window_event.guid)
expect(returned_guids).not_to include(other_type_in_window_event.guid)
end
end

context 'filtering by target_guid' do
it 'returns filtered events' do
get "/v3/audit_events?target_guids=#{app_model.guid}", nil, admin_header
Expand Down
Loading