Skip to content

Commit

Permalink
FIX: Use next instead of break to avoid a local jump error
Browse files Browse the repository at this point in the history
The current code uses `break` inside the initializer block used by
Rails. This actually breaks the initializers flow. The solution is quite
simple, use `next` instead of `break`.
  • Loading branch information
Flink committed May 30, 2023
1 parent 3aef07d commit c4509f5
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 13 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Expand Up @@ -5,7 +5,9 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
## [2.0.1] - 2023-05-30

- FIX: Use `next` instead of `break` to avoid a local jump error

## [2.0.0] - 2023-05-16

Expand Down
16 changes: 8 additions & 8 deletions lib/rails_failover/active_record/railtie.rb
Expand Up @@ -6,7 +6,7 @@ class Railtie < ::Rails::Railtie
initializer "rails_failover.init", after: "active_record.initialize_database" do |app|
app.config.active_record_rails_failover = false
config = RailsFailover::ActiveRecord.config
break unless config[:replica_host] && config[:replica_port]
next unless config[:replica_host] && config[:replica_port]

app.config.active_record_rails_failover = true
::ActiveSupport.on_load(:active_record) do
Expand All @@ -21,14 +21,14 @@ class Railtie < ::Rails::Railtie
end

initializer "rails_failover.insert_middleware" do |app|
if app.config.active_record_rails_failover
ActionDispatch::DebugExceptions.register_interceptor do |request, exception|
RailsFailover::ActiveRecord::Interceptor.handle(request, exception)
end
next unless app.config.active_record_rails_failover

if !skip_middleware?(app.config)
app.middleware.unshift(RailsFailover::ActiveRecord::Middleware)
end
ActionDispatch::DebugExceptions.register_interceptor do |request, exception|
RailsFailover::ActiveRecord::Interceptor.handle(request, exception)
end

if !skip_middleware?(app.config)
app.middleware.unshift(RailsFailover::ActiveRecord::Middleware)
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/rails_failover/version.rb
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module RailsFailover
VERSION = "2.0.0"
VERSION = "2.0.1"
end
15 changes: 15 additions & 0 deletions spec/integration/active_record_spec.rb
Expand Up @@ -115,4 +115,19 @@ def restart_dummy_rails_server
expect(path.exist?).to be true
end
end

context "when there is no replica config for the DB" do
let(:config_path) { Pathname.new("#{__dir__}/../support/dummy_app/config") }
let(:replicas_config) { config_path / "database.replicas.yml" }
let(:no_replicas_config) { config_path / "database.no-replicas.yml" }
let(:db_config) { config_path / "database.yml" }

before { FileUtils.cp(no_replicas_config, db_config) }

after { FileUtils.cp(replicas_config, db_config) }

it "does not prevent Rails from loading" do
expect { restart_dummy_rails_server }.not_to raise_error
end
end
end
15 changes: 15 additions & 0 deletions spec/support/dummy_app/config/database.no-replicas.yml
@@ -0,0 +1,15 @@
production:
primary:
adapter: postgresql
pool: 15
timeout: 5000
database: test
host: localhost
port: 5434
two:
adapter: postgresql
pool: 10
timeout: 5000
database: test
host: localhost
port: 5434
19 changes: 19 additions & 0 deletions spec/support/dummy_app/config/database.replicas.yml
@@ -0,0 +1,19 @@
production:
primary:
adapter: postgresql
pool: 15
timeout: 5000
database: test
host: localhost
port: 5434
replica_host: localhost
replica_port: 5435
two:
adapter: postgresql
pool: 10
timeout: 5000
database: test
host: localhost
port: 5434
replica_host: localhost
replica_port: 5435
Expand Up @@ -17,4 +17,6 @@ def call(env)
end
end

Rails.application.middleware.insert_before RailsFailover::ActiveRecord::Middleware, RoleSwitcher
if Rails.application.config.active_record_rails_failover
Rails.application.middleware.insert_before RailsFailover::ActiveRecord::Middleware, RoleSwitcher
end
Expand Up @@ -15,5 +15,7 @@ def call(env)
end
end

Rails.application.middleware.insert_after RailsFailover::ActiveRecord::Middleware,
TriggerPGException
if Rails.application.config.active_record_rails_failover
Rails.application.middleware.insert_after RailsFailover::ActiveRecord::Middleware,
TriggerPGException
end

0 comments on commit c4509f5

Please sign in to comment.