Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use empty hash string for metadata default value in both Rails 4 and 5 #316

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions lib/generators/statesman/generator_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def model_file_name
end

def migration_class_name
klass.gsub(/::/, "").pluralize
klass.demodulize.pluralize
end

def next_migration_number
Expand Down 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? %>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we sure that it's just MySQL that works this way? Would we be better off with a whitelist of adapters that need this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It makes sense to introduce a whitelist of adapters for more flexibility. However, now Statesman is tested for using Active Record with Postgres/MySQL and only MySQL needs this fix, so I think it is enough to use this fix as-is at now.

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 @@ -8,6 +8,8 @@
end

describe "creates a migration" do
extend Statesman::GeneratorHelpers

subject(:migration) { file("db/migrate/#{time}_create_bacon_transitions.rb") }

before do
Expand All @@ -21,29 +23,45 @@
it "includes a foreign key" do
expect(migration).to contain("add_foreign_key :bacon_transitions, :bacons")
end

it "does not include the metadata default value when using MySQL", if: mysql? do
expect(migration).to_not contain(/default: "{}"/)
end

it "includes the metadata default value when other than MySQL", unless: mysql? do
expect(migration).to contain(/default: "{}"/)
end

it "properly migrates the schema" do
require file("db/migrate/#{time}_create_bacon_transitions.rb")
expect { CreateBaconTransitions.new.up }.to_not raise_error
end
end

describe "properly adds class names" do
before { run_generator %w[Yummy::Bacon Yummy::BaconTransition] }
subject { file("app/models/yummy/bacon_transition.rb") }

before { run_generator %w[Yummy::Bacon Yummy::BaconTransition] }

it { is_expected.to contain(/:bacon_transition/) }
it { is_expected.to_not contain(%r{:yummy/bacon}) }
it { is_expected.to contain(/class_name: 'Yummy::Bacon'/) }
end

describe "properly formats without class names" do
before { run_generator %w[Bacon BaconTransition] }
subject { file("app/models/bacon_transition.rb") }

before { run_generator %w[Bacon BaconTransition] }

it { is_expected.to_not contain(/class_name:/) }
it { is_expected.to contain(/class BaconTransition/) }
end

describe "it doesn't create any double-spacing" do
before { run_generator %w[Yummy::Bacon Yummy::BaconTransition] }
subject { file("app/models/yummy/bacon_transition.rb") }

before { run_generator %w[Yummy::Bacon Yummy::BaconTransition] }

it { is_expected.to_not contain(/\n\n\n/) }
end
end
15 changes: 15 additions & 0 deletions spec/generators/statesman/migration_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
end

describe "the model contains the correct words" do
extend Statesman::GeneratorHelpers

subject(:migration) do
file(
"db/migrate/#{migration_number}_add_statesman_to_bacon_transitions.rb",
Expand All @@ -29,6 +31,14 @@
it { is_expected.to_not 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(migration).to_not contain(/default: "{}"/)
end

it "includes the metadata default value when other than MySQL", unless: mysql? do
expect(migration).to contain(/default: "{}"/)
end

it "names the sorting index appropriately" do
expect(migration).
to contain("name: \"index_bacon_transitions_parent_sort\"")
Expand All @@ -38,5 +48,10 @@
expect(migration).
to contain("name: \"index_bacon_transitions_parent_most_recent\"")
end

it "properly migrates the schema" do
require file("db/migrate/#{migration_number}_add_statesman_to_bacon_transitions.rb")
expect { AddStatesmanToBaconTransitions.new.up }.to_not raise_error
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@

describe Statesman::MongoidTransitionGenerator, type: :generator do
describe "the model contains the correct words" do
before { run_generator %w[Yummy::Bacon Yummy::BaconTransition] }
subject { file("app/models/yummy/bacon_transition.rb") }

before { run_generator %w[Yummy::Bacon Yummy::BaconTransition] }

it { is_expected.to_not contain(%r{:yummy/bacon}) }
it { is_expected.to contain(/class_name: 'Yummy::Bacon'/) }
end

describe "the model contains the correct words" do
before { run_generator %w[Bacon BaconTransition] }
subject { file("app/models/bacon_transition.rb") }

before { run_generator %w[Bacon BaconTransition] }

it { is_expected.to_not contain(/class_name:/) }
it { is_expected.to_not contain(/CreateYummy::Bacon/) }
end
Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "statesman"
require "generators/statesman/generator_helpers"
require "sqlite3"
require "mysql2"
require "pg"
Expand Down
1 change: 1 addition & 0 deletions spec/statesman/adapters/active_record_queries_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def self.initial_state
MyActiveRecordModel.send(:has_one, :other_active_record_model)
OtherActiveRecordModel.send(:belongs_to, :my_active_record_model)
end

after { Statesman.configure { storage_adapter(Statesman::Adapters::Memory) } }

let!(:model) do
Expand Down
4 changes: 4 additions & 0 deletions spec/statesman/adapters/active_record_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
end

before { MyActiveRecordModelTransition.serialize(:metadata, JSON) }

let(:observer) { double(Statesman::Machine, execute: nil) }
let(:model) { MyActiveRecordModel.create(current_state: :pending) }

Expand Down Expand Up @@ -283,6 +284,7 @@

context "after then creating a new transition" do
before { adapter.create(:y, :z, []) }

it "retrieves the new transition from the database" do
expect(adapter.last.to_state).to eq("z")
end
Expand Down Expand Up @@ -329,6 +331,7 @@

context "with a pre-fetched transition history" do
before { adapter.create(:x, :y) }

before { model.my_active_record_model_transitions.load_target }

it "doesn't query the database" do
Expand All @@ -347,6 +350,7 @@
before do
MyNamespace::MyActiveRecordModelTransition.serialize(:metadata, JSON)
end

let(:observer) { double(Statesman::Machine, execute: nil) }
let(:model) do
MyNamespace::MyActiveRecordModel.create(current_state: :pending)
Expand Down
3 changes: 3 additions & 0 deletions spec/statesman/adapters/mongoid_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

describe Statesman::Adapters::Mongoid, mongo: true do
after { Mongoid.purge! }

let(:observer) { double(Statesman::Machine, execute: nil) }
let(:model) { MyMongoidModel.create(current_state: :pending) }

Expand Down Expand Up @@ -34,6 +35,7 @@

context "with a previously looked up transition" do
before { adapter.create(:x, :y) }

before { adapter.last }

it "caches the transition" do
Expand All @@ -44,6 +46,7 @@

context "and a new transition" do
before { adapter.create(:y, :z) }

it "retrieves the new transition from the database" do
expect(adapter.last.to_state).to eq("z")
end
Expand Down
5 changes: 4 additions & 1 deletion spec/statesman/adapters/shared_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@

context "with a previous transition" do
before { adapter.create(from, to) }

its(:sort_key) { is_expected.to be(20) }
end
end
Expand Down Expand Up @@ -117,9 +118,11 @@
end

describe "#last" do
subject { adapter.last }

before { adapter.create(:x, :y) }

before { adapter.create(:y, :z) }
subject { adapter.last }

it { is_expected.to be_a(transition_class) }
specify { expect(adapter.last.to_state.to_sym).to eq(:z) }
Expand Down
Loading