Dearg
Dearg

Reputation: 47

`retrieve_connection': No connection pool for Model (ActiveRecord::ConnectionNotEstablished)

I have a simple Ruby script i use successfully locally to import a CSV file to my model. This works in development but fails in Production (Heroku)

I can't seem to get past the following error:

`retrieve_connection': No connection pool for Company (ActiveRecord::ConnectionNotEstablished)

open_csv_prod.rb

require 'csv'
require_relative 'helpers'
require 'active_record'
require 'rails'

# Connect to database
ActiveRecord::Base.configurations[Rails.env] || Rails.application.config.database_configuration[Rails.env]

class Company < ActiveRecord::Base
print_memory_usage do
print_time_spent do


  #open the file
    filename = 'lib/assets/1/BasicCompanyData-2016-09-01-part1_5.csv' 
  # count how many rows? 
    line_count = `wc -l "#{filename}"`.strip.split(' ')[0].to_i
  # Print that result  
    puts "#{line_count}"

    count = 0


    CSV.foreach(filename, headers: true) do |row|
      Company.create! row.to_hash
      count += 1
      p "#{count}"
    end

  end
  end 
  end

heroku run ruby open_csv_prod.rb

This results in the reported error above. I have no idea why Heroku would complain about a pool as this is listed when i check ActiveRecord::Base.configurations[Rails.env] in Heroku Rails console.

I should say i have tried this with the following database configuration but it also fails because heroku stores database information in [DATABASE_URL].

ActiveRecord::Base.establish_connection(
adapter: 'postgresql',
host:     ENV['IP'],
username: ENV['USERNAME'],
password: ENV['PASSWORD'],
database: 'dbb_development'
)

It is possible i am trying to connect all wrong, if so i just need to write to my Company model in a ruby script executed in the terminal.

Upvotes: 1

Views: 1929

Answers (1)

Shiyason
Shiyason

Reputation: 781

This is a longshot but, you said that heroku store the DB config info in a URL. The following snippet is from the rails docs

http://api.rubyonrails.org/classes/ActiveRecord/ConnectionHandling.html

ActiveRecord::Base.establish_connection(
  "postgres://myuser:mypass@localhost/somedatabase"
)

Maybe you can connect like this:

ActiveRecord::Base.establish_connection(
  ENV['DATABASE_URL']
)

Upvotes: 2

Related Questions