database.yml &references not working

2019-01-15 08:21发布

问题:

We just upgraded our virtual machines to what I thought was an identical ruby configuration (via RVM... Ruby 1.9.2, Rails 3.0.7, DataMapper 1.1.0). The biggest difference was that we went from MySQL 5.0 to 5.1.

For some reason, the exact same code/database.yml that was working on our old VMs now fails on our new ones at the point it tries to connect to the database.

The issue is that this YAML:

mysql_defaults: &mysql_defaults
  adapter: mysql
  encoding: UTF-8
  username: user
  password: pass
  host: localhost

development:
  <<: *mysql_defaults
  database: devdb

production:
  <<: *mysql_defaults
  database: productiondb
  host: master.db.site.com

Is just expanding to:

  "mysql_defaults" => {
    "adapter"=>"mysql",
    "encoding"=>"UTF-8",
    "username"=>"user",
    "password"=>"pass",
    "host"=>"localhost"
  },
  "development" => {
    "adapter"=>"mysql",
    "encoding"=>"UTF-8",
    "username"=>"user",
    "password"=>"pass",
    "host"=>"localhost"
  },
  "production" => {
    "adapter"=>"mysql",
    "encoding"=>"UTF-8",
    "username"=>"user",
    "password"=>"pass",
    "host"=>"localhost"
  }

Instead of:

  "mysql_defaults" => {
    "adapter"=>"mysql",
    "encoding"=>"UTF-8",
    "username"=>"user",
    "password"=>"pass",
    "host"=>"localhost"
  },
  "development" => {
    "adapter"=>"mysql",
    "encoding"=>"UTF-8",
    "username"=>"user",
    "password"=>"pass",
    "host"=>"localhost",
    "database"=>"devdb"
  },
  "production" => {
    "adapter"=>"mysql",
    "encoding"=>"UTF-8",
    "username"=>"user",
    "password"=>"pass",
    "host"=>"master.db.site.com",
    "database"=>"productiondb"
  }

Anyone experienced this before?

According to Gemfile.lock (I deleted it and ran bundle install again, just for sanity's sake), all the installed dependencies are the same (i.e. the Gemfile.lock does not diff between the old and the new setup). Nor does the database.yml.

回答1:

Psych is the new YAML parser which is presumably better but can't merge hash keys.

This should help http://pivotallabs.com/users/mkocher/blog/articles/1692-yaml-psych-and-ruby-1-9-2-p180-here-there-be-dragons



回答2:

Since you have done an upgrade it may be that your database permissions have messed up. Try viewing that you have the necessary permissions i.e the machine on which the code resides has privileges to connect and modify on the database machine. Looking at you database.yml it should be something like" GRANT ALL PRIVILEGES ON productionbd.* to 'user'@'<app-server-ip>' identified by 'pass';