diff --git a/Gemfile b/Gemfile index 65cab8bfa..0eca4cb1c 100644 --- a/Gemfile +++ b/Gemfile @@ -13,7 +13,7 @@ gem 'statsd-ruby', '~> 1.2.1' gem 'rspec', '~> 3.0' gem 'rack-test', '~> 0.6.3' gem 'sqlite3', '~> 1.3.11' -gem 'rails', "~> #{ENV['RAILS_VERSION'] || '4.2.5'}" +gem 'rails', "~> #{ENV['RAILS_VERSION'] || '5.1.4'}" gem 'minitest', '~> 5.8.0' gem 'rubocop', '~> 0.45.0' gem 'rubocop-rspec', '= 1.5.1' diff --git a/examples/active_record/ar_setup.rb b/examples/active_record/ar_setup.rb new file mode 100644 index 000000000..3da29cb38 --- /dev/null +++ b/examples/active_record/ar_setup.rb @@ -0,0 +1,36 @@ +require 'pathname' +require 'logger' + +root_path = Pathname(__FILE__).dirname.join('..').expand_path +lib_path = root_path.join('lib') +$:.unshift(lib_path) + +require 'active_record' +ActiveRecord::Base.establish_connection({ + adapter: 'sqlite3', + database: ':memory:', +}) + +ActiveRecord::Base.connection.execute <<-SQL + CREATE TABLE flipper_features ( + id integer PRIMARY KEY, + key text NOT NULL UNIQUE, + created_at datetime NOT NULL, + updated_at datetime NOT NULL + ) +SQL + +ActiveRecord::Base.connection.execute <<-SQL + CREATE TABLE flipper_gates ( + id integer PRIMARY KEY, + feature_key text NOT NULL, + key text NOT NULL, + value text DEFAULT NULL, + created_at datetime NOT NULL, + updated_at datetime NOT NULL + ) +SQL + +ActiveRecord::Base.connection.execute <<-SQL + CREATE UNIQUE INDEX index_gates_on_keys_and_value on flipper_gates (feature_key, key, value) +SQL diff --git a/examples/active_record/basic.rb b/examples/active_record/basic.rb index ee8b43369..23cab8290 100644 --- a/examples/active_record/basic.rb +++ b/examples/active_record/basic.rb @@ -1,18 +1,4 @@ -require 'pathname' -require 'logger' - -root_path = Pathname(__FILE__).dirname.join('..').expand_path -lib_path = root_path.join('lib') -$:.unshift(lib_path) - -require 'active_record' -ActiveRecord::Base.establish_connection({ - adapter: 'sqlite3', - database: ':memory:', -}) - -require 'generators/flipper/templates/migration' -CreateFlipperTables.up +require_relative "./ar_setup" # Requires the flipper-active_record gem to be installed. require 'flipper/adapters/active_record' diff --git a/examples/active_record/internals.rb b/examples/active_record/internals.rb index 06976cb14..fb9bd8ab5 100644 --- a/examples/active_record/internals.rb +++ b/examples/active_record/internals.rb @@ -1,20 +1,6 @@ -require 'pp' -require 'pathname' -require 'logger' - -root_path = Pathname(__FILE__).dirname.join('..').expand_path -lib_path = root_path.join('lib') -$:.unshift(lib_path) - -require 'active_record' -ActiveRecord::Base.establish_connection({ - adapter: 'sqlite3', - database: ':memory:', -}) - -require 'generators/flipper/templates/migration' -CreateFlipperTables.up +require_relative "./ar_setup" +# Requires the flipper-active_record gem to be installed. require 'flipper/adapters/active_record' adapter = Flipper::Adapters::ActiveRecord.new flipper = Flipper.new(adapter) diff --git a/examples/importing.rb b/examples/importing.rb index ad93568cd..df4726e1c 100644 --- a/examples/importing.rb +++ b/examples/importing.rb @@ -1,17 +1,9 @@ require File.expand_path('../example_setup', __FILE__) - +require_relative 'active_record/ar_setup' require 'flipper' require 'flipper/adapters/redis' require 'flipper/adapters/active_record' -# Active Record boiler plate, feel free to ignore. -ActiveRecord::Base.establish_connection({ - adapter: 'sqlite3', - database: ':memory:', -}) -require 'generators/flipper/templates/migration' -CreateFlipperTables.up - # Say you are using redis... redis_adapter = Flipper::Adapters::Redis.new(Redis.new) redis_flipper = Flipper.new(redis_adapter) diff --git a/lib/generators/flipper/active_record_generator.rb b/lib/generators/flipper/active_record_generator.rb index 46e0503c7..13a00c2f6 100644 --- a/lib/generators/flipper/active_record_generator.rb +++ b/lib/generators/flipper/active_record_generator.rb @@ -8,12 +8,22 @@ class ActiveRecordGenerator < ::Rails::Generators::Base source_paths << File.join(File.dirname(__FILE__), 'templates') + def self.next_migration_number(dirname) + ::ActiveRecord::Generators::Base.next_migration_number(dirname) + end + + def self.migration_version + if Rails.version.start_with?('5') + "[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]" + end + end + def create_migration_file - migration_template 'migration.rb', 'db/migrate/create_flipper_tables.rb' + migration_template 'migration.rb', 'db/migrate/create_flipper_tables.rb', migration_version: migration_version end - def self.next_migration_number(dirname) - ::ActiveRecord::Generators::Base.next_migration_number(dirname) + def migration_version + self.class.migration_version end end end diff --git a/lib/generators/flipper/templates/migration.rb b/lib/generators/flipper/templates/migration.rb index 5041e1f31..1b6d0e3df 100644 --- a/lib/generators/flipper/templates/migration.rb +++ b/lib/generators/flipper/templates/migration.rb @@ -1,4 +1,4 @@ -class CreateFlipperTables < ActiveRecord::Migration +class CreateFlipperTables < ActiveRecord::Migration<%= migration_version %> def self.up create_table :flipper_features do |t| t.string :key, null: false diff --git a/spec/flipper/adapters/active_record_spec.rb b/spec/flipper/adapters/active_record_spec.rb index bd780089d..0742168dc 100644 --- a/spec/flipper/adapters/active_record_spec.rb +++ b/spec/flipper/adapters/active_record_spec.rb @@ -5,8 +5,6 @@ # Turn off migration logging for specs ActiveRecord::Migration.verbose = false -require 'generators/flipper/templates/migration' - RSpec.describe Flipper::Adapters::ActiveRecord do subject { described_class.new } @@ -16,11 +14,34 @@ end before(:each) do - CreateFlipperTables.up + ActiveRecord::Base.connection.execute <<-SQL + CREATE TABLE flipper_features ( + id integer PRIMARY KEY, + key text NOT NULL UNIQUE, + created_at datetime NOT NULL, + updated_at datetime NOT NULL + ) + SQL + + ActiveRecord::Base.connection.execute <<-SQL + CREATE TABLE flipper_gates ( + id integer PRIMARY KEY, + feature_key text NOT NULL, + key text NOT NULL, + value text DEFAULT NULL, + created_at datetime NOT NULL, + updated_at datetime NOT NULL + ) + SQL + + ActiveRecord::Base.connection.execute <<-SQL + CREATE UNIQUE INDEX index_gates_on_keys_and_value on flipper_gates (feature_key, key, value) + SQL end after(:each) do - CreateFlipperTables.down + ActiveRecord::Base.connection.execute("DROP table IF EXISTS `flipper_features`") + ActiveRecord::Base.connection.execute("DROP table IF EXISTS `flipper_gates`") end it_should_behave_like 'a flipper adapter' diff --git a/test/adapters/active_record_test.rb b/test/adapters/active_record_test.rb index bd54437a1..b40c62469 100644 --- a/test/adapters/active_record_test.rb +++ b/test/adapters/active_record_test.rb @@ -1,6 +1,5 @@ require 'test_helper' require 'flipper/adapters/active_record' -require 'generators/flipper/templates/migration' # Turn off migration logging for specs ActiveRecord::Migration.verbose = false @@ -13,10 +12,34 @@ class ActiveRecordTest < MiniTest::Test def setup @adapter = Flipper::Adapters::ActiveRecord.new - CreateFlipperTables.up + + ActiveRecord::Base.connection.execute <<-SQL + CREATE TABLE flipper_features ( + id integer PRIMARY KEY, + key text NOT NULL UNIQUE, + created_at datetime NOT NULL, + updated_at datetime NOT NULL + ) + SQL + + ActiveRecord::Base.connection.execute <<-SQL + CREATE TABLE flipper_gates ( + id integer PRIMARY KEY, + feature_key text NOT NULL, + key text NOT NULL, + value text DEFAULT NULL, + created_at datetime NOT NULL, + updated_at datetime NOT NULL + ) + SQL + + ActiveRecord::Base.connection.execute <<-SQL + CREATE UNIQUE INDEX index_gates_on_keys_and_value on flipper_gates (feature_key, key, value) + SQL end def teardown - CreateFlipperTables.down + ActiveRecord::Base.connection.execute("DROP table IF EXISTS `flipper_features`") + ActiveRecord::Base.connection.execute("DROP table IF EXISTS `flipper_gates`") end end diff --git a/test/generators/flipper/active_record_generator_test.rb b/test/generators/flipper/active_record_generator_test.rb index 5be316f41..c58bc0199 100644 --- a/test/generators/flipper/active_record_generator_test.rb +++ b/test/generators/flipper/active_record_generator_test.rb @@ -10,8 +10,13 @@ class FlipperActiveRecordGeneratorTest < Rails::Generators::TestCase def test_generates_migration run_generator + migration_version = if Rails::VERSION::MAJOR.to_i < 5 + "" + else + "[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]" + end assert_migration 'db/migrate/create_flipper_tables.rb', <<-EOM -class CreateFlipperTables < ActiveRecord::Migration +class CreateFlipperTables < ActiveRecord::Migration#{migration_version} def self.up create_table :flipper_features do |t| t.string :key, null: false