Deployment Yout application part 2

The repository you cloned in Lesson 1 contained multiple branches that represent different stages of the application. For this step you will need to create a new local branch within your repository and set it to be track the remote “pg” branch. To do this make sure that you are in your repo and run git checkout -b pg origin/pg

We named this branch is called “pg” because the database that we have is a PostgreSQL database. There are many databases out there that you can use but Heroku requires that you use PostgreSQL when using a Ruby application. Sinatra doesn’t require any database by default but other frameworks like Rails come with SQLite as a default. SQLite will create a database file in your application directory that will change as your database updates. Because of Heroku’s ephemeral filesystem this changing database file will be eventually reset, meaning it is unsuitable for use with Heroku.

At this point your application is ready to be deployed, however unless you have PostgreSQL installed in your computer you will not be able to run this application in your localhost. If you do not have PostgreSQL installed follow these guides.

You may notice when opening this up in your editor that there are many more files present than there were in the version without a database. This is because to get a database working with this Sinatra application you have to have not only the database but also a way for your application to interact with the database, in this case ActiveRecord. ActiveRecord is an ORM or Object Relational Manager that will translate the ruby code to SQL (pronounced sequel) that the database can execute.

ActiveRecord comes in the form of a gem so you want to make sure that the application is stable locally before it can be deployed online, so run bundle install

Now that the required gems are installed on your local system, open the app as you regularly would on your local development environment. Run ruby app.rb When you try to open the application by visiting localhost:4567 you see that we have a Sinatra error:

Here we see that the table that needs to be in the database does not yet exist. You need to create it. Return to the command line, shut the server down and run rake db:migrate

Now that you have created the table required, go ahead and fire up the server again with ruby app.rb and navigate to it on your browser.

Now that the application is running without errors you are ready to deploy this new version of the application to Heroku.

To push your pg branch to the Heroku remote make sure that you are on the pg branch (you can check by running git branch) and run this command: git push heroku HEAD:master

This command will ensure that the code that is pushed to heroku master is the one that is at HEAD. In this case, this is the pg branch.

Once you do this open the application by running heroku open.

You will see that we get an internal server error. Well if you recall, when running the application locally you had to run the database migrations. To run a command on your Heroku server you have to prepend the command with heroku run. This will tell Heroku that you want to run a command on the live server, much like you would when you are running it on localhost. So instead of rake db:migrate, try heroku run rake db:migrate.

Well that’s strange, it looks different than the different error you got last time about the undefined PG table. It looks here that Heroku is not able to find a database connection.

This happened because you do not have a PostgreSQL addon included in Heroku. Heroku will only automatically include the PostgreSQL addon if your very first deploy includes the gem pg in the Gemfile. This is your second deploy to the same Heroku application so it is not included. To include this addon run heroku addons:create heroku-postgresql.

Now try running the database migrations again in the Heroku server by using heroku run heroku run rake db:migrate.

Great it looks like you’re almost there. Check if your page is up by refreshing it.

You have now successfully deployed the version of the Udacity Gallery application with a database!

Let’s recap the takeaways here:

  • Running commands on your Heroku server is different than running those same commands on your local computer.
    • Prepend your commands with heroku run.
  • Installing a database on your Heroku server is not always automatic.
    • If your first deploy contains the pg gem, then Heroku will include the heroku-postgresql addon.
    • If your application does not start with a postgres database but you want to add it you must run the heroku addons:create heroku-postgresql.

Published by

Mir Shaon

I'm very simple guy and punctual.I'm very hard worker and building any website actually this is my life.After graduation I'll started my own IT company Devshub so let's do something in this world.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s