Skip to content

Instantly share code, notes, and snippets.

@DamonLC21
Last active March 5, 2021 17:09
Show Gist options
  • Save DamonLC21/49682e2753f26138482f3d853a011047 to your computer and use it in GitHub Desktop.
Save DamonLC21/49682e2753f26138482f3d853a011047 to your computer and use it in GitHub Desktop.
Pushing a Rails/sqlite3 API to Heroku

Deploying Rails/SQLite3 app to Heroku

Before we begin install postgresql onto your device:

$ brew install postgresql

Check if the postgresql service is running via brew :

$ brew services

If not run the rollowing command :

$ brew services start postgresql

Step 1

When Deploying an existing application to Heroku first

You must change your Gemfile:

gem 'sqlite3' 

to:

gem 'pg' 

SQLite3 is not supported by Heroku and therefore we must use Postgres to deploy our database.

Step 2

Next you will need to convert your config/database.yml.

We will be changing the adapter to postgresql.

adapter: sqlite3

To:

adapter: postgresql

We will also be naming our database for production,test,and development for postgres to use as well. That change will look something similar to...

From this:

default: &default
  adapter: sqlite3
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  timeout: 5000

development:
  <<: *default
  database: db/development.sqlite3

test:
  <<: *default
  database: db/test.sqlite3

production:
  <<: *default
  database: db/production.sqlite3

To this:

default: &default
  adapter: postgresql
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  timeout: 5000

development:
  <<: *default
  database: my_database_development
  
test:
  <<: *default
  database: my_database_test

production:
  <<: *default
  database: my_database_production

Once all that is changed go ahead and save your changes.

Step 3

If you haven't already run bundle install to update the Gemfile to use gem 'pg' . Now we will create our postgres database and run our migrations.

rails db:create
rails db:migrate

At this point you are ready to deploy to heroku.
Note that your rails api will not have a home page so you will have to add a controller for your root page and add

root  'controller_name#index' 

in your config/routes.rb to have home page.

Deploying to Heroku

Before we begin make sure to save all changes and push to the github repo you will be saving your Rails app to.

Also if you don't have Heroku cli installed do so now with the code below

w/ Homebrew

$ brew tap heroku/brew && brew install heroku

##Step 1

Now that we have the Heroku cli installed... First we will run heroku create to create our heroku app. Make sure you are in the directory that contains your Rails app. You may be prompted to login to heroku at this point.

$ heroku create

Step 2

Once our heroku app is created we can deploy our code to heroku with the following command:

$ git push heroku master

Step 3

Now we run our migrations but this time via heroku:

$ heroku run rake db:migrate

And if you have seeds to add :

$ heroku run rake db:seed

You have successfully deployed your app to Heroku visit your given link to see your Rails API live !!!

@brookton
Copy link

brookton commented May 24, 2019

A few additional random notes I found incase you run into these errors:

Be sure to add the <%= %> erb tags to this line, as we found today:

pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

if your database isn't seeding or migrating try running

heroku run:detached rake db:migrate

and if you're starting a new project you can also use

rails new myapp --database=postgresql

and also if you're getting Cannot run more than 1 free size dynos
run

heroku ps

which lists out any open session.
The following is an example of what could be listed after running “heroku ps”
run.5656 (Free): up 2016/01/12 21:28:41 (~ 7m ago): rails c
then you would run

heroku ps:stop run.5656

random references

  1. https://devcenter.heroku.com/articles/getting-started-with-rails5
  2. https://stackoverflow.com/questions/20926403/heroku-rake-dbmigrate-results-in-error-r13-attach-error-failed-to-attach-t
  3. https://medium.com/@g_eduardo90/heroku-cannot-run-more-than-1-free-size-dynos-b1c3fdb5eccb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment