【问题标题】:Heroku vs YAML - Valid YAML is not being read by HerokuHeroku vs YAML - Heroku 没有读取有效的 YAML
【发布时间】:2016-01-24 06:25:49
【问题描述】:

我整天都在玩空间,但终其一生都无法让我的 Heroku 应用程序迁移数据库。我不断得到

    rake aborted!
    YAML syntax error occurred while parsing /app/config/database.yml. Please note that YAML must be consistently indented using spaces. Tabs are not allowed. Error: (<unknown>): did not find expected key while parsing a block mapping at line 62 column 3

除了我已经通过三个不同的验证器运行我的 YAML 文件并且他们都被告知它们是“有效的”之外,这一切都很好,而且很花哨。

这是已编辑有问题的 YAML

# PostgreSQL. Versions 8.2 and up are supported.
#
# Install the pg driver:
#   gem install pg
# On OS X with Homebrew:
#   gem install pg -- --with-pg-config=/usr/local/bin/pg_config
# On OS X with MacPorts:
#   gem install pg -- --with-pg-config=/opt/local/lib/postgresql84/bin/pg_config
# On Windows:
#   gem install pg
#       Choose the win32 build.
#       Install PostgreSQL and put its /bin directory on your path.
#
# Configure Using Gemfile
# gem 'pg'
#
default: &default
  adapter: postgresql
  encoding: unicode
  pool: 5

development:
  <<: *default
  database: coder-app_title
  username: postgres
  password: 123456
  host: localhost

  username: personal_username
  password: personal_password

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.

test: 
   <<: *default   [[<< This is line 62.]]
   database: coder-add_title_test
   username: postgres
   password: 123456
   host: localhost

# As with config/secrets.yml, you never want to store sensitive information,
# like your database password, in your source code. If your source code is
# ever seen by anyone, they now have access to your database.
#
# Instead, provide the password as a unix environment variable when you boot
# the app. Read http://guides.rubyonrails.org/configuring.html#configuring-a-database
# for a full rundown on how to provide these environment variables in a
# production deployment.
#
# On Heroku and other platform providers, you may have a full connection URL
# available as an environment variable. For example:
#
#   DATABASE_URL="postgres://myuser:mypass@localhost/somedatabase"
#
# You can use this database configuration with:
#
   production:
     url: <%= ENV['DATABASE_URL'] %>
#
production:

【问题讨论】:

  • 第 62 行不是您标记的行,而是文件中的最后一行。
  • 删除文件末尾的两个production: 部分。你不需要它们。 Heroku 在提交后挂钩中将生产数据库设置写入database.yml
  • 您应该考虑使用环境变量“DATABASE_URL”而不是推送数据库配置。

标签: ruby-on-rails ruby heroku yaml


【解决方案1】:

解析器错误是由于缩进不一致。我建议你使用一个不错的文本编辑器,并使用 2 个空格来缩进块。

我不知道您一直在使用什么来验证未捕获错误的 YAML - 将复制粘贴到基于 Web 的验证器中并不能很好地工作。相反,您可以使用 Ruby YAML 模块来解析文件:

# run `$ irb` from the root of project
require 'yaml'
YAML.parse(File.open('./config/development.rb'))

Heroku 还会将生产设置写入文件,因此您的 database.yml 文件中不应包含 production: 部分。

这是一个正确解析的更正版本:

# PostgreSQL. Versions 8.2 and up are supported.
#
# Install the pg driver:
#   gem install pg
# On OS X with Homebrew:
#   gem install pg -- --with-pg-config=/usr/local/bin/pg_config
# On OS X with MacPorts:
#   gem install pg -- --with-pg-config=/opt/local/lib/postgresql84/bin/pg_config
# On Windows:
#   gem install pg
#       Choose the win32 build.
#       Install PostgreSQL and put its /bin directory on your path.
#
# Configure Using Gemfile
# gem 'pg'
#
default: &default
  adapter: postgresql
  encoding: unicode
  pool: 5

development:
  <<: *default
  database: coder-app_title
  username: postgres
  password: 123456
  host: localhost
  username: personal_username
  password: personal_password

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.

test: 
  <<: *default
  database: coder-add_title_test
  username: postgres
  password: 123456
  host: localhost

# As with config/secrets.yml, you never want to store sensitive information,
# like your database password, in your source code. If your source code is
# ever seen by anyone, they now have access to your database.
#
# Instead, provide the password as a unix environment variable when you boot
# the app. Read http://guides.rubyonrails.org/configuring.html#configuring-a-database
# for a full rundown on how to provide these environment variables in a
# production deployment.
#
# NOTE. Heroku will write production settings in a post-commit hook. No configuration needed.

【讨论】:

  • 我会推荐 Atom 作为编辑。它免费并且理解 YAML 和 Ruby。此外,您可能希望使用环境变量而不是检查本地开发 postgres 设置,以便其他开发人员不必设置 postgres 以匹配您的设置。
猜你喜欢
  • 2021-12-31
  • 2019-06-29
  • 2020-10-23
  • 1970-01-01
  • 1970-01-01
  • 2022-12-22
  • 2019-01-04
  • 2011-05-02
  • 2017-11-25
相关资源
最近更新 更多