Skip to content

Commit

Permalink
Move sqlite database initialization in tests
Browse files Browse the repository at this point in the history
sqlite3 gem in 2.1.0 has introduced fork safety mechanism that discards
open writable database connections carried across a fork() which is used
by mutant gem

https://github.com/sparklemotion/sqlite3-ruby/blob/main/CHANGELOG.md#fork-safety-improvements
  • Loading branch information
mpraglowski committed Feb 20, 2025
1 parent 190a683 commit ffebdca
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 27 deletions.
28 changes: 15 additions & 13 deletions contrib/ruby_event_store-rom/spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,10 @@
module RubyEventStore
module ROM
class SpecHelper
attr_reader :rom_container
attr_reader :rom_container, :database_uri

def initialize(database_uri = ENV["DATABASE_URL"])
config =
::ROM::Configuration.new(
:sql,
database_uri,
max_connections: database_uri =~ /sqlite/ ? 1 : 5,
preconnect: :concurrently,
fractional_seconds: true
)
config.default.use_logger(Logger.new(STDOUT)) if ENV.has_key?("VERBOSE")
config.default.run_migrations

@rom_container = ROM.setup(config)
@database_uri = database_uri
end

def serializer
Expand Down Expand Up @@ -83,6 +72,19 @@ def connection_pool_size
protected

def gateway
@config ||=
::ROM::Configuration.new(
:sql,
database_uri,
max_connections: database_uri =~ /sqlite/ ? 1 : 5,
preconnect: :concurrently,
fractional_seconds: true
).tap do |config|
config.default.use_logger(Logger.new(STDOUT)) if ENV.has_key?("VERBOSE")
config.default.run_migrations
end
@rom_container ||= ROM.setup(@config)

rom_container.gateways.fetch(:default)
end

Expand Down
33 changes: 19 additions & 14 deletions contrib/ruby_event_store-sequel/spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,10 @@
module RubyEventStore
module Sequel
class SpecHelper
attr_reader :sequel
attr_reader :database_uri

def initialize(database_uri = ENV["DATABASE_URL"])
@sequel =
::Sequel.connect(
database_uri,
fractional_seconds: true,
preconnect: :concurrently,
max_connections: database_uri.include?("sqlite") ? 1 : 5
)
@sequel.loggers << Logger.new(STDOUT) if ENV.has_key?("VERBOSE")
@database_uri = database_uri
end

def serializer
Expand Down Expand Up @@ -73,20 +66,32 @@ def has_connection_pooling?
end

def connection_pool_size
@sequel.pool.max_size
sequel.pool.max_size
end

def sequel
@sequel ||=
::Sequel.connect(
database_uri,
fractional_seconds: true,
preconnect: :concurrently,
max_connections: database_uri.include?("sqlite") ? 1 : 5
).tap do |sequel|
sequel.loggers << Logger.new(STDOUT) if ENV.has_key?("VERBOSE")
end
end

protected

def load_schema
::Sequel.extension :migration
::Sequel::Migrator.run(@sequel, "lib/ruby_event_store/generators/templates/#{template_dir}", version: 0)
::Sequel::Migrator.run(sequel, "lib/ruby_event_store/generators/templates/#{template_dir}", version: 0)
end

def drop_schema
@sequel.drop_table?(:event_store_events)
@sequel.drop_table?(:event_store_events_in_streams)
@sequel.drop_table?(:schema_info)
sequel.drop_table?(:event_store_events)
sequel.drop_table?(:event_store_events_in_streams)
sequel.drop_table?(:schema_info)
end

private
Expand Down

0 comments on commit ffebdca

Please sign in to comment.