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:
gem 'flipper-active_record'Run $ bundle to install:
vagrant@ubuntu-xenial:/vagrant/play_flipper$ bundle
Using rake 12.3.2
Using concurrent-ruby 1.1.4
Using i18n 1.5.3
...
Using web-console 3.7.0
Bundle complete! 20 Gemfile dependencies, 83 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.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:
vagrant@ubuntu-xenial:/vagrant/play_flipper$ rails generate flipper:active_record
Running via Spring preloader in process 2996
created db/migrate/20190207124709_create_flipper_tables.rbWith the migration script, run: $ rake db:migrate to create the
necessary tables for Flipper to record feature flag information.
vagrant@ubuntu-xenial:/vagrant/play_flipper$ rake db:migrate
== 20190207124709 CreateFlipperTables: migrating ==============================
-- create_table(:flipper_features)
-> 0.0068s
-- add_index(:flipper_features, :key, {:unique=>true})
-> 0.0017s
-- create_table(:flipper_gates)
-> 0.0018s
-- add_index(:flipper_gates, [:feature_key, :key, :value], {:unique=>true})
-> 0.0024s
== 20190207124709 CreateFlipperTables: migrated (0.0138s) =====================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.