How to solve an error in Heroku's config/database.yml file "mapping values are not allowed in this context"

StackOverflow https://stackoverflow.com/questions/18139003

Question

I'm deploying an app to Heroku and it's mostly fine but for this error

mapping values are not allowed in this context at line 22 column 13 (Psych::SyntaxError)

The line they are referring to is line 22 of the database.yml file that Heroku itself is writing over my own file, as per standard practice.

Jumping into heroku run bash and taking a look at the file in question:

<%

require 'cgi'
require 'uri'

begin
  uri = URI.parse(ENV["DATABASE_URL"])
rescue URI::InvalidURIError
  raise "Invalid DATABASE_URL"
end

raise "No RACK_ENV or RAILS_ENV found" unless ENV["RAILS_ENV"] || ENV["RACK_ENV"]

def attribute(name, value, force_string = false)
  if value
    value_string =
      if force_string
        '"' + value + '"'
      else
        value
      end
    "#{name}: #{value_string}"
  else
    ""
  end
end

adapter = uri.scheme
adapter = "postgresql" if adapter == "postgres"

database = (uri.path || "").split("/")[1]

username = uri.user
password = uri.password

host = uri.host
port = uri.port

params = CGI.parse(uri.query || "")

%>

<%= ENV["RAILS_ENV"] || ENV["RACK_ENV"] %>:
  <%= attribute "adapter",  adapter %>
  <%= attribute "database", database %>
  <%= attribute "username", username %>
  <%= attribute "password", password, true %>
  <%= attribute "host",     host %>
  <%= attribute "port",     port %>

<% params.each do |key, value| %>
  <%= key %>: <%= value.first %>
<% end %>

Line 22 is

    "#{name}: #{value_string}"

Any suggestions as to how to resolve this problem?

Was it helpful?

Solution

Okay after some more research I have worked out the problem. Sigh.

On my local machine I am loading my database configuration as follows:

dbconfig = YAML::load(File.open(File.join("config","database.yml")))

but because the yml file that Heroku generates is actually an ERB file in disguise, I need to parse it first.

Changing the above line to:

dbconfig = YAML.load(ERB.new(File.read(File.join("config","database.yml"))).result)

works a treat.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top