Skip to content

Commit

Permalink
Set the default value of metadata as empty hash string
Browse files Browse the repository at this point in the history
  • Loading branch information
kymmt90 committed Apr 24, 2018
1 parent 0a05a0b commit cd28db0
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 6 deletions.
4 changes: 0 additions & 4 deletions lib/generators/statesman/generator_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion lib/generators/statesman/templates/create_migration.rb.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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? %>
Expand Down
2 changes: 1 addition & 1 deletion lib/generators/statesman/templates/update_migration.rb.erb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions spec/generators/statesman/migration_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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\"")
Expand Down
4 changes: 4 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit cd28db0

Please sign in to comment.