-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #753 from RailsEventStore/multiple-databases-repos…
…itory-support Multiple databases repository support
- Loading branch information
Showing
12 changed files
with
186 additions
and
21 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
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
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
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
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
38 changes: 38 additions & 0 deletions
38
...event_store_active_record/lib/rails_event_store_active_record/with_abstract_base_class.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,38 @@ | ||
# frozen_string_literal: true | ||
|
||
module RailsEventStoreActiveRecord | ||
class WithAbstractBaseClass | ||
def initialize(base_klass) | ||
raise ArgumentError.new( | ||
"#{base_klass} must be an abstract class that inherits from ActiveRecord::Base" | ||
) unless base_klass < ActiveRecord::Base && base_klass.abstract_class? | ||
@base_klass = base_klass | ||
end | ||
|
||
def call(instance_id: SecureRandom.hex) | ||
[ | ||
build_event_klass(instance_id), | ||
build_stream_klass(instance_id), | ||
] | ||
end | ||
|
||
private | ||
def build_event_klass(instance_id) | ||
Object.const_set("Event_"+instance_id, | ||
Class.new(@base_klass) do | ||
self.table_name = 'event_store_events' | ||
self.primary_key = 'id' | ||
end | ||
) | ||
end | ||
|
||
def build_stream_klass(instance_id) | ||
Object.const_set("EventInStream_"+instance_id, | ||
Class.new(@base_klass) do | ||
self.table_name = 'event_store_events_in_streams' | ||
belongs_to :event, class_name: "Event_"+instance_id | ||
end | ||
) | ||
end | ||
end | ||
end |
9 changes: 9 additions & 0 deletions
9
rails_event_store_active_record/lib/rails_event_store_active_record/with_default_models.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,9 @@ | ||
# frozen_string_literal: true | ||
|
||
module RailsEventStoreActiveRecord | ||
class WithDefaultModels | ||
def call | ||
[Event, EventInStream] | ||
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
1 change: 0 additions & 1 deletion
1
rails_event_store_active_record/spec/pg_linearized_event_repository_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
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
68 changes: 68 additions & 0 deletions
68
rails_event_store_active_record/spec/with_abstract_base_class_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,68 @@ | ||
require 'spec_helper' | ||
require 'ruby_event_store' | ||
require 'ruby_event_store/spec/event_repository_lint' | ||
|
||
module RailsEventStoreActiveRecord | ||
RSpec.describe WithAbstractBaseClass do | ||
include SchemaHelper | ||
|
||
specify 'Base for event factory models must be an abstract class' do | ||
NonAbstractClass = Class.new(ActiveRecord::Base) | ||
expect { | ||
WithAbstractBaseClass.new(NonAbstractClass) | ||
}.to raise_error(ArgumentError) | ||
.with_message('RailsEventStoreActiveRecord::NonAbstractClass must be an abstract class that inherits from ActiveRecord::Base') | ||
end | ||
|
||
specify 'Base for event factory models could not be the ActiveRecord::Base' do | ||
expect { | ||
WithAbstractBaseClass.new(ActiveRecord::Base) | ||
}.to raise_error(ArgumentError) | ||
.with_message('ActiveRecord::Base must be an abstract class that inherits from ActiveRecord::Base') | ||
end | ||
|
||
specify 'Base for event factory models must inherit from ActiveRecord::Base' do | ||
expect { | ||
WithAbstractBaseClass.new(Object) | ||
}.to raise_error(ArgumentError) | ||
.with_message('Object must be an abstract class that inherits from ActiveRecord::Base') | ||
end | ||
|
||
specify 'AR classes must have the same instance id' do | ||
event_klass, stream_klass = WithAbstractBaseClass.new(CustomApplicationRecord).call | ||
|
||
expect(event_klass.name).to match(/^Event_[a-z,0-9]{32}$/) | ||
expect(stream_klass.name).to match(/^EventInStream_[a-z,0-9]{32}$/) | ||
expect(event_klass.name[6..-1]).to eq(stream_klass.name[14..-1]) | ||
end | ||
|
||
specify 'each factory must generate different AR classes' do | ||
factory1 = WithAbstractBaseClass.new(CustomApplicationRecord) | ||
factory2 = WithAbstractBaseClass.new(CustomApplicationRecord) | ||
event_klass_1, stream_klass_1 = factory1.call | ||
event_klass_2, stream_klass_2 = factory2.call | ||
expect(event_klass_1).not_to eq(event_klass_2) | ||
expect(stream_klass_1).not_to eq(stream_klass_2) | ||
end | ||
|
||
specify 'reading/writting works with base class' do | ||
begin | ||
establish_database_connection | ||
load_database_schema | ||
|
||
repository = EventRepository.new(model_factory: WithAbstractBaseClass.new(CustomApplicationRecord), serializer: YAML) | ||
repository.append_to_stream( | ||
[event = RubyEventStore::SRecord.new], | ||
RubyEventStore::Stream.new(RubyEventStore::GLOBAL_STREAM), | ||
RubyEventStore::ExpectedVersion.any | ||
) | ||
reader = RubyEventStore::SpecificationReader.new(repository, RubyEventStore::Mappers::NullMapper.new) | ||
specification = RubyEventStore::Specification.new(reader) | ||
read_event = repository.read(specification.result).first | ||
expect(read_event).to eq(event) | ||
ensure | ||
drop_database | ||
end | ||
end | ||
end | ||
end |
12 changes: 12 additions & 0 deletions
12
rails_event_store_active_record/spec/with_default_models_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,12 @@ | ||
require 'spec_helper' | ||
|
||
module RailsEventStoreActiveRecord | ||
RSpec.describe WithDefaultModels do | ||
specify do | ||
event_klass, stream_klass = WithDefaultModels.new.call | ||
|
||
expect(event_klass).to eq(Event) | ||
expect(stream_klass).to eq(EventInStream) | ||
end | ||
end | ||
end |