9

I am using PostgreSQL, Rails 3.1.3 and Ruby 1.9.3. I am struggling to use db:migrate as outlined here.

This is what I am seeing in the terminal:

funkdified@funkdified-laptop:~/railsprojects/hartl$ bundle exec rake db:migrate --trace 
** Invoke db:migrate (first_time) 
** Invoke environment (first_time) 
** Execute environment 
** Invoke db:load_config (first_time) 
** Invoke rails_env (first_time) 
** Execute rails_env 
** Execute db:load_config 
** Execute db:migrate 
== AddEmailUniquenessIndex: migrating ======================================== 
-- add_index(:users, :email, {:unique=>true})

and then the code hangs at this point. Any ideas why?

From: development.log

[1m[36m (0.1ms)[0m [1mSHOW search_path[0m 
[1m[35m (0.5ms)[0m SELECT "schema_migrations"."version" FROM "schema_migrations"
Migrating to CreateUsers (20120124022843) 
Migrating to AddEmailUniquenessIndex (20120124093922) 
[1m[36m (0.1ms)[0m [1mBEGIN[0m 
[1m[35m (3.6ms)[0m SELECT distinct i.relname, d.indisunique, d.indkey, t.oid 
FROM pg_class t 
INNER JOIN pg_index d ON t.oid = d.indrelid 
INNER JOIN pg_class i ON d.indexrelid = i.oid 
WHERE i.relkind = 'i' 
AND d.indisprimary = 'f' 
AND t.relname = 'users' 
AND i.relnamespace IN (SELECT oid FROM pg_namespace WHERE nspname = ANY (current_schemas(false)) ) 
ORDER BY i.relname
Chris
  • 44,602
  • 16
  • 137
  • 156
Abram
  • 39,950
  • 26
  • 134
  • 184
  • 1
    Can you log in a an appropriately privileged PostgreSQL user, and do `select * from pg_stat_activity` – derobert Jan 24 '12 at 16:17
  • I sure can. I've just executed this query through pgAdmin III: -- Executing query: select * from pg_stat_activity Total query runtime: 11 ms. 4 rows retrieved. – Abram Jan 24 '12 at 19:45
  • @derobert, thanks for your response, but the migration has worked for me this morning. Nothing different - same migration file. Perhaps the computer just needed a restart? Strange. – Abram Jan 24 '12 at 19:49
  • Well, glad it worked... Its possible PosgreSQL's autovacuum or auto-stats collection kicked in overnight. BTW: The idea of the pg_stat_activity select was that you'd see what query the migration was running. Sort of defeated when you just pasted the row count, without the rows... – derobert Jan 24 '12 at 21:23

4 Answers4

17

I just had a similar problem, where a very simple migration was stalling for no apparent reason. I believe the problem has to do with not being able to get a database connection. I exited a rails console session that I had open in another terminal and then the migration immediately finished with no problems.

jgautsch
  • 291
  • 2
  • 5
  • 3
    My solution was the same as well. I closed my connection in another terminal tab and the migration ended immediately. – Sisi Nov 14 '14 at 22:34
  • Same here. I've been facing this issue several times and it was because I had a rails console terminal window in another tab. Closed it, run migration and it succesfully finished. – Francisco Quintero Jun 30 '15 at 15:53
7

I had same problem .. I found out that there was idle transaction which blocked further queries on this table ..

Run:

heroku pg:ps --app=...

To view database processes. You will have to kill idle process:

heroku pg:kill 913 --force --app=...

(913 is ID of idle process -> change it to your needs)

knagode
  • 5,816
  • 5
  • 49
  • 65
  • Before running this stuff, try the migration after ctrl c'ing your rails server and any open rails consoles if you have them. – Peter Berg Jul 10 '14 at 22:44
1

I had the same issue and after trying a bunch of solutions what worked was...shutting down all of my terminal sessions, restarting the computer, and trying again. Sometimes it's just power cycle magic.

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 07 '22 at 22:18
0

I just made two migrations. The first one created a new table, the second one removed fields from an existing table. The second migration was hanging, and the reason turned out to be a rails console session (rails console --sandbox) running in another terminal windows.

Jussi Hirvi
  • 565
  • 3
  • 6