A Sinatra + Datamapper app on Heroku - ruby

I've had some problems running an app on Heroku. It uses Sinatra and Datamapper. The full project is here: https://github.com/pixelwolf/phonedb
The problem happens when I deploy the app, all goes well, until I go to the url, where I get an "Application Error" page. I have also push the SQLite3 database using heroku db:push sqlite://database.db
Here are the logs from running heroku logs: https://gist.github.com/1439777

You have to configure your database in datamapper with a line like this
DataMapper.setup(:default, ENV['DATABASE_URL'] || 'sqlite3://my.db')
That was from heroku's website. http://devcenter.heroku.com/articles/database That will configure datamapper to continue to allow you to use sqlite3 on your local machine but switches to Heroku's postgres DB when the application is used there.
Also, Tom Anderson is correct, you need to add the dm-postgres-adapter gem to your Gemfile. I also added the pg gem as well, I'm not sure if that was necessary though.

The error page has the line:
`require': no such file to load -- dm-postgres-adapter
You can only use postgres on heroku. So likely you are not accounting for that in some way. You can develop with sqlite on your machine, but you then need to arrange a few things to make it all work, plus you need to not use any sqlite or postgres only features.
Adapters not working with datamapper
Heroku and Datamapper problems
http://www.ruby-forum.com/topic/1770484

Related

Why is the rails_12factor gem necessary on Heroku?

I noticed that the new version of Michael Hartl's Ruby on Rails Tutorial says to include the following to serving static assets on Heroku
gem 'rails_12factor', group: :production
The previous version of his book did not have that. Can someone explain the details of what happened with Heroku that requires this gem?
The best answer I could find is by looking at the rails_12factor README.
There's also a discussion on why Heroku decided to include the gem: Why does this gem exist?
Especially read the answer from schneems about halfway down the page.
This gem enables serving assets in production and setting your logger to standard out, both of which are required to run a Rails 4 application on a twelve-factor provider. The gem also makes the appropriate changes for Rails 3 apps.
In the Getting Started with Rails 4.x on Heroku, we can read this:
Heroku integration has previously relied on using the Rails plugin system, which has been removed from Rails 4. To enable features such as static asset serving and logging on Heroku please add rails_12factor gem to your Gemfile.
Older question, but FWIW Rails 5 Apps do not require the rails_12factor gem. Per the README:
We worked with the Rails core team to make Rails 5 work on twelve-factor platforms out of the box.
There is also a snippet for migrating to Rails 5.
Just ran into a problem with a Rails 4 application on Heroku and couldn't debug it with heroku run logs. Without the rails12factor gem, you can't see the full logs that you normally see on your console in development. I was getting a 500 error and there wasn't any available information.
It actually didn't tell me that migrations were pending without rails12factor
As of Rails 5, you no longer need to include this gem in your Gemfile. They've removed it from their documentation also: https://github.com/railsbridge/docs/issues/569

Heroku. db:push when I app contains two databases

I have Rails 3.2 application hosted on Heroku. My application contains two databases (one for my model, the second is a kind of a dictionary with static data).
I need to push the second database (dictionary) to Heroku, but when I try db:push Heroku thinks that I'm going to push the first database (with Rails model).
The question is - how could I specify that I want to push my local database dictionary.sqlite to heroku dictionary.pg?
You could use the Heroku pg:transfer plugin which will let you set the target destination by it's URL.
https://github.com/ddollar/heroku-pg-transfer
Alternatively, use psql client locally but restore to the heroku pg isntance.
Don't use db:push/pull; those methods are deprecated. Use pgbackups:capture/restore for things like this. It accepts the HEROKU_POSTGRESQL_COLOR as part of the command:
$ heroku pgbackups:restore HEROKU_POSTGRESQL_COLOR 'https://example.com/data.dump' --app app-name
See Importing and Exporting Heroku Postgres Databases with PG Backups for more detailed explanation.
Also, heroku-pg-transfer has been integrated into pg-extras, check that out here: https://github.com/heroku/heroku-pg-extras

Installing MaxMind GeoIP ruby library on heroku

I have a Rails app running on heroku. Now I want to run a rake task (which uses a model of my rails app) and geo-tag each record using the MaxMind GeoIP database (http://www.maxmind.com/app/geolite).
Using this database in a Ruby application involves :
Building it's C-API (http://www.maxmind.com/app/c) and then
Build the Ruby bindings for that (http://www.maxmind.com/app/ruby).
I could do this on my local machine and successfully get the country codes from IP addresses. However I do not know how to install these libraries on Heroku. From what I understand the ruby bindings are not available as a gem with native extensions (which heroku would have handled just fine). Also their C-API seems to have a few other dependencies which makes me wonder whether such a thing is possible at all.
So has anyone installed the MaxMind GeoIP (a.k.a Net::GeoIP) on Heroku? If yes how?
I could run the rake task from my laptop by pointing my local setup to the production DB. Before that I would like to know if I can run it from my Heroku setup itself to avoid latency or connection breakdown etc.
I found a gem which bundles the MaxMind C-API and provides it's own Ruby API. It works just fine - http://rubygems.org/gems/geoip
I ll tentatively accept this as the answer. However it would be awesome if someone can tell me how to install binary extensions to Ruby on Heroku.

Heroku app with Xeround DB, how to configure?

I’m trying to setup a Heroku app but using a Xeround DB. The instructions I have found are a bit confusing:
http://xeround.com/developers/heroku-cloud-database-mysql/
I'm trying to follow the second way, creating the db directly on Xeround.
My doubts are:
Is the Xeround addon required for this? At least heroku addons:add xeround –app xxxx says: That add-on is only available to selected users
The Heroku DATABASE_URL needs to be set to: mysql://username:password#host:port/database
I've seen some posts with mysql2 and mysql (none has worked for me anyway). My gem file has mysql2, 0.2.7 (for Rails 3.0.x)
I can see the config var ok, but Heroku config --app xxx still shows SHARED_DATABASE_URL => postgres://pjyqfgjcbn:.... is that ok?
Then from the instructions the step #5 is really confusing, it is not clear if that part is optional or not (I expect it is). And if it was mandatory it doesn't tell to which file it has to be added ...
My objective is to create a fresh Heroku app, push an existing app I have, set it up to use the Xeround DB and then run Heroku rake db:schema:load but no matter what I try I keep getting:
rake aborted!
database configuration does not specify adapter
Any ideas how to set it up?
do the same with Rails 3.0.5 but having some problems and comments.
Is the adapter mysql or mysql2? The example on Xeround about the setup site is mysql://username:password#host:port/database
mysql2 is correct gem to use with Rails 3 (mysql 0.2.7 for 3.0.x and latest for 3.1) but the name in the url might be just a name they picked. I guess yours worked but still…
I imagine the Xeround addon of Heroku is not needed right?
heroku addons:add xeround –app xxxx says That add-on is only available to selected users
And finally with mysql or mysql2 in the name of the db url when I try to migrate I just get:
rake aborted!
database configuration does not specify adapter
Did ask Heroku but no answer yet…
I've been using Xeround for several of my apps hosted on Heroku.
I don't have specific experience with Rails. Sinatra only... But hopefully this helps you out.
I do not use the add-on. It's less expensive to setup an account through Xeround on your own.
Connect to it just like you would any MySQL Database....
Connection String will look just like you stated:
mysql://username:password#host:port/database
Host/Port are provided by Xeround in the admin database. Database name would be whatever you setup in phpMyAdmin. User/pass are NOT your Xeround account credentials. They are the credentials you setup for that database instance.
For example... if your ORM is Datamapper... do something like this:
DataMapper.setup(:default, ENV['XEROUND_CONN'])
Or if you're using Sequel:
DB = Sequel.connect(ENV['XEROUND_CONN'])
Then you need to set your XEROUND_CONN (or whatever you wish to call it) ENV variable.
Do this by using the Heroku command
heroku config:add XEROUND_CONN='mysql://username:password#host:port/database'
Warning: Datamapper has an issue with Xeround. Datamapper does not have support for setting your storage engine (myisam..etc..). Xeround uses their own custom Xeround storage engine. So... I've had trouble running auto_migrate due to differences in storage engine.

Heroku and Datamapper problems

I can launch a basic app on Heroku, displaying a message with get '/'... works just fine. However, whenever I try to add sqlite with datamapper, the thing breaks down.
In order to see my app structure, check out the project on github. I've kept the code pretty bare-bones.
In the log from heroku I'm getting:
2011-06-26T21:28:36+00:00 app[web.1]: /app/.bundle/gems/ruby/1.9.1/gems/dm-core-1.1.0/lib/dm-core/adapters.rb:163:in `require': no such file to load -- dm-postgres-adapter (LoadError)
The thing about this is that I'm not using postgres, so I'm confused why it is saying this.
"The thing about this is that I'm not using postgres, so I'm confused why it is saying this."
You're using Heroku, so you are using Postgresql.
In your app.rb you have this line:
DataMapper::setup(:default, ENV['DATABASE_URL'] || "sqlite3://#{Dir.pwd}/notes.db")
which I'm guessing you got from the Heroku database docs. This line basically says "check for the environment variable 'DATABASE_URL', and if it's set use it as the database url, otherwise use the Sqlite url". Running locally this environment variable won't be set, so you'll use the Sqlite url, but on Heroku this will be set to something like (see the page linked above):
postgres://username:password#hostname/database
Datamapper will see that this is a Postgresql url, and try to require the postgres adapter, which isn't installed so will result in the error that you see.
The best solution would be to install Postgresql locally, so that your development and production environments are as similar as possible. If you can't do this, or don't want to, you can specify the Sqlite adapter locally, and the Postgres adapter in production. It'll look something like this in your Gemfile:
group :development do
gem 'dm-sqlite-adapter'
end
group :production do
gem 'dm-postgres-adapter'
end
If you do this you'll need to tell Heroku to which groups to leave out when installing gems, see the Heroku Gem Bunder docs.
You are moving from sqlite to postgres-sql and thus you should include dm-postgres-adapter in your Gemfile (and install it ofcourse).
Heroku only supports a read-only file system (or at least only supports read-only operations for any files that you want to be persistent). Hence, you can't use SQLite on Heroku. Heroku only supports PostgreSQL as a database so you need to configure DataMapper to use PostgreSQL (see Yet Another Geek's answer).
I'd recommend installing PostgreSQL in your development environment if you're planning to deploy to Heroku. Every database is a little different and no ORM will protect you from the differences. A bit of searching for questions tagged heroku and postgresql should show some of the problems you'll have if you develop on top of one database but deploy on another.

Resources