-
-
Notifications
You must be signed in to change notification settings - Fork 422
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'update-generator' into mysql-fix
* update-generator: Update changelog and warning message Add tests for update migrations Add `generate flipper:update` to add migrations Fix running of generator test, update test
- Loading branch information
Showing
9 changed files
with
199 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
lib/generators/flipper/templates/update/migrations/01_create_flipper_tables.rb.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
class CreateFlipperTables < ActiveRecord::Migration<%= migration_version %> | ||
def up | ||
create_table :flipper_features do |t| | ||
t.string :key, null: false | ||
t.timestamps null: false | ||
end | ||
add_index :flipper_features, :key, unique: true | ||
|
||
create_table :flipper_gates do |t| | ||
t.string :feature_key, null: false | ||
t.string :key, null: false | ||
t.string :value | ||
t.timestamps null: false | ||
end | ||
add_index :flipper_gates, [:feature_key, :key, :value], unique: true | ||
end | ||
|
||
def down | ||
drop_table :flipper_gates | ||
drop_table :flipper_features | ||
end | ||
end |
14 changes: 14 additions & 0 deletions
14
...nerators/flipper/templates/update/migrations/02_change_flipper_gates_value_to_text.rb.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# frozen_string_literal: true | ||
|
||
class ChangeFlipperGatesValueToText < ActiveRecord::Migration<%= migration_version %> | ||
def up | ||
# Ensure this incremental update migration is idempotent | ||
return unless connection.column_exists?(:flipper_gates, :value, :string) | ||
|
||
change_column :flipper_gates, :value, :text | ||
end | ||
|
||
def down | ||
change_column :flipper_gates, :value, :string | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails/generators' | ||
require 'rails/generators/active_record' | ||
|
||
module Flipper | ||
module Generators | ||
# | ||
# Rails generator used for updating Flipper in a Rails application. | ||
# Run it with +bin/rails g flipper:update+ in your console. | ||
# | ||
class UpdateGenerator < Rails::Generators::Base | ||
include ActiveRecord::Generators::Migration | ||
|
||
TEMPLATES = File.join(File.dirname(__FILE__), 'templates/update') | ||
source_paths << TEMPLATES | ||
|
||
# Generates incremental migration files unless they already exist. | ||
# All migrations should be idempotent e.g. +add_index+ is guarded with +if_index_exists?+ | ||
def update_migration_files | ||
migration_templates = Dir.children(File.join(TEMPLATES, 'migrations')).sort | ||
migration_templates.each do |template_file| | ||
destination_file = template_file.match(/^\d*_(.*\.rb)/)[1] # 01_create_flipper_tables.rb.erb => create_flipper_tables.rb | ||
migration_template "migrations/#{template_file}", File.join(db_migrate_path, destination_file), skip: true | ||
end | ||
end | ||
|
||
private | ||
|
||
def migration_version | ||
"[#{ActiveRecord::VERSION::STRING.to_f}]" | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
require "helper" | ||
require "generators/flipper/update_generator" | ||
|
||
class BasicsGeneratorTest < Rails::Generators::TestCase | ||
tests Flipper::Generators::UpdateGenerator | ||
ROOT = File.expand_path("../../../../tmp/generators", __FILE__) | ||
destination ROOT | ||
setup :prepare_destination | ||
|
||
setup do | ||
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:') | ||
end | ||
|
||
teardown do | ||
ActiveRecord::Base.connection.close | ||
end | ||
|
||
test "generates migrations" do | ||
run_generator | ||
|
||
assert_migration "db/migrate/create_flipper_tables.rb" do |migration| | ||
assert_method :up, migration do |up| | ||
assert_match(/create_table :flipper_features/, up) | ||
assert_match(/create_table :flipper_gates/, up) | ||
end | ||
|
||
assert_method :down, migration do |down| | ||
assert_match(/drop_table :flipper_features/, down) | ||
assert_match(/drop_table :flipper_gates/, down) | ||
end | ||
end | ||
|
||
assert_migration "db/migrate/change_flipper_gates_value_to_text.rb" do |migration| | ||
[:up, :down].each do |dir| | ||
assert_method :up, migration do |method| | ||
assert_match(/change_column/, method) | ||
end | ||
end | ||
end | ||
|
||
require_migrations | ||
|
||
silence { CreateFlipperTables.migrate(:up) } | ||
assert ActiveRecord::Base.connection.table_exists?(:flipper_features) | ||
assert ActiveRecord::Base.connection.table_exists?(:flipper_gates) | ||
|
||
assert ActiveRecord::Base.connection.column_exists?(:flipper_gates, :value, :string) | ||
silence { ChangeFlipperGatesValueToText.migrate(:up) } | ||
assert ActiveRecord::Base.connection.column_exists?(:flipper_gates, :value, :text) | ||
|
||
silence { ChangeFlipperGatesValueToText.migrate(:down) } | ||
assert ActiveRecord::Base.connection.column_exists?(:flipper_gates, :value, :string) | ||
|
||
silence { CreateFlipperTables.migrate(:down) } | ||
refute ActiveRecord::Base.connection.table_exists?(:flipper_features) | ||
refute ActiveRecord::Base.connection.table_exists?(:flipper_gates) | ||
end | ||
|
||
test "ChangeFlipperGatesValueToText is a noop if value is already text" do | ||
self.class.generator_class = Flipper::Generators::ActiveRecordGenerator | ||
run_generator | ||
|
||
self.class.generator_class = Flipper::Generators::UpdateGenerator | ||
run_generator | ||
|
||
assert_migration "db/migrate/create_flipper_tables.rb" do |migration| | ||
assert_method :up, migration do |up| | ||
assert_match /text :value/, up | ||
end | ||
end | ||
|
||
assert_migration "db/migrate/change_flipper_gates_value_to_text.rb" | ||
|
||
require_migrations | ||
|
||
silence { CreateFlipperTables.migrate(:up) } | ||
assert ActiveRecord::Base.connection.column_exists?(:flipper_gates, :value, :text) | ||
|
||
assert_nothing_raised do | ||
silence { ChangeFlipperGatesValueToText.migrate(:up) } | ||
end | ||
assert ActiveRecord::Base.connection.column_exists?(:flipper_gates, :value, :text) | ||
end | ||
|
||
def require_migrations | ||
# If these are not reloaded, then test order can cause failures | ||
Object.send(:remove_const, :CreateFlipperTables) if defined?(::CreateFlipperTables) | ||
Object.send(:remove_const, :ChangeFlipperGatesValueToText) if defined?(::ChangeFlipperGatesValueToText) | ||
|
||
Dir.glob("#{ROOT}/db/migrate/*.rb").each do |file| | ||
assert_nothing_raised do | ||
load file | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters