Heroku database migrations failing with "unable to connect" - ruby

I'm having a heck of a time getting a Postgres database on a heroku instance to allow me to run database migrations.
I'm also incredibly new to Heroku, so apologies in advance if this is a silly question :)
So my app is on Heroku and can successfully connect to its database - it just can't do anything because the tables aren't set up. My connect block in the code looks like this:
if ENV['DATABASE_URL']
ActiveRecord::Base.establish_connection(ENV['DATABASE_URL'])
else
ActiveRecord::Base.establish_connection(dbconfig['development'])
end
The environment variable is set by Heroku at deploy time - this also works. If I jump in after everything is initialized with Pry or something, connection works great. The dbconfig hash is populated from my db/config.yml which works fine locally.
I can rake db:migrate on my local system just fine. However, trying this via heroku run rake db:migrate gives me an error dump beginning with:
rake aborted!
could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
The only other odd thing I can think of here is that I'm using a gem called standalone migrations (declared in Gemfile) which should allow me to do this without pulling in the rest of Rails.
So, my question, why aren't my migrations running?

Related

How can I get Heroku's database information in a ruby script?

I created a rails app that is hosted on Heroku. Now I want to upload a ruby script that will populate my database.
This script already exists and is using
PGconn.connect( :hostaddr=>"127.0.0.1", :port=>5432, :dbname=>"myDB", :user=>"MyUser", :password=>'MyPass');
This will obviously not work on Heroku.
I tried to get it using:
Rails.configuration.database_configuration
But I get
uninitialized constant Rails
How can I get the information so I can connect to my Heroku DB? Is there any way to make the same code wotk both on my localhost and on Heroku?
(ps: I am using foreman and the .ENV file, if it helps)
Edit:
The solution I took:
require 'yaml'
require 'erb'
config = YAML.load(ERB.new(File.read(File.join("config","database.yml"))).result)
In config I have all the information I need to perform the DB access.
Why would you not just access ENV['DATABASE_URL'] and break it up into the constituent parts? To see how this is done, if you do heroku run bash and do cat config\database.yml you will see how Heroku does this itself.
To ensure that locally it works as well if you execute via foreman run <Scriptname> then it will be executed and the contents of your .env will be loaded.

heroku rake db:structure:load failure

I need to use some PostgreSQL proprietary features such as rules and triggers for table partitioning. As long as I know, these kind of features cannot be dump to schema.rb so I have changed my schema_format configuration parameter to :sql.
Now, when I try to load rake db:structure:load to load the generated structure.sql into the heroku database, it fails saying: sh: psql: not found
How can I do it?
You can use pg:psql to run the script from your development machine against the database:
cd your-rails-project
heroku pg:psql -a your-app-name <db/structure.sql
Just make sure that the branch you have checked out locally is the same as the one you have deployed.

Why cant I run my rails app using heroku locally?

I am working on a new project that is using heroku, I get this error when I try to run the app locally:
Moped::Errors::ConnectionFailure at / Could not connect to any secondary or primary nodes for replica set <Moped::Cluster nodes=[<Moped::Node resolved_address="127.0.0.1:27017">]>
and I think it has something to do with Heroku because if I type
$ heroku info,
i get this error:
! You do not have access to the app disrupt.
I can't find this error anywhere on the internet, on any forums and there is nothing in the Heroku documentation.
The error normally just means that the MongoDB server is not running locally. Try starting it up by running mongod. Otherwise check you mongoid.yml to see how the development and test environments are set up to connect to MongoDB.
The other error that you get from heroku looks like a separate issue. As tolgap suggests you may just not be authenticated with heroku.

When or where in a custom Heroku buildpack are config vars available in the environment?

I understand that running a rake task that expects the environment to fully work requires that the DB connection can actually be stablished. Basically, that's the reason why Heroku needs asset pipeline precompilations to run before the environment can be loaded.
That's why this line is needed to deploy rails 3.X apps to the Cedar:
config.assets.initialize_on_precompile = false
Now, I'm trying to build a custom build pack that needs a rake task to do other assets compilations to make the app work as expected. This is because I have internationalized JS asset files and use https://github.com/fnando/i18n-js
I need to run rake i18n:js:export and that requires the environment. *Is there any point in the buildpack where I can add this call and be sure that the connection to the DB of the Rails app will work? * Loading the environment is as a requirement of this task and I don't know how to work it out and it gives the typical error:
rake aborted!
could not connect to server: Connection refused
Is the server running on host "127.0.0.1" and accepting
TCP/IP connections on port 5432?
Naturally, I can compile locally and then deploy. But the buildpack seems like the right tool to solve this matter in an automated way. Thanks for the help!
Check out the user env Heroku labs feature: https://devcenter.heroku.com/articles/labs-user-env-compile

Heroku throws SQLITE3 Read only exception

After I deploy an app to Heroku, I run migration scripts and get this error message
...ites\padrino\prophetmargin> heroku rake ar:migrate
rake aborted!
SQLite3::ReadOnlyException: attempt to write a readonly database: CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL)
/disk1/home/slugs/215264_925fd2c_65a3/mnt/.bundle/gems/gems/padrino-core-0.9.11/lib/padrino-core/cli/rake.rb:9:in `init'
How can this be? I also tried running heroku dbpush sqlite://db/my-db.db and that also did not work.
heroku doesn't use sqlite3 but postgres. I'm not sure why you're getting this error though as I use sqlite3 in devel and when pushing to heroku they do some magic which migrates over to postgres.
I'm not exactly sure how Heroku does this db backend 'swap' but it looks like it's not happening for you as it's trying to write out the sqlite db file which obviously fails due to Heroku's read-only file system.
Sorry this isn't much of an answer, you may actually know all this already, but if you're new to heroku, it might give you some insight?
hmm... just noticed... what's the ar:migrate command? I haven't run Heroku for a few months which changes all the time, but normally you'd want a heroku rake db:migrate

Resources