@ledsun blog

Hのキーがhellで、Sのキーがslaveだ、と彼は思った。そしてYのキーがyouだ。

Ruby 2.5とRails3.2のときrake db:createでString can't be coerced into Integerが発生する

現象

~ bundle exec rake db:create
Deprecation warning: Expected string default value for '--quiet'; got false (boolean).
This will be rejected in the future unless you explicitly pass the options `check_default_type: false` or call `allow_incompatible_default_type!` in your code
You can silence deprecations warning by setting the environment variable THOR_SILENCE_DEPRECATION.
Deprecation warning: Expected string default value for '--syslog'; got false (boolean).
This will be rejected in the future unless you explicitly pass the options `check_default_type: false` or call `allow_incompatible_default_type!` in your code
You can silence deprecations warning by setting the environment variable THOR_SILENCE_DEPRECATION.
Deprecation warning: Expected string default value for '--logfile'; got true (boolean).
This will be rejected in the future unless you explicitly pass the options `check_default_type: false` or call `allow_incompatible_default_type!` in your code
You can silence deprecations warning by setting the environment variable THOR_SILENCE_DEPRECATION.
String can't be coerced into Integer
/Users/shigerunakajima/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/activesupport-3.2.22.2/lib/active_support/core_ext/enumerable.rb:60:in `+'
/Users/shigerunakajima/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/activesupport-3.2.22.2/lib/active_support/core_ext/enumerable.rb:60:in `sum'
/Users/shigerunakajima/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/activesupport-3.2.22.2/lib/active_support/core_ext/enumerable.rb:60:in `sum'
/Users/shigerunakajima/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/activerecord-3.2.22.2/lib/active_record/connection_adapters/postgresql_adapter.rb:747:in `create_database'
/Users/shigerunakajima/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/activerecord-3.2.22.2/lib/active_record/railties/databases.rake:144:in `rescue in create_database'
/Users/shigerunakajima/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/activerecord-3.2.22.2/lib/active_record/railties/databases.rake:84:in `create_database'
/Users/shigerunakajima/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/activerecord-3.2.22.2/lib/active_record/railties/databases.rake:62:in `block (3 levels) in <top (required)>'
/Users/shigerunakajima/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/activerecord-3.2.22.2/lib/active_record/railties/databases.rake:62:in `each'
/Users/shigerunakajima/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/activerecord-3.2.22.2/lib/active_record/railties/databases.rake:62:in `block (2 levels) in <top (required)>'
/Users/shigerunakajima/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/rake-10.5.0/lib/rake/task.rb:240:in `block in execute'
/Users/shigerunakajima/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/rake-10.5.0/lib/rake/task.rb:235:in `each'
/Users/shigerunakajima/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/rake-10.5.0/lib/rake/task.rb:235:in `execute'
/Users/shigerunakajima/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/rake-10.5.0/lib/rake/task.rb:179:in `block in invoke_with_call_chain'
/Users/shigerunakajima/.rbenv/versions/2.5.7/lib/ruby/2.5.0/monitor.rb:235:in `mon_synchronize'
/Users/shigerunakajima/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/rake-10.5.0/lib/rake/task.rb:172:in `invoke_with_call_chain'
/Users/shigerunakajima/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/rake-10.5.0/lib/rake/task.rb:165:in `invoke'
/Users/shigerunakajima/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/rake-10.5.0/lib/rake/application.rb:150:in `invoke_task'
/Users/shigerunakajima/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/rake-10.5.0/lib/rake/application.rb:106:in `block (2 levels) in top_level'
/Users/shigerunakajima/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/rake-10.5.0/lib/rake/application.rb:106:in `each'
/Users/shigerunakajima/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/rake-10.5.0/lib/rake/application.rb:106:in `block in top_level'
/Users/shigerunakajima/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/rake-10.5.0/lib/rake/application.rb:115:in `run_with_threads'
/Users/shigerunakajima/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/rake-10.5.0/lib/rake/application.rb:100:in `top_level'
/Users/shigerunakajima/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/rake-10.5.0/lib/rake/application.rb:78:in `block in run'
/Users/shigerunakajima/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/rake-10.5.0/lib/rake/application.rb:176:in `standard_exception_handling'
/Users/shigerunakajima/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/rake-10.5.0/lib/rake/application.rb:75:in `run'
/Users/shigerunakajima/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/rake-10.5.0/bin/rake:33:in `<top (required)>'
/Users/shigerunakajima/.rbenv/versions/2.5.7/bin/rake:23:in `load'
/Users/shigerunakajima/.rbenv/versions/2.5.7/bin/rake:23:in `<top (required)>'
/Users/shigerunakajima/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/bundler-1.17.3/lib/bundler/cli/exec.rb:74:in `load'
/Users/shigerunakajima/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/bundler-1.17.3/lib/bundler/cli/exec.rb:74:in `kernel_load'
/Users/shigerunakajima/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/bundler-1.17.3/lib/bundler/cli/exec.rb:28:in `run'
/Users/shigerunakajima/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/bundler-1.17.3/lib/bundler/cli.rb:463:in `exec'
/Users/shigerunakajima/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/bundler-1.17.3/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
/Users/shigerunakajima/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/bundler-1.17.3/lib/bundler/vendor/thor/lib/thor/invocation.rb:126:in `invoke_command'
/Users/shigerunakajima/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/bundler-1.17.3/lib/bundler/vendor/thor/lib/thor.rb:387:in `dispatch'
/Users/shigerunakajima/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/bundler-1.17.3/lib/bundler/cli.rb:27:in `dispatch'
/Users/shigerunakajima/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/bundler-1.17.3/lib/bundler/vendor/thor/lib/thor/base.rb:466:in `start'
/Users/shigerunakajima/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/bundler-1.17.3/lib/bundler/cli.rb:18:in `start'
/Users/shigerunakajima/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/bundler-1.17.3/exe/bundle:30:in `block in <top (required)>'
/Users/shigerunakajima/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/bundler-1.17.3/lib/bundler/friendly_errors.rb:124:in `with_friendly_errors'
/Users/shigerunakajima/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/bundler-1.17.3/exe/bundle:22:in `<top (required)>'
/Users/shigerunakajima/.rbenv/versions/2.5.7/bin/bundle:23:in `load'
/Users/shigerunakajima/.rbenv/versions/2.5.7/bin/bundle:23:in `<main>'
Couldn't create database for {"adapter"=>"postgresql", "encoding"=>"unicode", "database"=>"pubannotation", "pool"=>5, "host"=>"localhost", "username"=>"postgres", "password"=>"password"}
String can't be coerced into Integer
/Users/shigerunakajima/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/activesupport-3.2.22.2/lib/active_support/core_ext/enumerable.rb:60:in `+'
/Users/shigerunakajima/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/activesupport-3.2.22.2/lib/active_support/core_ext/enumerable.rb:60:in `sum'
/Users/shigerunakajima/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/activesupport-3.2.22.2/lib/active_support/core_ext/enumerable.rb:60:in `sum'
/Users/shigerunakajima/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/activerecord-3.2.22.2/lib/active_record/connection_adapters/postgresql_adapter.rb:747:in `create_database'
/Users/shigerunakajima/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/activerecord-3.2.22.2/lib/active_record/railties/databases.rake:144:in `rescue in create_database'
/Users/shigerunakajima/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/activerecord-3.2.22.2/lib/active_record/railties/databases.rake:84:in `create_database'
/Users/shigerunakajima/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/activerecord-3.2.22.2/lib/active_record/railties/databases.rake:62:in `block (3 levels) in <top (required)>'
/Users/shigerunakajima/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/activerecord-3.2.22.2/lib/active_record/railties/databases.rake:62:in `each'
/Users/shigerunakajima/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/activerecord-3.2.22.2/lib/active_record/railties/databases.rake:62:in `block (2 levels) in <top (required)>'
/Users/shigerunakajima/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/rake-10.5.0/lib/rake/task.rb:240:in `block in execute'
/Users/shigerunakajima/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/rake-10.5.0/lib/rake/task.rb:235:in `each'
/Users/shigerunakajima/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/rake-10.5.0/lib/rake/task.rb:235:in `execute'
/Users/shigerunakajima/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/rake-10.5.0/lib/rake/task.rb:179:in `block in invoke_with_call_chain'
/Users/shigerunakajima/.rbenv/versions/2.5.7/lib/ruby/2.5.0/monitor.rb:235:in `mon_synchronize'
/Users/shigerunakajima/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/rake-10.5.0/lib/rake/task.rb:172:in `invoke_with_call_chain'
/Users/shigerunakajima/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/rake-10.5.0/lib/rake/task.rb:165:in `invoke'
/Users/shigerunakajima/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/rake-10.5.0/lib/rake/application.rb:150:in `invoke_task'
/Users/shigerunakajima/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/rake-10.5.0/lib/rake/application.rb:106:in `block (2 levels) in top_level'
/Users/shigerunakajima/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/rake-10.5.0/lib/rake/application.rb:106:in `each'
/Users/shigerunakajima/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/rake-10.5.0/lib/rake/application.rb:106:in `block in top_level'
/Users/shigerunakajima/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/rake-10.5.0/lib/rake/application.rb:115:in `run_with_threads'
/Users/shigerunakajima/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/rake-10.5.0/lib/rake/application.rb:100:in `top_level'
/Users/shigerunakajima/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/rake-10.5.0/lib/rake/application.rb:78:in `block in run'
/Users/shigerunakajima/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/rake-10.5.0/lib/rake/application.rb:176:in `standard_exception_handling'
/Users/shigerunakajima/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/rake-10.5.0/lib/rake/application.rb:75:in `run'
/Users/shigerunakajima/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/rake-10.5.0/bin/rake:33:in `<top (required)>'
/Users/shigerunakajima/.rbenv/versions/2.5.7/bin/rake:23:in `load'
/Users/shigerunakajima/.rbenv/versions/2.5.7/bin/rake:23:in `<top (required)>'
/Users/shigerunakajima/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/bundler-1.17.3/lib/bundler/cli/exec.rb:74:in `load'
/Users/shigerunakajima/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/bundler-1.17.3/lib/bundler/cli/exec.rb:74:in `kernel_load'
/Users/shigerunakajima/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/bundler-1.17.3/lib/bundler/cli/exec.rb:28:in `run'
/Users/shigerunakajima/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/bundler-1.17.3/lib/bundler/cli.rb:463:in `exec'
/Users/shigerunakajima/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/bundler-1.17.3/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
/Users/shigerunakajima/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/bundler-1.17.3/lib/bundler/vendor/thor/lib/thor/invocation.rb:126:in `invoke_command'
/Users/shigerunakajima/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/bundler-1.17.3/lib/bundler/vendor/thor/lib/thor.rb:387:in `dispatch'
/Users/shigerunakajima/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/bundler-1.17.3/lib/bundler/cli.rb:27:in `dispatch'
/Users/shigerunakajima/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/bundler-1.17.3/lib/bundler/vendor/thor/lib/thor/base.rb:466:in `start'
/Users/shigerunakajima/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/bundler-1.17.3/lib/bundler/cli.rb:18:in `start'
/Users/shigerunakajima/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/bundler-1.17.3/exe/bundle:30:in `block in <top (required)>'
/Users/shigerunakajima/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/bundler-1.17.3/lib/bundler/friendly_errors.rb:124:in `with_friendly_errors'
/Users/shigerunakajima/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/bundler-1.17.3/exe/bundle:22:in `<top (required)>'
/Users/shigerunakajima/.rbenv/versions/2.5.7/bin/bundle:23:in `load'
/Users/shigerunakajima/.rbenv/versions/2.5.7/bin/bundle:23:in `<main>'
Couldn't create database for {"adapter"=>"postgresql", "encoding"=>"unicode", "database"=>"pubannotation_test", "pool"=>5, "host"=>"localhost", "username"=>"postgres", "password"=>"password"}

原因

エラーが起きているのは https://github.com/rails/rails/blob/3-2-stable/activesupport/lib/active_support/core_ext/enumerable.rb#L58-L64 です。

 def sum(identity = 0, &block)
    if block_given?
      map(&block).sum(identity)
    else
      inject(:+) || identity
    end
  end

Ruby 2.4で Enumerable#sum が実装されたことが原因のようです。 60行目で呼ばれるsumActiveSupportのEnumerable#sumからRubyのEnumerable#sum に変わるようです。 なぜそうなるのかはよくわかりません。

Ruby 2.5でのみこのエラーが起きることを確認しました。 おそらくRuby2.4でも起きると思います。

対策

rake db:create以外は正常に動くので、psqlPostgreSQLにログインして、次のSQLを実行してdatabaseを作成しました。

CREATE DATABASE "pubannotation" ENCODING = 'unicode';
CREATE DATABASE "pubannotation_test" ENCODING = 'unicode';

参考