-
Notifications
You must be signed in to change notification settings - Fork 163
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
Conversation
@timrogers How about this PR? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It feels like we should ideally have a test that checks our generated migrations work. It feels like a weak spot right now. What do you think?
@@ -46,7 +46,7 @@ def database_supports_partial_indexes? | |||
end | |||
|
|||
def metadata_default_value |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We might as well drop this method and put the correct value directly in the template if we're convinced it works everywhere.
999e955
to
7115a10
Compare
Thank you for your review. I agree with what you are saying, so I will add tests (working in progress...). And I will put |
8c6431e
to
cd28db0
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#317 has been merged, so if you give this a rebase, it may well pass now.
I wonder if we can have tests that actually run the migration? That would be excellent.
@@ -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? %> |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
spec/spec_helper.rb
Outdated
@@ -27,6 +27,10 @@ def connection_failure | |||
end | |||
end | |||
|
|||
def mysql? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this getting defined on Object
? I'm not sure what happens with this stuff inside RSpec.configure
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is getting defined as the class method of example groups. But I noticed that only doing extend Statesman::GeneratorHelpers
in describe
blocks enables to use helper methods, so I fixed it in that clearer way.
cd28db0
to
441cb13
Compare
1e81f29
to
c643460
Compare
Fixed GeneratorHelpers::migration_class_name because a migration file name and its migration class name must match each other
0e6686d
to
47a3158
Compare
fixes #313
This PR changes
Statesman::GeneratorHelpers#metadata_default_value
to return\"{}\"
in both Rails 4 and 5 byStrint#dump
.#metadata_default_value
returns"{}"
when using Rails 5. This places the textdefault: {}
to generated migration files. So if{}
is passed toActiveRecord::ConnectionAdapters::Quoting#_quote
whenbin/rails db:migrate
, it no longer accepts Hash and raisesTypeError
. It seems to avoid unintentional YAML producing.ref: rails/rails@aa31d21
I confirmed that this issue is reproduced with Rails 5.0.7, 5.1.6 and 5.2.0 and not reproduced in Rails 4.2.10.
Rails 4.2.10
Rails 5.0.7 (same issue occurs in Rails 5.1.6 and 5.2.0)
json
andjsonb
types for Postgres allow to use{}
in migration files, but I think it is better to use"{}"
as the default value to be database-agnostic. AndString#inspect
is just for debugging, so I think it is better to useString#dump
.