Skip to content

Commit

Permalink
Merge pull request #348 from RailsEventStore/0-28
Browse files Browse the repository at this point in the history
Do not double serialize YAML in Legacy Repository
  • Loading branch information
mostlyobvious committed May 6, 2018
2 parents 58adf72 + 836bfcc commit b79081c
Show file tree
Hide file tree
Showing 6 changed files with 192 additions and 2 deletions.
4 changes: 4 additions & 0 deletions rails_event_store_active_record-legacy/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ DATABASE_URL ?= sqlite3::memory:
install: ## Install gem dependencies
@echo "Installing gem dependencies"
@bundle install
@echo "Installing schema compatibility check gem dependencies"
@BUNDLE_GEMFILE=spec/schema/Gemfile bundle install

remove-lock:
@echo "Removing resolved dependency versions"
-rm Gemfile.lock
-rm spec/schema/Gemfile.lock


reinstall: remove-lock install ## Removing resolved dependency versions

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ class EventRepository
class LegacyEvent < ::ActiveRecord::Base
self.primary_key = :id
self.table_name = 'event_store_events'
serialize :metadata
serialize :data
end

private_constant :LegacyEvent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'sqlite3', '1.3.13'
spec.add_development_dependency 'pg', '0.21'
spec.add_development_dependency 'mysql2', '0.4.10'
spec.add_development_dependency 'childprocess'
spec.add_development_dependency 'mutant-rspec', '~> 0.8.14'

spec.add_dependency 'ruby_event_store', '= 0.28.1'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
require 'spec_helper'
require 'pathname'
require 'childprocess'
require 'active_record'
require 'logger'
require 'ruby_event_store'


class EventAll < RubyEventStore::Event
end
class EventA1 < RubyEventStore::Event
end
class EventA2 < RubyEventStore::Event
end
class EventB1 < RubyEventStore::Event
end
class EventB2 < RubyEventStore::Event
end

RSpec.describe "legacy schema compatibility" do
include SchemaHelper

def silence_stderr
$stderr = StringIO.new
yield
$stderr = STDERR
end

around(:each) do |example|
begin
ActiveRecord::Schema.verbose = $verbose
fill_data_using_older_gem
establish_database_connection
silence_stderr { example.run }
ensure
drop_legacy_database
end
end

specify "reading events" do
skip("in-memory sqlite cannot run this test") if ENV['DATABASE_URL'].include?(":memory:")

event = client.read_all_streams_forward[0]
expect(event.metadata[:timestamp]).to be_kind_of(Time)
end

specify "writing events" do
skip("in-memory sqlite cannot run this test") if ENV['DATABASE_URL'].include?(":memory:")
client.append_to_stream(
write_event = EventAll.new(metadata: { foo: 13 }),
stream_name: 'foo',
expected_version: -1
)

read_event = client.read_stream_events_forward('foo')[0]
expect(read_event).to eq(write_event)
expect(read_event.metadata[:foo]).to eq(13)
expect(read_event.metadata[:timestamp]).to be_kind_of(Time)
end

private

let(:client) { RubyEventStore::Client.new(repository: RailsEventStoreActiveRecord::Legacy::EventRepository.new) }

def fill_data_using_older_gem
pathname = Pathname.new(__FILE__).dirname
cwd = pathname.join("schema")
FileUtils.rm(cwd.join("Gemfile.lock")) if File.exists?(cwd.join("Gemfile.lock"))
process = ChildProcess.build("bundle", "exec", "ruby", "fill_data.rb")
process.environment['BUNDLE_GEMFILE'] = cwd.join('Gemfile')
process.environment['DATABASE_URL'] = ENV['DATABASE_URL']
process.environment['RAILS_VERSION'] = ENV['RAILS_VERSION']
process.cwd = cwd
process.io.stdout = $stdout
process.io.stderr = $stderr
process.start
begin
process.poll_for_exit(10)
rescue ChildProcess::TimeoutError
process.stop
end
expect(process.exit_code).to eq(0)
end
end
6 changes: 6 additions & 0 deletions rails_event_store_active_record-legacy/spec/schema/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
source 'https://rubygems.org'
gem 'rails_event_store', '0.18.2'
gem 'rails', ENV['RAILS_VERSION']
gem 'sqlite3', '1.3.13'
gem 'pg', '0.21'
gem 'mysql2', '0.4.10'
97 changes: 97 additions & 0 deletions rails_event_store_active_record-legacy/spec/schema/fill_data.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
require 'rails/generators' # doh
require 'rails_event_store_active_record'
require 'ruby_event_store'
require 'logger'

$verbose = ENV.has_key?('VERBOSE') ? true : false

ActiveRecord::Base.logger = Logger.new(STDOUT) if $verbose
ActiveRecord::Base.establish_connection(ENV['DATABASE_URL'].gsub("db.sqlite3", "../../db.sqlite3"))
ActiveRecord::Schema.define do
self.verbose = $verbose
create_table(:event_store_events, force: false) do |t|
t.string :stream, null: false
t.string :event_type, null: false
t.string :event_id, null: false
t.text :metadata
t.text :data, null: false
t.datetime :created_at, null: false
end
add_index :event_store_events, :stream
add_index :event_store_events, :created_at
add_index :event_store_events, :event_type
add_index :event_store_events, :event_id, unique: true
end

class EventAll < RubyEventStore::Event
end
class EventA1 < RubyEventStore::Event
end
class EventA2 < RubyEventStore::Event
end
class EventB1 < RubyEventStore::Event
end
class EventB2 < RubyEventStore::Event
end

client = RubyEventStore::Client.new(
repository: RailsEventStoreActiveRecord::EventRepository.new
)
client.append_to_stream(EventAll.new(data: {
all: true,
a: 1,
text: "text",
}, event_id: "94b297a3-5a29-4942-9038-3efeceb4d905"))
client.append_to_stream(EventAll.new(data: {
all: true,
a: 2,
date: Date.new(2017, 10, 11),
}, event_id: "6a31b594-7d8f-428b-916f-496f6da05bfd"))
client.append_to_stream(EventAll.new(data: {
all: true,
a: 3,
time: Time.new(2017,10, 10, 12),
}, event_id: "011cc5c4-d638-4785-9aa0-7d6a2d3e2a58"))



client.append_to_stream(EventA1.new(data: {
a1: true,
decimal: BigDecimal.new("20.00"),
}, event_id: "d39cb65f-bc3c-4fbb-9470-52bf5e322bba"),
stream_name: "Order-1",
)
client.append_to_stream(EventA2.new(data: {
all: true,
symbol: :symbol,
}, event_id: "f2cecc51-adb1-4d83-b3ca-483d26311f03"),
stream_name: "Order-1",
)
client.append_to_stream(EventA1.new(data: {
all: true,
symbol: :symbol,
}, event_id: "600e1e1b-7fdf-44e2-a406-8b612c67c881"),
stream_name: "Order-1",
)


client.append_to_stream(EventB1.new(data: {
a1: true,
decimal: BigDecimal.new("20.00"),
}, event_id: "9009df88-6044-4a62-b7ae-098c42a9c5e1"),
stream_name: "WroclawBuyers",
)
client.append_to_stream(EventB2.new(data: {
all: true,
symbol: :symbol,
}, event_id: "cefdd213-0c92-46f6-bbdf-3ea9542d969a"),
stream_name: "WroclawBuyers",
)
client.append_to_stream(EventB2.new(data: {
all: true,
symbol: :symbol,
}, event_id: "36775fcd-c5d8-49c9-bf70-f460ba12d7c2"),
stream_name: "WroclawBuyers",
)

puts "filled" if $verbose

0 comments on commit b79081c

Please sign in to comment.