Skip to content

Commit

Permalink
Merge pull request #832 from pbstriker38/wait_for_active_record_to_be…
Browse files Browse the repository at this point in the history
…_loaded

Wait for active record to be loaded
  • Loading branch information
bkeepers committed Apr 30, 2024
2 parents 13c415c + 9e62a45 commit 5aa9cfd
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 41 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
53 changes: 35 additions & 18 deletions spec/flipper/adapters/active_record_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
{
"adapter" => "mysql2",
"encoding" => "utf8mb4",
"host" => ENV["MYSQL_HOST"],
"username" => ENV["MYSQL_USER"] || "root",
"password" => ENV["MYSQL_PASSWORD"] || "",
"database" => ENV["MYSQL_DATABASE"] || "flipper_test",
Expand All @@ -42,22 +43,17 @@
context "with tables created" do
before(:all) do
skip_on_error(ActiveRecord::ConnectionNotEstablished, "#{config['adapter']} not available") do
silence { ActiveRecord::Tasks::DatabaseTasks.create(config) }
silence do
ActiveRecord::Tasks::DatabaseTasks.create(config)
end
end

Flipper.configuration = nil
end

before(:each) do
skip_on_error(ActiveRecord::ConnectionNotEstablished, "#{config['adapter']} not available") do
ActiveRecord::Base.establish_connection(config)
CreateFlipperTables.migrate(:up)
end
end

after(:each) do
ActiveRecord::Tasks::DatabaseTasks.purge(config)
ActiveRecord::Base.connection.close
CreateFlipperTables.migrate(:up)
end

after(:all) do
Expand Down Expand Up @@ -215,18 +211,39 @@
end
end

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

Flipper.configuration = nil
end

before(:each) do
ActiveRecord::Base.establish_connection(config)
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)
after(:all) do
silence { ActiveRecord::Tasks::DatabaseTasks.drop(config) } unless $skip
end

silence do
expect {
load 'flipper/adapters/active_record.rb'
Flipper::Adapters::ActiveRecord.new
}.not_to raise_error
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
end
end
end
end
Expand Down

0 comments on commit 5aa9cfd

Please sign in to comment.