Creating an App in heroku

I had recently created a heroku app. The syntax for creating a heroku app is

heroku create [app-name]

If you don’t provide an app name, heroku will generate one for you. It will be something like ‘stormy-hamlet-9918’. I’m neither a git or heroku expert. But, I think what happens when you issue this command is that it creates a remote with the name (taking from the example) stormy-hamlet-9918.git. The url to your app will be stormy-hamlet-9918.herokuapp.com

Now you can push to heroku using command

git push heroku master

Renaming the App.

Heroku gives provision to rename the app to something that you understand. Log into your heroku.com account. Change the name of your app from Settings tab. Say you changed the name to ‘MyNewApp’. The url to your app will also be updated. It will be MyNewApp.herokuapp.com. stormy-hamlet-9918.herokuapp.com will no longer work.

Pushing new changes to Heroku.
Now that we have changed the name of the app, pushing new changes will require a little clean up work.

git push heroku master

will give you error like this.

 !  No such app as stormy-hamlet-9918.
fatal: The remote end hung up unexpectedly

This is because when we renamed the app to MyNewApp the remote repository name stormy-hamlet-9918.git no longer exists. Its MyNewApp.git now. Here are the steps to fix the issue.

git remote rm heroku
git remote add heroku git@heroku.com:MyNewApp.git

Once this step is done, git push heroku master will work.

DISCLAIMER: I’m new to heroku, git and all. So, if there’s anything wrong that I have mentioned here, please be kind enough to correct me. Thanks.

I have been fascinated by the Sudoku puzzle. It has been my dream to build the sudoku puzzle all by myself. Learning Scala has helped me fulfill that easier.

I have used AngularJS to make it a single page application. Using Bootstrap from Twitter helped me make the page look prettier.

Then I used Heroku to deploy it in cloud. Here’s the link. Sudoku in Scala It may not be perfect, but I loved working on it. Learned a lot in the way.

I want to write how I developed this one day.

I was playing with Play and Scala. I decided to use Squeryl for Data access.

Here’s my model class models/Product.scala

package models

import org.squeryl.KeyedEntity
import org.squeryl.PrimitiveTypeMode.inTransaction
import org.squeryl.PrimitiveTypeMode._
import org.squeryl.Schema

object AppDB extends Schema {
  val productsTable = table[Product]("products")
}

case class Product (name:String,price:Long) extends KeyedEntity[Long] {
	val id:Long = 0;
}

object Product {
  def create (product:Product)= transaction {
    AppDB.productsTable insert(product)
  }

  def all:Iterable[Product] = inTransaction {
    val products = from (AppDB.productsTable)(productsTable => 
    		select(productsTable))
    products.toIterable
  }
}

Here’s my controller controllers/Products.scala

package controllers

import play.mvc.Controller
import play.api.mvc._
import play.api.mvc.Results._
object Products extends Controller {

	def index = Action {
		  val products = models.Product.all
		  Ok(views.html.products.index(products))

	}

}

Now, when I run my app localhost:8080/products I get the following error.
[RuntimeException: no session is bound to current thread, a session must be created via Session.create and bound to the thread via ‘work’ or ‘bindToCurrentThread’]

It appears that I should have wrapped the index action in Products controller around a transaction block. Here’s the updated code.

package controllers

import play.mvc.Controller
import play.api.mvc._
import play.api.mvc.Results._
object Products extends Controller {

	def index = Action { 
                  transaction {
		  val products = models.Product.all
		  Ok(views.html.products.index(products))
                  }

	}

}

After this I was able to load products pages. I have to dig deeper to get the details. Will update when I find more information.

I am diving into Play! framework for Scala. While I’ve been working on MySQL all this time, I thought I’ll use a new database to learn Play! for Scala. Thats how I decided to use PostgreSQL. So, here’s how I got it setup.

1. Add Dependency project/Build.scala

val appDependencies = Seq(
      // Add your project dependencies here,
        "postgresql" % "postgresql" % "9.1-901-1.jdbc4"
    )

 2.  Configure database access in conf/application.conf

db.default.driver=org.postgresql.Driver
db.default.url="jdbc:postgresql://localhost:5432/YOUR_POST_GRE_SQL_DATABASE"
db.default.user=USERNAME
db.default.password=PASSWORD

3. Using evolutions

Evolutions help you propagate your schema changes easily to your database.  Its similar to migrations in Ruby on Rails. Each evolution script is a plain sql script. The file name goes as 1.sql, 2.sql and so on. Evolutions are placed under the  directoryconf/evolutions/<DATABASE_NAME>. Here is an example.

conf/evolutions/warehouse/1.sql

# --- !Ups

CREATE TABLE products (
	id serial primary key,
	name text,
	price float
);

# -- !Downs

DROP TABLE products;