Heroku run rake db:migrate results in no change in the database, app restarted several times - heroku

I have a problem with pushing my migrations to the production database.
The issue:
I've altered database schema by adding 1 column.
I've migrated it to the production database:
MacBook-Air-Mac:app msc$ rake db:migrate RAILS_ENV="production"
[RailsAdmin] RailsAdmin initialization disabled by default. Pass SKIP_RAILS_ADMIN_INITIALIZER=false if you need it.
== AddLengthColumnToBooks: migrating =========================================
-- add_column(:books, :length, :integer)
-> 0.0017s
== AddLengthColumnToBooks: migrated (0.0019s) ================================
Thinking that the new DB schema is now in production, I've deployed the code which does some things with :length.
In production, I got the following error:
undefined method `length=' for #
I did heroku rollback and downgraded the app to the latest reliable version.
THEN (a bit too late probably) I found out that I have to heroku restart the app to load the new indexes. I did this several times.
I opened the console then and checked Book.column_names, but there was no length
I did heroku run rake db:migrate followed by heroku restart one more time, no change.
I've tried migrating another column to the production db, but didn't get any message at all, not even the one from p.2.
What am I doing wrong here?
Update
Based on the answers of Philipe, I did a number of additional steps:
git add db/schema.rb, git add db/migrate/20130325103953_add_length_column_to_books.rb
and 'git add db/migrate/20130401041910_add_duration_column_to_books.rb'. Git's answer was:
Changes to be committed:
(use "git reset HEAD ..." to unstage)
new file: db/migrate/20130325103953_add_length_column_to_books.rb
new file: db/migrate/20130401041910_add_duration_column_to_books.rb
modified: db/schema.rb
Then I did git commit -m "Updating the schema".
Again the output was:
3 files changed, 168 insertions(+), 156 deletions(-)
create mode 100644 db/migrate/20130325103953_add_length_column_to_books.rb
create mode 100644 db/migrate/20130401041910_add_duration_column_to_books.rb
Then I run heroku run rake db:migrate. Unfortunately there was no sign of migrations, simply got:
Running rake db:migrate attached to terminal... up, run.5428
and that's it.
In the production Rails Console, running Book.column_names still lacks both length and duration.
Now I'm even more out of ideas.
`

It doesn't look like you are pushing changes to Heroku. Steps should be as follows;
Make changes to local code
Run any migrations LOCALLY
Add all changed files to Git git add .
Commit all added files to git git commit -m "Adding features"
Push the changes to Heroku git push heroku master - assuming you are using heroku as your remote name and you are working in the master branch
If you have migrations run heroku run rake db:migrate to run the migrations ON HEROKU
Following migrations do heroku restart
That should be all you need to get you working.

I had the same problem; I git added the migrations, git pushed them to the server, cap deployed them, and the database didn't change. Logged in to the server directly, ran rake db:migrate, and the command line seemed to run the migration, but nothing changed.
In my case, somehow rake db:migrate was using the wrong RAILS_ENV. I logged in to the server, and ran
rake db:migrate RAILS_ENV=production
This caused the database to create the new columns, and then all of the code that I had debugged in the test database started working on the server.

I just had same problem. After adding a column to my DB locally, I did heroku run rake db:migrate -app [my app name]. Running my code on production, I got ActiveRecord::UnknownAttributeError (unknown attribute '_____' for [table name].)
This solved my problem:
heroku restart --app [my app name]

if you want to run the migration on Heroku for your postgresql database you can add this line in your Procfile
release: alembic upgrade head

My two cents based on my experience: I thought that heroku run db:migrate also migrated db content into production. No! Only structure. So in my case, log in was not working in production because there were no users in it. I had to sign test user up again and then it worked. Hope it helps rookies like me out there

Related

Redeploy Heroku app without code changes

I would like to deploy a Heroku app which will be done ideally using git push -u heroku master. However this will only work if there are any pending commits to be pushed to master.
How can I redeploy the app while there is nothing to push ? I tried git push -u heroku master -f and still get the same below
Branch master set up to track remote branch master from heroku.
Everything up-to-date
PS: I also want to retain the existing app, which means I cannot make use of this answer https://stackoverflow.com/a/22043184/968442
Normally setting a config var causes your application to be restarted. In most situations there should be no need to redeploy after doing this.
If you really do need to trigger a new deployment you can add a new empty commit, then push to Heroku again:
git commit --allow-empty -m "Trigger Heroku deploy after enabling collectstatic"
git push heroku master
The new empty commit is a regular commit. It has a hash, an author, a timestamp, etc. It will have the same tree as its parent. This should cause Heroku to build your app slug again using the same code as the previous commit.
It's a bit awkward, but it works.
You can do it from UI as well!
Login to your Heroku dashboard and go to deploy section
Find Manual deploy option
Hit Deploy Branch button!
Note: you must have your app connected to GitHub for this option to be available (see comment from Derek below).
There is now also a plugin for the Heroku command-line that allows you to re-release the most recently deployed slug.
See https://www.npmjs.com/package/heroku-releases-retry
It turns out there is a neat plugin for Heroku called heroku release retry that lets you retry the last deploy without resorting to adding bad commits to your repository.
// install plugin
heroku plugins:install heroku-releases-retry
// retry release
heroku releases:retry --app {your-app}
Source: https://www.darraghoriordan.com/2019/03/02/heroku-push-failed-force-rebuild
You can run heroku restart --app app_name and you are good to go.
This worked for me, it did an actual build and release without any commit, contrary to one other post that only does a release:
heroku plugins:install heroku-builds
heroku builds:create --source-url https://user:token#api.github.com/repos/<username>/<repo name>/tarball/master/ --app <app-name>
Source: https://help.heroku.com/I3E6QPQN/how-do-i-force-a-new-deploy-without-adding-a-commit-to-my-github-repo
For stop the heroku app uses :
$ heroku ps:scale web=0
And for start it uses :
$ heroku ps:scale web=1

What is a good way to run a task whenever a Heroku app restarts?

Use case is to bust the cache.
What is a good way to run given code (or rake task) whenever a Ruby Heroku app is restarted (or deployed)?
There's no way to do this via the Heroku API far as I know. The Heroku Platform API doesn't support this.
What you can do (if you're fast, however!) is listen for a SIGTERM message in your code (that's what Heroku sends to your application process when it attempts to restart it) -- you can then fire off your script quickly.
Here's more information on SIGTERM on Heroku: https://devcenter.heroku.com/articles/dynos#graceful-shutdown-with-sigterm
If you're using some sort of CI, you can probably configure it there. Heres how to do it with CircleCI:
deployment:
production:
branch: production
commands:
- git push git#heroku.com:foo-bar-123.git $CIRCLE_SHA1:master
- heroku run rake <your task> --app <your app name>
If you're not using a CI you can still whip together a script that first does the git push to Heroku and then executes your cache busting task through heroku run (the app's bin/ folder would be an obvious place to put it).
Note: you can also use heroku run:detached, which will send output to your logs instead of stdout.
You can use "release" feature that allows you to run any command before a new release is deployed. https://devcenter.heroku.com/articles/release-phase
Define the command that should be run in your Procfile.
release: rake db:migrate
From documentation:
The release command is run immediately after a release is created, but before the release is deployed to the app’s dyno formation. That means it will be run after an event that creates a new release.

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.

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

How can I pull an existing heroku app to new location for development?

I currently have the latest version of my code on another computer that I want to develop from (Home computer and laptop for when I'm out and about) I set up heroku for my app on my laptop. Now I need to associate my code on my desktop so that I can push to heroku from there as well.
This is what I get from my desktop:
desktop:~/NetBeansProjects/onlinescheduler$ git pull heroku master
fatal: 'heroku' does not appear to be a git repository
fatal: The remote end hung up unexpectedly
I can't do heroku create because that will create a separate app. How do I associated the existing code with (or pull down a brand new version from) heroku?
Whats the command to do this?
Also, If you've never used heroku before on the other machine, you'll need to do a few more things first:
$ gem install heroku
$ heroku login
[then enter your credentials]
$ heroku keys:add [path to keyfile]
Now you can clone the remote repository:
$ git clone git#heroku.com:<heroku_app>.git <local_directory>
First of all, you'll want to follow the Quick Start instructions for Heroku, which you can get straight from the horse's mouth, right here: https://devcenter.heroku.com/articles/quickstart
Once you've gotten through step 3, come back here.
Then, you can type this into the command line:
heroku git:clone -a myapp
This is described here:
https://devcenter.heroku.com/articles/git-clone-heroku-app
Then, if you want to grab the database too, here are some options.
Newer Heroku instructions on import/export:
https://devcenter.heroku.com/articles/heroku-postgres-import-export
Older heroku instructions on push and pull: https://blog.heroku.com/archives/2009/3/18/push_and_pull_databases_to_and_from_heroku
If you are using mongo, this is a useful tool to sync your mongo database: https://github.com/pedro/heroku-mongo-sync#readme
If you first need to get the app from Heroku, clone your app.
To do that, write in your Terminal:
heroku git:clone -a your_app_name
If you already have the app and the remote to heroku follow the next steps. If not, you can check instructions here https://devcenter.heroku.com/articles/git
Find the name of your database
Write in your Terminal:
heroku pg:info -a your_app_name
it will look something like this:
HEROKU_POSTGRESQL_MAROON_URL
Find the name of your local database
In your Rails app go to config/database.yml
it will look something like this:
your_app_name_development
Clone your production database (PostgreSQL)
Write in your Terminal with your own database names:
heroku pg:pull HEROKU_POSTGRESQL_MAROON_URL your_app_name_development -a your_app_name
HEROKU_POSTGRESQL_MAROON_URL is an example of how could be the name of your production database (in Heroku):
my_app_name_development is the name of your development database (locally)
the_name_of_my_app is the name of your app in Heroku
Don't forget to finish this with bundle install...
If you already have your code base ready and have heroku setup, use:
$ heroku git:remote -a your_heroku_app
This will allow you to deploy from your new location.
Reference: https://devcenter.heroku.com/articles/git#creating-a-heroku-remote
Once you create a key in a new computer, you have to upload your new SSH key by typing heroku keys:add.

Resources