Playing with Flipper 2
After my last article, Playing with Flipper, I noticed the Flipper User Interface is not stable when I reload quickly
Which made me wonder, what’s going on??
Looking around a bit more, I found the Flipper ActiveRecord documentation, which makes sense: save the feature state in the database, not in memory.
To do this, we need to:
- get the Flipper ActiveRecord gem
- create the migration file
- migrate the database to include
Requirements
If you would like to follow along:
- Install Virtualbox
- Install vagrant
- please clone this repository
Once cloned, run: $ vagrant up
to create the Virtualbox that canrun
this project.
After the Virtualbox finishes building, run: $ vagrant ssh
to log
into the virtualbox computer and to get to the project resources, run
command: cd /vagrant
.
Gem Installation
Install the Flipper ActiveRecord gem by adding the following line to
the Gemfile
:
Run $ bundle
to install:
Let’s configure the rest of the application for Flipper to use ActiveRecord.
Create Migration
The Flipper gem provides an ActiveRecord migration script to use a
database. To create the migration script, run: $ rails generate
flipper:active_record
:
With the migration script, run: $ rake db:migrate
to create the
necessary tables for Flipper to record feature flag information.
The database is ready for Flipper, now to make the appropriate changes in Flipper’s initialization.
Initializer Changes
Even with the database set up for Flipper, its configuration needs to know to use the database. The main change is for Flipper to use the ActiveRecord adapter instead of the Memory adapter.
Change the config/initializers/flipper.rb
file from:
1
2
3
4
5
6
7
8
9
10
11
12
13
require 'flipper'
Flipper.configure do |config|
config.default do
adapter = Flipper::Adapters::Memory.new
flipper = Flipper.new(adapter)
flipper[:from_initializer_disabled].enable
flipper[:from_initializer_disabled].disable
flipper[:from_initializer_enabled].enable
flipper
end
end
To this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
require 'flipper'
require 'flipper/adapters/active_record'
Flipper.configure do |config|
config.default do
adapter = Flipper::Adapters::ActiveRecord.new
flipper = Flipper.new(adapter)
flipper[:from_initializer_disabled].enable
flipper[:from_initializer_disabled].disable
flipper[:from_initializer_enabled].enable
flipper
end
end
The main change are:
Line | Reasoning |
---|---|
2 | Includes additional library files used by Flipper for ActiveRecord |
6 | Change adapter from Memory to ActiveRecord |
Restart Server
Use the $ rails s
command to restart the server:
Flipper with ActiveRecord
Let’s attempt the same actions performed earlier:
- list features
- activate features
- add new features
With Flipper using ActiveRecord, the features are stable. Features that are off stay off or on. There’s no question on the Flipper User Interface.
Conclusion
This has been another session of playing with Flipper. Utilizing Flipper’s ActiveRecord store requires a database migration generation, running the migration, and change in the initializer.
With Flipper using ActiveRecord, the Flipper User Interface consistently displays features properly.