Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be simpler to just move all the things that depends on Active Record into another file and then require it when AR is loaded, or something like that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's already a generator for adding the DB migrations, so another option is to just have the generator add these models to the app/models directory.

# 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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed that this test wasn't actually testing the table not existing, but the fact that it's not even connected to the DB. Without the silence, you can see the message:

No connection pool for 'ActiveRecord::Base' found.. You likely need to run `rails g flipper:active_record` and/or `rails db:migrate`.

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