Setting Up - olendorf/jsonb_elasticsearch_demo GitHub Wiki

This tutorial covers how I set up a Rails site on Cloud 9 with Elasticsearch, PostgreSQL with HSTORE, Rspec, Travis CI and Coveralls. It took a few days of trial and error and digging around the web to put it all together. It may seem overly detailed, but some of the answers I found skipped steps which seemed obvious to the author, but sure weren't to me.

Assumptions.

  1. You have, or are able to set up a Cloud 9 account.
  2. You have a working knowledge of Git and Rails
  3. You can navigate a web page

Step 1 - Create a Cloud 9 project

This is easy enough. Just make sure you choose Rails project. You could start with a blank project, but I think thats just making it harder on yourself.

Step 2 - Set up versioning

Goto Github and create a new repo. I prefer to start with their .gitignore for Rails, a README and a License. This will cause a slight merge conflict later. You can try to avoid this by deleting the README and .gitignore in Cloud 9. Or fix it when it happens.

In your Cloud 9 terminal do the following.

Initialize your repository

    $ git init                                 # intialize the local repository
    $ git add -A                               # stage the initial content
    $ git commit -m 'initial commit'           # commit the initial content.

Set up the remote and sync the two repos

    # Get the URI for your repository by clicking the Clone or Download button and 
    # copying the URI to your clipboard. Owner name and project name will vary.
    $ git remote add origin [email protected]:owner_name/project_name.git

    # Gets the .gitignore, license and README from Github
    $ git pull origin master                   

    # Fix any merge conflicts, and commit.

    # Sends all the application files generated by Cloud 9 to remote
    $ git push origin master                                

Set up your branches and prepare for a good workflow. I also use waffle.io and use their recommended workflow. Going forward, many branch names will reflect waffle.io's automated tracking. Yep, I actually create a series of issues to do this.

    $ git checkout -b develop
    $ git push origin develop

Go to Github, click the branch button to get to the branch management page. Change your default branch to develop. If you are working on a team, I also recommend protecting your master and develop branch.

Step 3 - Set up Postgres with HSTORE

Cloud 9 sets you up with Sqlite by default and it takes a little work to get Postgresql up and running there. I found this tutorial and modify it only slightly here. Pretty sure this could and should be scripted?

create a new branch (the issue reference is optional)

    $ git checkout -b postgresql-#1

edit your Gemfile, removing sqlite3 and adding pg.

    source 'https://rubygems.org'

    # Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
    gem 'rails', '4.2.5'
    # Use postgresql as the database for Active Record
    gem 'pg'
    # Use SCSS for stylesheets
    gem 'sass-rails', '~> 5.0'
    ...
    ...

In your terminal $ bundle install

Edit your database.yml. This assumes you will be using environmental variables for your credentials, which is more secure.

    ##
    # Allow more secure way of handling credentials as environmental variables
    #
    # $ echo "export USERNAME=username" >> ~/.profile
    #
    # $ echo "export PASSWORD=password" >> ~/.profile
    default: &default
          adapter: postgresql
          encoding: unicode
          pool: 5
          username: <%= ENV['USERNAME'] %>
          password: <%= ENV['PASSWORD'] %>
          host:     <%= ENV['IP'] %>

    development:
      <<: *default
      database: app_development

    test:
      <<: *default
      database: app_test

    production:
      <<: *default
      database: app_production

Edit your ~/.profile by typing this in your terminal. Also add aliases for stopping and starting the database.

   # User appropriate username and password.
   $ echo "export USERNAME=username" >> ~/.profile
   $ echo "export USERNAME=password" >> ~/.profile
   $ source ~/.profile

   # Cloud 9 free version stops your db after you are off for a bit. This is very handy.
   $ echo 'alias pgstart="sudo service postgresql start"' >> ~/.bashrc
   $ echo 'alias pgstop="sudo service postgresql stop"' >> ~/.bashrc
   $ source ~/.bashrc

In your terminal type $ pgstart, you should see something like

    * Starting PostgreSQL 9.3 database server
   ...done.

No we need to create a database user with these credentials.

    $ sudo sudo -u postgres psql      # You need the two sudos

In Postgresql...

    postgres=# CREATE USER username SUPERUSER PASSWORD 'password';
    # -> CREATE ROLE
    postgres=# \q

The default encoding is not UNICODE, so change the template to add that.

    postgres=# UPDATE pg_database SET datistemplate = FALSE WHERE datname = 'template1';
    # -> UPDATE 1

    postgres=# DROP DATABASE template1;
    # -> DROP DATABASE

    postgres=# CREATE DATABASE template1 WITH TEMPLATE = template0 ENCODING = 'UNICODE';
    # -> CREATE DATABASE

    postgres=# UPDATE pg_database SET datistemplate = TRUE WHERE datname = 'template1';
    # -> UPDATE 1

    postgres=# \c template1
    # -> You are now connected to database "template1" as user "postgres".

    template1=#  VACUUM FREEZE;               # NOTE THE PROMPT CHANGE
    # -> VACUUM

    template1=# \q

Now we finally get to HSTORE. If you don't know what that is, it allows you to use Postgresql like a documents store such as MongoDB. If you are already using Postgresql, it means one less piece of technology to manage, and according to this review, out performs MondoDB.

HSTORE requires adding an extension. I had to first update my apt-get repositories, then install the extension. Finally I added HSTORE to my template, so that all databases get the extension. Thanks to these two sites for helping.

In your terminal (no need to connect to Postgresql)

    $ sudo apt-get update                               # may not be necessary, but can't hurt
    # -> lots of stuff

    $ sudo apt-get install postgresql-contrib           # Has HSTORE extension
    $ -> lots of stuff. Say yes when it asks to continue.

    $ psql template1 -c 'create extension hstore;'
    # -> CREATE EXTENSION

Step 4 - Sanity Test

We did a lot, so lets create a quick model to make sure its all working. In your terminal

    $ rails g model ChuckNorris fact:text knockouts:integer