bin/rails db:create で ActiveRecord::NoDatabaseError: Unknown database が出る


## 対象

MySQL と ActsAsTaggableOn を使っている方。

Postgresqlを使っている人はこっちで解決できるかも

https://stackoverflow.com/questions/25611004/rake-dbcreate-throws-database-does-not-exist-error-with-postgresql

## 要約

`config/initializers/acts_as_taggable_on.rb` の

```ruby
ActsAsTaggableOn.force_binary_collation = true
```
をコメントアウトするとエラーが起きなくなる。

## 何をして起こったか

いつもは `reset` しているが
ある動作確認のために
開発環境で `bin/rails db:drop` を実行して
その後 `bin/rails db:create` した際に起きた

```

$ bin/rails db:create

rails aborted!
ActiveRecord::NoDatabaseError: Unknown database 'hogehoge_development'
```

## 解決

本題
原因を調べてもわからず、StackTraceをもう一度読んでみると `acts-as-taggable-on` が異質な感じ。

```
rails aborted!
ActiveRecord::NoDatabaseError: Unknown database 'hogehoge_development'
/Users/username/projects/hogehoge/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/connection_adapters/mysql2_adapter.rb:26:in `rescue in mysql2_connection'
/Users/username/projects/hogehoge/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/connection_adapters/mysql2_adapter.rb:12:in `mysql2_connection'
/Users/username/projects/hogehoge/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:811:in `new_connection'
/Users/username/projects/hogehoge/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:855:in `checkout_new_connection'
/Users/username/projects/hogehoge/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:834:in `try_to_checkout_new_connection'
/Users/username/projects/hogehoge/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:795:in `acquire_connection'
/Users/username/projects/hogehoge/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:523:in `checkout'
/Users/username/projects/hogehoge/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:382:in `connection'
/Users/username/projects/hogehoge/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:1010:in `retrieve_connection'
/Users/username/projects/hogehoge/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/connection_handling.rb:118:in `retrieve_connection'
/Users/username/projects/hogehoge/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.2.1/lib/active_record/connection_handling.rb:90:in `connection'
/Users/username/projects/hogehoge/vendor/bundle/ruby/2.5.0/gems/acts-as-taggable-on-6.0.0/lib/acts_as_taggable_on/utils.rb:8:in `connection'
/Users/username/projects/hogehoge/vendor/bundle/ruby/2.5.0/gems/acts-as-taggable-on-6.0.0/lib/acts_as_taggable_on/utils.rb:16:in `using_mysql?'
/Users/username/projects/hogehoge/vendor/bundle/ruby/2.5.0/gems/acts-as-taggable-on-6.0.0/lib/acts-as-taggable-on.rb:94:in `force_binary_collation='
/Users/username/projects/hogehoge/vendor/bundle/ruby/2.5.0/gems/acts-as-taggable-on-6.0.0/lib/acts-as-taggable-on.rb:50:in `method_missing'
/Users/username/projects/hogehoge/config/initializers/acts_as_taggable_on.rb:1:in `<main>'
/Users/username/projects/hogehoge/vendor/bundle/ruby/2.5.0/gems/bootsnap-1.4.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:50:in `load'
/Users/username/projects/hogehoge/vendor/bundle/ruby/2.5.0/gems/bootsnap-1.4.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:50:in `load'
/Users/username/projects/hogehoge/vendor/bundle/ruby/2.5.0/gems/activesupport-5.2.2.1/lib/active_support/dependencies.rb:285:in `block in load'
/Users/username/projects/hogehoge/vendor/bundle/ruby/2.5.0/gems/activesupport-5.2.2.1/lib/active_support/dependencies.rb:257:in `load_dependency'
/Users/username/projects/hogehoge/vendor/bundle/ruby/2.5.0/gems/activesupport-5.2.2.1/lib/active_support/dependencies.rb:285:in `load'
/Users/username/projects/hogehoge/vendor/bundle/ruby/2.5.0/gems/railties-5.2.2.1/lib/rails/engine.rb:657:in `block in load_config_initializer'
/Users/username/projects/hogehoge/vendor/bundle/ruby/2.5.0/gems/activesupport-5.2.2.1/lib/active_support/notifications.rb:170:in `instrument'
/Users/username/projects/hogehoge/vendor/bundle/ruby/2.5.0/gems/railties-5.2.2.1/lib/rails/engine.rb:656:in `load_config_initializer'
/Users/username/projects/hogehoge/vendor/bundle/ruby/2.5.0/gems/railties-5.2.2.1/lib/rails/engine.rb:614:in `block (2 levels) in <class:Engine>'
/Users/username/projects/hogehoge/vendor/bundle/ruby/2.5.0/gems/railties-5.2.2.1/lib/rails/engine.rb:613:in `each'
/Users/username/projects/hogehoge/vendor/bundle/ruby/2.5.0/gems/railties-5.2.2.1/lib/rails/engine.rb:613:in `block in <class:Engine>'
/Users/username/projects/hogehoge/vendor/bundle/ruby/2.5.0/gems/railties-5.2.2.1/lib/rails/initializable.rb:32:in `instance_exec'
/Users/username/projects/hogehoge/vendor/bundle/ruby/2.5.0/gems/railties-5.2.2.1/lib/rails/initializable.rb:32:in `run'
/Users/username/projects/hogehoge/vendor/bundle/ruby/2.5.0/gems/railties-5.2.2.1/lib/rails/initializable.rb:61:in `block in run_initializers'
/Users/username/projects/hogehoge/vendor/bundle/ruby/2.5.0/gems/railties-5.2.2.1/lib/rails/initializable.rb:50:in `each'
/Users/username/projects/hogehoge/vendor/bundle/ruby/2.5.0/gems/railties-5.2.2.1/lib/rails/initializable.rb:50:in `tsort_each_child'
/Users/username/projects/hogehoge/vendor/bundle/ruby/2.5.0/gems/railties-5.2.2.1/lib/rails/initializable.rb:60:in `run_initializers'
/Users/username/projects/hogehoge/vendor/bundle/ruby/2.5.0/gems/railties-5.2.2.1/lib/rails/application.rb:361:in `initialize!'
/Users/username/projects/hogehoge/config/environment.rb:5:in `<main>'
/Users/username/projects/hogehoge/vendor/bundle/ruby/2.5.0/gems/bootsnap-1.4.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `require'
/Users/username/projects/hogehoge/vendor/bundle/ruby/2.5.0/gems/bootsnap-1.4.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `block in require_with_bootsnap_lfi'
/Users/username/projects/hogehoge/vendor/bundle/ruby/2.5.0/gems/bootsnap-1.4.1/lib/bootsnap/load_path_cache/loaded_features_index.rb:83:in `register'
/Users/username/projects/hogehoge/vendor/bundle/ruby/2.5.0/gems/bootsnap-1.4.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:20:in `require_with_bootsnap_lfi'
/Users/username/projects/hogehoge/vendor/bundle/ruby/2.5.0/gems/bootsnap-1.4.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:29:in `require'
/Users/username/projects/hogehoge/vendor/bundle/ruby/2.5.0/gems/activesupport-5.2.2.1/lib/active_support/dependencies.rb:291:in `block in require'
/Users/username/projects/hogehoge/vendor/bundle/ruby/2.5.0/gems/activesupport-5.2.2.1/lib/active_support/dependencies.rb:257:in `load_dependency'
/Users/username/projects/hogehoge/vendor/bundle/ruby/2.5.0/gems/activesupport-5.2.2.1/lib/active_support/dependencies.rb:291:in `require'
/Users/username/projects/hogehoge/vendor/bundle/ruby/2.5.0/gems/railties-5.2.2.1/lib/rails/application.rb:337:in `require_environment!'
/Users/username/projects/hogehoge/vendor/bundle/ruby/2.5.0/gems/railties-5.2.2.1/lib/rails/application.rb:520:in `block in run_tasks_blocks'
/Users/username/projects/hogehoge/vendor/bundle/ruby/2.5.0/gems/railties-5.2.2.1/lib/rails/commands/rake/rake_command.rb:23:in `block in perform'
/Users/username/projects/hogehoge/vendor/bundle/ruby/2.5.0/gems/railties-5.2.2.1/lib/rails/commands/rake/rake_command.rb:20:in `perform'
/Users/username/projects/hogehoge/vendor/bundle/ruby/2.5.0/gems/railties-5.2.2.1/lib/rails/command.rb:48:in `invoke'
/Users/username/projects/hogehoge/vendor/bundle/ruby/2.5.0/gems/railties-5.2.2.1/lib/rails/commands.rb:18:in `<main>'
/Users/username/projects/hogehoge/vendor/bundle/ruby/2.5.0/gems/bootsnap-1.4.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `require'
/Users/username/projects/hogehoge/vendor/bundle/ruby/2.5.0/gems/bootsnap-1.4.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `block in require_with_bootsnap_lfi'
/Users/username/projects/hogehoge/vendor/bundle/ruby/2.5.0/gems/bootsnap-1.4.1/lib/bootsnap/load_path_cache/loaded_features_index.rb:83:in `register'
/Users/username/projects/hogehoge/vendor/bundle/ruby/2.5.0/gems/bootsnap-1.4.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:20:in `require_with_bootsnap_lfi'
/Users/username/projects/hogehoge/vendor/bundle/ruby/2.5.0/gems/bootsnap-1.4.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:29:in `require'
/Users/username/projects/hogehoge/vendor/bundle/ruby/2.5.0/gems/activesupport-5.2.2.1/lib/active_support/dependencies.rb:291:in `block in require'
/Users/username/projects/hogehoge/vendor/bundle/ruby/2.5.0/gems/activesupport-5.2.2.1/lib/active_support/dependencies.rb:257:in `load_dependency'
/Users/username/projects/hogehoge/vendor/bundle/ruby/2.5.0/gems/activesupport-5.2.2.1/lib/active_support/dependencies.rb:291:in `require'
bin/rails:4:in `<main>'
```

project内で`config/initializers/acts_as_taggable_on.rb` に

```ruby
ActsAsTaggableOn.force_binary_collation = true
```
こんな定義があった

公式をみるとMySQLユーザーに対して推奨している設定みたい。ざっくりとしか読んでないけど特殊文字対策と書いてある。
https://github.com/mbleigh/acts-as-taggable-on#for-mysql-users

試しにコメントアウトし
もう一度 `bin/rails db:create` を実行したところ成功した。

実装をみてみると `using_mysql?` のところで mysql に connectionしていてここで起きているのかな。
`acts-as-taggable-on.rb`

```ruby

    def force_binary_collation=(force_bin)
      if Utils.using_mysql?
        if force_bin
          Configuration.apply_binary_collation(true)
          @force_binary_collation = true
          @strict_case_match = true
        else
          Configuration.apply_binary_collation(false)
          @force_binary_collation = false
        end
      end
    end
```

StackTraceをさらに読み進めるとここでエラーになってます。

`active_record/connection_adapters/mysql2_adapter.rb`

```ruby

    def mysql2_connection(config)
      config = config.symbolize_keys
      config[:flags] ||= 0

      if config[:flags].kind_of? Array
        config[:flags].push "FOUND_ROWS".freeze
      else
        config[:flags] |= Mysql2::Client::FOUND_ROWS
      end

      client = Mysql2::Client.new(config)
      ConnectionAdapters::Mysql2Adapter.new(client, logger, nil, config)
    rescue Mysql2::Error => error
      if error.message.include?("Unknown database")
        raise ActiveRecord::NoDatabaseError
      else
        raise
      end
    end
```

調べてみると他の人もエラーになっているようだった。

https://github.com/mbleigh/acts-as-taggable-on/issues/905

## 教訓

エラーはStackTrace含めてちゃんと読もう。

時間があるときにもう少しちゃんと読んで解決策考えられると良いかな。
まぁ割と早めに気づけてよかったです。