Skip to content

Commit

Permalink
Wait for ActiveRecord to be fully loaded before adding models
Browse files Browse the repository at this point in the history
  • Loading branch information
pbstriker38 committed Apr 23, 2024
1 parent 90372a0 commit bad0fc1
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 43 deletions.
48 changes: 25 additions & 23 deletions lib/flipper/adapters/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,36 @@ module Adapters
class ActiveRecord
include ::Flipper::Adapter

# Abstract base class for internal models
class Model < ::ActiveRecord::Base
self.abstract_class = true
end
ActiveSupport.on_load(:active_record) do
# Abstract base class for internal models
class Model < ::ActiveRecord::Base
self.abstract_class = true
end

# Private: Do not use outside of this adapter.
class Feature < Model
self.table_name = [
Model.table_name_prefix,
"flipper_features",
Model.table_name_suffix,
].join
# Private: Do not use outside of this adapter.
class Feature < Model
self.table_name = [
Model.table_name_prefix,
"flipper_features",
Model.table_name_suffix,
].join

has_many :gates, foreign_key: "feature_key", primary_key: "key"
has_many :gates, foreign_key: "feature_key", primary_key: "key"

validates :key, presence: true
end
validates :key, presence: true
end

# Private: Do not use outside of this adapter.
class Gate < Model
self.table_name = [
Model.table_name_prefix,
"flipper_gates",
Model.table_name_suffix,
].join
# Private: Do not use outside of this adapter.
class Gate < Model
self.table_name = [
Model.table_name_prefix,
"flipper_gates",
Model.table_name_suffix,
].join

validates :feature_key, presence: true
validates :key, presence: true
validates :feature_key, presence: true
validates :key, presence: true
end
end

VALUE_TO_TEXT_WARNING = <<-EOS
Expand Down
49 changes: 29 additions & 20 deletions spec/flipper/adapters/active_record_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,19 @@
}
].each do |config|
context "with #{config['adapter']}" do
context "with tables created" do
before(:all) do
skip_on_error(ActiveRecord::ConnectionNotEstablished, "#{config['adapter']} not available") do
silence { ActiveRecord::Tasks::DatabaseTasks.create(config) }
end

Flipper.configuration = nil
before(:all) do
skip_on_error(ActiveRecord::ConnectionNotEstablished, "#{config['adapter']} not available") do
silence { ActiveRecord::Tasks::DatabaseTasks.create(config) }
end

Flipper.configuration = nil
end

after(:all) do
silence { ActiveRecord::Tasks::DatabaseTasks.drop(config) } unless $skip
end

context "with tables created" do
before(:each) do
skip_on_error(ActiveRecord::ConnectionNotEstablished, "#{config['adapter']} not available") do
ActiveRecord::Base.establish_connection(config)
Expand All @@ -60,10 +64,6 @@
ActiveRecord::Base.connection.close
end

after(:all) do
silence { ActiveRecord::Tasks::DatabaseTasks.drop(config) } unless $skip
end

it_should_behave_like 'a flipper adapter'

it "should load actor ids fine" do
Expand Down Expand Up @@ -215,18 +215,27 @@
end
end

it "works when table doesn't exist" do
context "without tables created" do
before(:each) do
skip_on_error(ActiveRecord::ConnectionNotEstablished, "#{config['adapter']} not available") do
ActiveRecord::Base.establish_connection(config)
end
end

Flipper.configuration = nil
Flipper.instance = nil
after(:each) do
ActiveRecord::Base.connection.close
end

Flipper::Adapters.send(:remove_const, :ActiveRecord) if Flipper::Adapters.const_defined?(:ActiveRecord)
it "does not raise an error" do
Flipper.configuration = nil
Flipper.instance = nil

silence do
expect {
load 'flipper/adapters/active_record.rb'
Flipper::Adapters::ActiveRecord.new
}.not_to raise_error
silence do
expect {
load 'flipper/adapters/active_record.rb'
Flipper::Adapters::ActiveRecord.new
}.not_to raise_error
end
end
end
end
Expand Down

0 comments on commit bad0fc1

Please sign in to comment.