David Rubin
2012-Jul-17 12:59 UTC
Database connection parameters are tied to the filesystem
HI Guys, I recently tried VERY hard to override the database configuration bassed on ENV vars or actual API calls. This task is almost next to impossible. I know that the database.yml file is parsed as ERB but that is sane for simple ENV replacement if you want to actually change which database adaptor it is (requires extra config/params) it becomes messy. I found https://github.com/rails/rails/issues/5297 which explains the issue/solution far better then I could. Active record supports this natively, I can''t see why rails would force the requirement of a database.yml and for it to have a "production" section. see http://apidock.com/rails/ActiveRecord/Base/establish_connection/class URL section. Regards David -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-core/-/8qOi7BryxukJ. To post to this group, send email to rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
Nicolás Sanguinetti
2012-Jul-17 15:17 UTC
Re: Database connection parameters are tied to the filesystem
On Tue, Jul 17, 2012 at 9:59 AM, David Rubin <davidrub@gmail.com> wrote:> HI Guys, > > I recently tried VERY hard to override the database configuration bassed on > ENV vars or actual API calls. This task is almost next to impossible. > > I know that the database.yml file is parsed as ERB but that is sane for > simple ENV replacement if you want to actually change which database adaptor > it is (requires extra config/params) it becomes messy. > > I found https://github.com/rails/rails/issues/5297 which explains the > issue/solution far better then I could. > > Active record supports this natively, I can''t see why rails would force the > requirement of a database.yml and for it to have a "production" section. see > http://apidock.com/rails/ActiveRecord/Base/establish_connection/class URL > section.As I mentioned on the ticket, I would rather have ENV["DATABASE_URL"] supported as well, but I can''t see how it''s "VERY hard to override". See https://github.com/rails/rails/issues/5297#issuecomment-7037942 I''ve been using variations of that for a long time, and it works pretty well. Cheers, -foca> Regards > > David > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Core" group. > To view this discussion on the web visit > https://groups.google.com/d/msg/rubyonrails-core/-/8qOi7BryxukJ. > To post to this group, send email to rubyonrails-core@googlegroups.com. > To unsubscribe from this group, send email to > rubyonrails-core+unsubscribe@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/rubyonrails-core?hl=en.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
Aaron Patterson
2012-Jul-17 18:27 UTC
Re: Database connection parameters are tied to the filesystem
On Tue, Jul 17, 2012 at 12:17:26PM -0300, Nicolás Sanguinetti wrote:> On Tue, Jul 17, 2012 at 9:59 AM, David Rubin <davidrub@gmail.com> wrote: > > HI Guys, > > > > I recently tried VERY hard to override the database configuration bassed on > > ENV vars or actual API calls. This task is almost next to impossible. > > > > I know that the database.yml file is parsed as ERB but that is sane for > > simple ENV replacement if you want to actually change which database adaptor > > it is (requires extra config/params) it becomes messy. > > > > I found https://github.com/rails/rails/issues/5297 which explains the > > issue/solution far better then I could. > > > > Active record supports this natively, I can''t see why rails would force the > > requirement of a database.yml and for it to have a "production" section. see > > http://apidock.com/rails/ActiveRecord/Base/establish_connection/class URL > > section. > > As I mentioned on the ticket, I would rather have ENV["DATABASE_URL"] > supported as well, but I can''t see how it''s "VERY hard to override". > See https://github.com/rails/rails/issues/5297#issuecomment-7037942 > > I''ve been using variations of that for a long time, and it works pretty well.3.2+ should have support for a DATABASE_URL environment variable: https://github.com/rails/rails/blob/master/activerecord/lib/active_record/connection_handling.rb#L39 Which you should be able to use like this: $ DATABASE_URL=''sqlite3:///db/development.sqlite3'' rake do:whatever But that doesn''t help the problem in #5297. The problem in #5297 is that Heroku calculates it''s own parameters to pass in for the URL. AFAIK, there is no way to augment the connection parameters that Heroku sends. But this seems like an issue with Heroku rather than Rails. If you find entry points where the environment variable *isn''t* used, then we should consider it a bug. For example, I just discovered that `rake db:schema:dump` will not use the environment variable: https://github.com/rails/rails/blob/master/activerecord/lib/active_record/railties/databases.rake#L229 So someone should send a patch for that. :-) -- Aaron Patterson http://tenderlovemaking.com/
On Tue, Jul 17, 2012 at 2:27 PM, Aaron Patterson <tenderlove@ruby-lang.org> wrote:> If you find entry points where the environment variable *isn''t* used, > then we should consider it a bug. For example, I just discovered that > `rake db:schema:dump` will not use the environment variable: > https://github.com/rails/rails/blob/master/activerecord/lib/active_record/railties/databases.rake#L229 > > So someone should send a patch for that. :-)Is it simple as: - ActiveRecord::Base.establish_connection(Rails.env) + ActiveRecord::Base.establish_connection(ENV["DATABASE_URL"] || Rails.env) wherever I see it not being used? Also inside namespace :test? Are there tests for rake tasks? -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
Aaron Patterson
2012-Jul-23 17:10 UTC
Re: Database connection parameters are tied to the filesystem
On Sun, Jul 22, 2012 at 12:52:26PM -0400, TuteC wrote:> On Tue, Jul 17, 2012 at 2:27 PM, Aaron Patterson > <tenderlove@ruby-lang.org> wrote: > > If you find entry points where the environment variable *isn''t* used, > > then we should consider it a bug. For example, I just discovered that > > `rake db:schema:dump` will not use the environment variable: > > https://github.com/rails/rails/blob/master/activerecord/lib/active_record/railties/databases.rake#L229 > > > > So someone should send a patch for that. :-) > > Is it simple as: > > - ActiveRecord::Base.establish_connection(Rails.env) > + ActiveRecord::Base.establish_connection(ENV["DATABASE_URL"] > || Rails.env) > > wherever I see it not being used?Yes, but I think it would be better if we change the definition of `establish_connection` in connection_handling.rb. Like this: diff --git a/activerecord/lib/active_record/connection_handling.rb b/activerecord/lib/active_record/connection_handling.rb index bda41df..ce1f84f 100644 --- a/activerecord/lib/active_record/connection_handling.rb +++ b/activerecord/lib/active_record/connection_handling.rb @@ -36,7 +36,7 @@ module ActiveRecord # # The exceptions AdapterNotSpecified, AdapterNotFound and ArgumentError # may be returned on an error. - def establish_connection(spec = ENV["DATABASE_URL"]) + def establish_connection(spec = ENV["DATABASE_URL"] || Rails.env) resolver = ConnectionAdapters::ConnectionSpecification::Resolver.new spec, configurations spec = resolver.spec Then we should just call `establish_connection` and it will automatically choose the right thing. (Note that this diff may break stuff, I haven''t run the tests to find out ;-) )> Also inside namespace :test?Yes.> Are there tests for rake tasks?There should be tests in Railties. -- Aaron Patterson http://tenderlovemaking.com/
Grace Liu
2012-Sep-04 17:38 UTC
Re: Database connection parameters are tied to the filesystem
I have submitted a pull request to fix support for DATABASE_URL environment variable: https://github.com/rails/rails/pull/7521 It includes fix for rake db:schema:dump and other rake db tasks, plus new tests to verify correct behavior whether DATABASE_URL environment variable is set or not. All tests were re-run after fix and completed successfully. Any feedback for changes are welcomed. Best, Grace -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-core/-/zA2NFnbBRgQJ. To post to this group, send email to rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
Grace Liu
2012-Sep-04 22:47 UTC
Re: Database connection parameters are tied to the filesystem
I have submitted a pull request to fix DATABASE_URL support: https://github.com/rails/rails/pull/7521 The fix addresses rake db:schema:dump as well as other rake db tasks, and includes new tests to validate behavior for both with and without DATABASE_URL environment variable set. All tests have been run after the fix was applied and completed successfully. Any feedback welcomed. Best, Grace -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-core/-/ij8jdwlnWRAJ. To post to this group, send email to rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.