Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Heroku hangs during migrate with strange pg_advisory_unlock

I'm using Rails 5.1 hosted on Heroku, and I use the following command to migrate my database:

heroku run rake db:migrate -a [my app name]

All the migrations themselves complete correctly:

SQL (1.6ms)  INSERT INTO "schema_migrations" ("version") VALUES ($1) RETURNING "version"  [["version", "20180504164326"]]
   (2.1ms)  COMMIT
Migrating to AddPinToStaff (20180519024721)
   (1.5ms)  BEGIN
== 20180519024721 AddPinToStaff: migrating ====================================
-- add_column(:staff, :pin_number, :string)
   (4.2ms)  ALTER TABLE "staff" ADD "pin_number" character varying
   -> 0.0045s
== 20180519024721 AddPinToStaff: migrated (0.0046s) ===========================

That's the last migration file I created, so it all seems to work. Then, this runs:

SQL (1.6ms)  INSERT INTO "schema_migrations" ("version") VALUES ($1) RETURNING "version"  [["version", "20180519024721"]]
(2.4ms)  COMMIT
ActiveRecord::InternalMetadata Load (1.7ms)  SELECT  "ar_internal_metadata".* FROM "ar_internal_metadata" WHERE "ar_internal_metadata"."key" = $1 LIMIT $2  [["key", "environment"], ["LIMIT", 1]]
(1.3ms)  BEGIN
SQL (1.6ms)  INSERT INTO "ar_internal_metadata" ("key", "value", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "key"  [["key", "environment"], ["value", "beta"], ["created_at", "2018-06-04 18:54:24.766405"], ["updated_at", "2018-06-04 18:54:24.766405"]]
(2.1ms)  COMMIT
(1.4ms)  SELECT pg_advisory_unlock(5988010931190918735)

And it hangs there at that last SELECT statement. What is pg_advisory_unlock and why is it running? Reading this blog post it seems like those should be called from my application somewhere, but I can't find any similar text in my application anywhere. Please help!

like image 762
Onikoroshi Avatar asked Jun 04 '18 19:06

Onikoroshi


People also ask

Does heroku run migrations automatically?

Heroku makes deploying Ruby web app a breeze, however it doesn't run rake db:migrate automatically each time you push a new commit to it.


1 Answers

pg_advisory_unlock is nothing but a postgresql lock that Heroku is using to obtain a transaction level lock on your database. It is not necessary that if Heroku is unable to obtain the lock the migration itself was unsuccessful. Please check if your schema contains the migrated tables, if yes, you don't need to do anything more. Edit: Your logs say the migrations were commited, which is a strong indication that the migration was indeed successful.

Otherwise, you can try dropping your database and re-creating it, then running the migrations again. If it contains important data already, use heroku pg:backups:capture --app <name-of-app> to backup your database, then run rails db:drop, rails db:create, and finally, rails db:migrate in order to run the migration again. You can restore the database using heroku pg:backups:restore <name-of-backup> --app <name-of-app>

like image 85
a3y3 Avatar answered Oct 10 '22 09:10

a3y3