Can someone point me to a basic Sinatra + Postgres + Heroku app

955 views
Skip to first unread message

tripdragon

unread,
Jan 13, 2012, 10:35:01 PM1/13/12
to sinatrarb
I have spent the better half of two days researching pushing a simple
todo app onto heroku using some sort of database, be it Herokus
preferred postgres or the various NoSQL tools. I keep running into
logs that give me either sql and bundler are so not getting along or
whatever.
Nor have I been lucky enough to find a simple tutorial that has the
winning combo.

I'd rather as heroku suggests use postgres in development instead of
the SQLite.

If anyone could point me to just a simple tutorial or github app that
accomplishes this basic goal, that would be spiffy ponys riding limos
awesome.

Jesús Gabriel y Galán

unread,
Jan 14, 2012, 5:14:18 AM1/14/12
to sina...@googlegroups.com

Not published anywhere, but I'm building a site to manage Magic the
Gathering decks in heroku, using PG. I used Sequel models as my ORM.
In my local machine I use SQLite. The application is just a test to
learn a bit more about Sinatra and Heroku. I'm not sure I'll even
continue developing it, although I have learnt a lot, so that's good.

Basically the setup stuff is like this (I haven't used bundler, so if
the problems are because of that this is not going to be very useful,
though):

config.ru:

require './magic_decks'
run MagicDecks

models.rb (required from the sinatra application):

require 'sequel'

# Use Heroku's environment variable or a sqlite db when locally
DB = Sequel.connect(ENV['DATABASE_URL'] || 'sqlite://decks.sqlite')
require 'logger'
DB.logger = Logger.new($stdout)

class Card < Sequel::Model(:mtg_database)
[...] # all the models stuff

I have this rakefile (can't remember where I copied it from) to run
the migrations both locally and in heroku. It uses the same trick
about heroku's env variable or the sqlite url to my local file:

namespace :db do
require 'sequel'
Sequel.extension :migration

task :migrate do
puts "task migrate"
m = Sequel::Migrator
db = Sequel.connect(ENV['DATABASE_URL'] || 'sqlite://decks.sqlite')
dir = "migrations"
target = ENV['TARGET'] ? ENV['TARGET'].to_i : nil
current = ENV['CURRENT'] ? ENV['CURRENT'].to_i : nil
puts "target: #{target}, current: #{current}"
m.run(db, dir, :target => target, :current => current)
puts "migrator finished running"
end
end

I have a migrations folder with the migrations. For example this is
the start of the first one (001_initial.rb):

Sequel.migration do
up do
create_table(:mtg_database) do
Integer :gatherer_id, :primary_key => true
[...]

The sinatra file just requires models and stuff and has usual routes and so on:

# encoding: UTF-8
require 'sinatra/base'
require 'open-uri'
require 'nokogiri'
require 'uri'
require 'cgi'
require_relative 'models'
require_relative 'gatherer_searcher'

class MagicDecks < Sinatra::Base
[...] # the rest of the class

I also have a .gems file with the gems I use:

sinatra
nokogiri
sequel
sqlite3

And I think that's all. From the sinatra application I can use the
Sequel models just fine.

Hope this helps,

Jesus.

Buck Rogers and 24th and Half Century!

unread,
Jan 14, 2012, 4:27:06 PM1/14/12
to sina...@googlegroups.com
Thank you for this. I am going see if I can reimplement this.

2012/1/14 Jesús Gabriel y Galán <jgabrie...@gmail.com>:

> --
> You received this message because you are subscribed to the Google Groups "sinatrarb" group.
> To post to this group, send email to sina...@googlegroups.com.
> To unsubscribe from this group, send email to sinatrarb+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/sinatrarb?hl=en.
>

DAZ

unread,
Jan 15, 2012, 1:24:16 PM1/15/12
to sinatrarb
I wrote this tutorial for Ruby Source a while back:
http://rubysource.com/just-do-it-learn-sinatra-i/

It is a to do list and uses sqlite locally, but shows how to set up
Bundler for heroku in part 4. It shouldn't really matter which
database you use if you use an ORM like DataMapper or Mongoid.

Hope it helps,

DAZ
Reply all
Reply to author
Forward
0 new messages