rails-sqlserver / heroku-buildpack-freetds

This is a Heroku buildpack for vendoring the FreeTDS binaries into your project.
MIT License
10 stars 31 forks source link

Talk A Little About DATABASE_URL? #1

Open metaskills opened 6 years ago

metaskills commented 6 years ago

I just saw this thread on the adapter and I wonder if it would help to have a small section in the README to talk about DABASE_URL?

https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/issues/497#issuecomment-358516950

bf4 commented 6 years ago

Looks to me like the recommendation for using DATABASE_URL is for the post-erb evaluated database yml to look something like

 adapter: sqlserver
  pool: 5
  timeout: 5000
  encoding: utf-8

production:
  <<: *default
  azure: true
  url: sqlserver://username:password@my-app.database.windows.net:1433/database_name

where azure: true is not specified in the url, because

The default parsing of the DATABASE_URL by rails means that the value for :azure is a string "true" instead of true. All values coming the params of a URL are returned as strings. That is what causes this error. In the code you could !!config[:azure] to ensure its always true or false

https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/blob/000b9c79797a8635a569ab28abfbbc1eca29d2cf/lib/active_record/connection_adapters/sqlserver_adapter.rb#L352

          azure: config[:azure]

and someone posted

When heroku attempts to precompile assets, it uses a fake database url, such that rails won't complain about not having one (in the case none is set) while running rake and bringing up the environment. When it does this, I'm pretty sure it must dynamically try to figure out the scheme from the database adapter, but in this case is not capable of doing so.

It is only an issue for me with Azure where we need to manage the connection differently than through DATABASE_URL (because of the azure:true flag). As a brief workaround, in case anyone googles this and gets stuck, you can try a deploy script like the following that will wrap your git push in an environment toggle.

#!/usr/bin/env bash

# $1 should be environment
# $2 should be branch

# set database url
heroku config:set DATABASE_URL=`heroku config -r $1 | grep -o "\(sqlserver..*\)" | head -n1` -r $1

# push code
git push $1 $2:master

# roll back database url setting
heroku config:unset DATABASE_URL -r $1

# run migations
heroku run rake db:migrate -r $1