From cd28db09c666bbe0f4e53a99e5ac2f2a376249ed Mon Sep 17 00:00:00 2001 From: Kohei Yamamoto Date: Sun, 15 Apr 2018 19:28:53 +0900 Subject: [PATCH] Set the default value of metadata as empty hash string --- lib/generators/statesman/generator_helpers.rb | 4 ---- .../statesman/templates/create_migration.rb.erb | 2 +- .../statesman/templates/update_migration.rb.erb | 2 +- .../statesman/active_record_transition_generator_spec.rb | 8 ++++++++ spec/generators/statesman/migration_generator_spec.rb | 8 ++++++++ spec/spec_helper.rb | 4 ++++ 6 files changed, 22 insertions(+), 6 deletions(-) diff --git a/lib/generators/statesman/generator_helpers.rb b/lib/generators/statesman/generator_helpers.rb index ad2dd3de..d36d8590 100644 --- a/lib/generators/statesman/generator_helpers.rb +++ b/lib/generators/statesman/generator_helpers.rb @@ -44,9 +44,5 @@ def mysql? def database_supports_partial_indexes? Statesman::Adapters::ActiveRecord.database_supports_partial_indexes? end - - def metadata_default_value - Utils.rails_5_or_higher? ? "{}" : "{}".inspect - end end end diff --git a/lib/generators/statesman/templates/create_migration.rb.erb b/lib/generators/statesman/templates/create_migration.rb.erb index c538adaa..93fe5253 100644 --- a/lib/generators/statesman/templates/create_migration.rb.erb +++ b/lib/generators/statesman/templates/create_migration.rb.erb @@ -2,7 +2,7 @@ class Create<%= migration_class_name %> < ActiveRecord::Migration<%= "[#{ActiveR def change create_table :<%= table_name %> do |t| t.string :to_state, null: false - t.text :metadata<%= ", default: #{metadata_default_value}" unless mysql? %> + t.text :metadata<%= ", default: \"{}\"" unless mysql? %> t.integer :sort_key, null: false t.integer :<%= parent_id %>, null: false t.boolean :most_recent<%= ", null: false" if database_supports_partial_indexes? %> diff --git a/lib/generators/statesman/templates/update_migration.rb.erb b/lib/generators/statesman/templates/update_migration.rb.erb index 399b1762..9ce90327 100644 --- a/lib/generators/statesman/templates/update_migration.rb.erb +++ b/lib/generators/statesman/templates/update_migration.rb.erb @@ -1,7 +1,7 @@ class AddStatesmanTo<%= migration_class_name %> < ActiveRecord::Migration<%= "[#{ActiveRecord::Migration.current_version}]" if Statesman::Utils.rails_5_or_higher? %> def change add_column :<%= table_name %>, :to_state, :string, null: false - add_column :<%= table_name %>, :metadata, :text<%= ", default: #{metadata_default_value}" unless mysql? %> + add_column :<%= table_name %>, :metadata, :text<%= ", default: \"{}\"" unless mysql? %> add_column :<%= table_name %>, :sort_key, :integer, null: false add_column :<%= table_name %>, :<%= parent_id %>, :integer, null: false add_column :<%= table_name %>, :most_recent, null: false diff --git a/spec/generators/statesman/active_record_transition_generator_spec.rb b/spec/generators/statesman/active_record_transition_generator_spec.rb index ec5569c0..2c8f5dda 100644 --- a/spec/generators/statesman/active_record_transition_generator_spec.rb +++ b/spec/generators/statesman/active_record_transition_generator_spec.rb @@ -19,6 +19,14 @@ it "includes a foreign key" do expect(subject).to contain("add_foreign_key :bacon_transitions, :bacons") end + + it "does not include the metadata default value when using MySQL", if: mysql? do + expect(subject).not_to contain(/default: "{}"/) + end + + it "includes the metadata default value when other than MySQL", unless: mysql? do + expect(subject).to contain(/default: "{}"/) + end end describe "properly adds class names" do diff --git a/spec/generators/statesman/migration_generator_spec.rb b/spec/generators/statesman/migration_generator_spec.rb index b3f2c4ba..13c94af6 100644 --- a/spec/generators/statesman/migration_generator_spec.rb +++ b/spec/generators/statesman/migration_generator_spec.rb @@ -29,6 +29,14 @@ it { is_expected.not_to contain(%r{:yummy/bacon}) } it { is_expected.to contain(/null: false/) } + it "does not include the metadata default value when using MySQL", if: mysql? do + expect(subject).not_to contain(/default: "{}"/) + end + + it "includes the metadata default value when other than MySQL", unless: mysql? do + expect(subject).to contain(/default: "{}"/) + end + it "names the sorting index appropriately" do expect(subject). to contain("name: \"index_bacon_transitions_parent_sort\"") diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 11b57c9e..a3fa1fff 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -27,6 +27,10 @@ def connection_failure end end + def mysql? + Class.new.extend(Statesman::GeneratorHelpers).mysql? + end + if config.exclusion_filter[:mongo] puts "Skipping Mongo tests" else