-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ruby Migration generator for event_id index
RES Browser slowed down when displaying event overview. It turned out it was because of "Event Streams" involving full table scan on event_store_events_in_streams. Adding the index fixes the issue, obviously. Co-authored-by: Łukasz Reszke <[email protected]>
- Loading branch information
1 parent
0b6c34e
commit 5c2c367
Showing
5 changed files
with
132 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
...ecord/lib/ruby_event_store/active_record/generators/event_id_index_migration_generator.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# frozen_string_literal: true | ||
|
||
module RubyEventStore | ||
module ActiveRecord | ||
class EventIdIndexMigrationGenerator | ||
def call(migration_path) | ||
path = build_path(migration_path) | ||
write_to_file(path) | ||
path | ||
end | ||
|
||
private | ||
|
||
def absolute_path(path) | ||
File.expand_path(path, __dir__) | ||
end | ||
|
||
def migration_code | ||
migration_template.result_with_hash(migration_version: migration_version) | ||
end | ||
|
||
def migration_template | ||
ERB.new( | ||
File.read( | ||
File.join(absolute_path("./templates"), "add_event_id_index_to_event_store_events_in_streams_template.erb") | ||
) | ||
) | ||
end | ||
|
||
def migration_version | ||
::ActiveRecord::Migration.current_version | ||
end | ||
|
||
def timestamp | ||
Time.now.strftime("%Y%m%d%H%M%S") | ||
end | ||
|
||
def write_to_file(path) | ||
File.write(path, migration_code) | ||
end | ||
|
||
def build_path(migration_path) | ||
File.join("#{migration_path}", "#{timestamp}_add_event_id_index_to_event_store_events_in_streams.rb") | ||
end | ||
end | ||
end | ||
end |
9 changes: 9 additions & 0 deletions
9
...ord/generators/templates/add_event_id_index_to_event_store_events_in_streams_template.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# frozen_string_literal: true | ||
|
||
class AddEventIdIndexToEventStoreEventsInStreams < ActiveRecord::Migration[<%= migration_version %>] | ||
def change | ||
return if index_exists?(:event_store_events_in_streams, :event_id) | ||
|
||
add_index :event_store_events_in_streams, [:event_id] | ||
end | ||
end |
72 changes: 72 additions & 0 deletions
72
ruby_event_store-active_record/spec/event_id_index_migration_generator_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# frozen_string_literal: true | ||
|
||
require "spec_helper" | ||
require_relative "../../support/helpers/silence_stdout" | ||
|
||
module RubyEventStore | ||
module ActiveRecord | ||
::RSpec.describe EventIdIndexMigrationGenerator do | ||
around { |example| SilenceStdout.silence_stdout { example.run } } | ||
around do |example| | ||
begin | ||
@dir = Dir.mktmpdir(nil, "./") | ||
example.call | ||
ensure | ||
FileUtils.rm_r(@dir) | ||
FileUtils.rm_f(["./20221130213700_add_event_id_index_to_event_store_events_in_streams.rb"]) | ||
end | ||
end | ||
|
||
before { allow(Time).to receive(:now).and_return(Time.new(2022, 11, 30, 21, 37, 00)) } | ||
|
||
specify "it is created within specified directory" do | ||
migration_generator(@dir) | ||
|
||
expect(migration_exists?(@dir)).to be_truthy | ||
end | ||
|
||
specify "returns path to migration file" do | ||
path = migration_generator(@dir) | ||
|
||
expected_path = "#{@dir}/20221130213700_add_event_id_index_to_event_store_events_in_streams.rb" | ||
expect(path).to match(expected_path) | ||
end | ||
|
||
specify "uses particular migration version" do | ||
migration_generator(@dir) | ||
|
||
expect(read_migration(@dir)).to include("ActiveRecord::Migration[#{::ActiveRecord::Migration.current_version}]") | ||
end | ||
|
||
specify "uses particular migration version for rails 6.0" do | ||
skip unless ENV["BUNDLE_GEMFILE"]&.include?("rails_6_0") | ||
|
||
migration_generator(@dir) | ||
|
||
expect(read_migration(@dir)).to include("ActiveRecord::Migration[6.0]") | ||
end | ||
|
||
specify "uses particular migration version for rails 6.1" do | ||
skip unless ENV["BUNDLE_GEMFILE"]&.include?("rails_6_1") | ||
|
||
migration_generator(@dir) | ||
|
||
expect(read_migration(@dir)).to include("ActiveRecord::Migration[6.1]") | ||
end | ||
|
||
private | ||
|
||
def migration_generator(dir) | ||
EventIdIndexMigrationGenerator.new.call(dir) | ||
end | ||
|
||
def migration_exists?(dir) | ||
File.exist?("#{dir}/20221130213700_add_event_id_index_to_event_store_events_in_streams.rb") | ||
end | ||
|
||
def read_migration(dir) | ||
File.read("#{dir}/20221130213700_add_event_id_index_to_event_store_events_in_streams.rb") | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters