1. Introduction to Databases
Introduction to working with data through databases
Guides, tutorials and labs to accompany CMU's Programming for Online Prototypes (49-714).
Everything you need to know about building microservices for the web with Ruby and Sinatra.
Full credit: Much of this is adapted from the Heroku guide, but set up to show you how to transition a local project into a heroku deployable project.
Heroku provides production grade PostgreSQL databases as a service.
Add-ons are third-party cloud services that provide out-of-the-box additional services for your application, from persistence through logging to monitoring and more. By default, Heroku stores 1500 lines of logs from your application. However, it makes the full log stream available as a service - and several add-on providers have written logging services that provide things such as log persistence, search, and email and SMS alerts.
In this step you will provision one of these logging add-ons, to set up a postgres database. To add one, type this in the terminal window
heroku addons:create heroku-postgresql
You’ll see a confirmation like this:
Database has been created and is available
! This database is empty. If upgrading, you can transfer
! data from another database with pg:copy
Created postgresql-convex-90661 as HEROKU_POSTGRESQL_GREEN_URL
Use heroku addons:docs heroku-postgresql to view documentation
The add-on is now deployed and configured for your application. You can list add-ons for your app like so:
heroku addons
Listing the config vars for your app will display the URL that your app is using to connect to the database, DATABASE_URL:
heroku config
and you’ll see
=== polar-inlet-4930 Config Vars
DATABASE_URL: postgres://xx:yyy@host:5432/d8slm9t7b5mjnd
HEROKU_POSTGRESQL_GREEN_URL: postgres://xx:yyy@host:5432/d8slm9t7b5mjnd
…
Heroku also provides a pg
(postgres) command that shows a lot more:
heroku pg
=== HEROKU_POSTGRESQL_GREEN_URL (DATABASE_URL)
Plan: Hobby-dev
Status: Available
Connections: 0
PG Version: 9.3.3
Created: 2014-07-07 11:30 UTC
Data Size: 6.6 MB
Tables: 2
Rows: 1/10000 (In compliance)
Fork/Follow: Unsupported
Rollback: Unsupported
This indicates I have a hobby database (free), running Postgres 9.3.3, with a single row of data.
Add the changes to the git repo and push to Heroku:
Open GitHub Desktop and commit any new changes (type a name for the commit, and press ‘Commit to Master’, then Sync
on the top right )
Once it’s synced, return to the Terminal/Command Prompt window and type the following to deploy your code:
git push heroku master
Now we’ve got our code there, we’ll need to actually set up our database
If you have migrations, you can run them like so:
heroku run rake db:migrate
If you have a seed file, you can add data to your database like so:
heroku run rake db:seed
And, you can also interact directly with the database if you have Postgres installed locally. For example, here’s how to connect to the database using psql and execute a query:
heroku pg:psql
Then you can type in SQL queries to view, insert, delete or update records:
d8slm9t7b5mjnd=> select * from widgets;
id | name | description | stock | created_at | updated_at
----+-----------+--------------+-------+----------------------------+----------------------------
1 | My Widget | It's amazing | 100 | 2014-07-08 15:05:13.330566 | 2014-07-08 15:05:13.330566
(1 row)
If you’ve followed these steps, you’ve hopefully got a working project on Heroku!
Type:
heroku open
to see the live application and interact with it.
This series guides you through working with databases, from setting up and installing your database, storing and retrieving data and deploying to heroku
Introduction to working with data through databases
Getting started - installing your database engine
Getting started - adding dependencies to your project
Getting started - adding activerecord rake commands
Migration files are small Ruby scripts that make changes to your database
Schemas define the structure of your database table and allow ActiveRecord to structure requests and changes for data
Use Models to map a database table onto native ruby objects.
Linking Models to Routes. Making a CRUD API
Linking Models to Models - Adding Associations
How to deploy your project to Heroku and add a database
How to check the information you're adding to the database
How to create initial data with a Seed File