Ruby on Rails is a very established web framework, and Heroku is a platform as a service that offers for building, running, and operating the applications in the cloud itself.
Since you are reading this, I assume either you have some horrifying task of playing with the production database or are just curious! So let’s get right into it.
This method works on any Postgres plan you have on Heroku.
Follow these 09 steps to upgrade your plan on Heroku and Rails set up and experience how things work wonders.
===========================================================================================================
Step 1: Firing up your Heroku App Console
This step is for absolute beginners; the complete process will be done on Heroku Rails Console.
For accessing your console, use this command :
heroku run rails c --app your-app-name
===========================================================================================================
Step 2: Backing Up Current Database
Perhaps the most important thing you should never forget is to ensure that your current database is safe. Therefore we will start with backing up your database and then downloading it. For this process, use these commands in your console.
heroku pg:backups:capture --app your-app-name heroku pg:backups:download --app your-app-name
===========================================================================================================
Step 3: Get your application’s current information.
Since we are done with backing up the database, now we need the current information of the application regarding Postgres.
pg:info command comes in handy here.
I would suggest saving the output of this as it will be helpful later.
heroku pg:info --app your-app-name
===========================================================================================================
Step 4: Creating your new database
After you get the information from Postgres, you need to create another database with a required plan ( for me, it’s hobby-basic ). Find the command below:
// use pg:wait command to notify you when provisioning ( database creation ) is complete. //
heroku pg:wait --app your-app-name> HEROKU_POSTGRESQL_SILVER_URL //Sample o/p – your’s can be different
===========================================================================================================
Step 5: Enter maintenance mode
Until now, all the above processes didn’t affect your current database, but now we will be copying all the data from our existing database to the newly created database. Since we don’t miss out on any entries, we have to take down our website. Yes, I feel sad too.
//Scaling workers to 0 to ensure that delay jobs don’t write to our database. //
heroku ps:scale worker=0 --app your-app-name//Entering maintenance mode
Heroku maintenance:on --app your-app-name===========================================================================================================
Step 6: Transfer data from old DB to new DB
//This command copies all the content from DATABASE_URL(Current Database name which you will find in pg:info command ) to HEROKU_POSTGRESQL_SILVER_URL
heroku pg:copy DATABASE_URL HEROKU_POSTGRESQL_SILVER_URL --app your-app-name//Enter app name again to confirm when it prompts.
your-app-name===========================================================================================================
Step 7: Promote the new DB / connect the DB to live
Since we have copied all the data to the new database and are ready to use it, we need to promote it. It means we are attaching this newly created database as a primary database to our application.
Heroku pg:promote HEROKU_POSTGRESQL_SILVER_URL --app your-app-name===========================================================================================================
Step 8: Exit maintenance mode
Everything is done, and our newly created database is ready to use. Now it’s time to bring our app back to life. I love this step 3000. At this point, anxiety must be kicking in along with that smirk of hoping it will work; anyway, let’s get this thing running. To do so, follow me.
//Scaling workers to 1 to ensure workers start writing to the database
heroku ps:scale worker=1 --app your-app-name// Finally exiting maintenance mode
heroku maintenance:off --app your-app-name===========================================================================================================
Step 9: Deprovisioning old database
Since we have moved to our new database and don’t need the old database anymore, we can delete it. Follow this to achieve it.
( Not mandatory )
You can use this database as a follower too.
heroku addons:destroy HEROKU_POSTGRESQL_CRIMSON_URL --app your-app-nameRun pg:info command to find your application’s database url, HEROKU_POSTGRESQL_CRIMSON_URL in my case.
===========================================================================================================
But wait. There’s more!
Bonus: Add follower database
To add a follower database, use this in step 3
heroku addons:create heroku-postgresql:standard-0 --follow DATABASE_URL --app your-app-name===========================================================================================================
So, this was all about how to upgrade your database plan on Heroku and Rails setup. I hope you have understood it pretty well. I have tried my best to go through Heroku’s official documentation and bring this blog to you.
If you are looking for expert Ruby on Rails developers who are well-versed with Ruby on Rails development services and following best Ruby on rails practices, then get in touch with Bacancy Technology today. A globally-renowned Ruby on Rails allows you to hire dedicated Ruby on Rails developers at your convenience and ease. Connect with us to know more about full-stack Ruby on Rails expertise on JSON Standards, MVC architecture, OOPs concepts, HTML, CSS, and JQuery.