Friday, September 30, 2011

Upgrading to Rails 3.1 from Rails 3.0.4 with MySQL on Heroku

I ran into a few issues when trying to update my Rails 3.0.4 app to Rails 3.1.0.  I was using a MySQL database (all vanilla stuff) and heroku.  This all worked fine.  I wanted to try SCSS and found that Rails 3.1 has built in support for it; so, I decided to upgrade.  Here's what I did:


Watched the railscast for upgrading: http://railscasts.com/episodes/282-upgrading-to-rails-3-1  Which instructed me to follow these steps:

  1. Change Gemfile rails version to 3.0.10
  2. bundle update
  3. make sure it still works
  4. Change Gemfile rails version to 3.1.0
  5. Comment out line in development.rb: config.action_view.debug_rjs
  6. bundle update
  7. make sure it still works
This is where I got my first issue.  It said the mysql2 gem was too old (mine was < 0.3).  I change the Gemfile to use the newest version and ran 'bundle update' again.  

Then there was another issue related to the mysql2 bundle pointing to the correct library.  This is a common issue running on OS X I guess.  Here's the fix and the link where I got it: http://freddyandersen.wordpress.com/2010/10/03/mysql-5-5-snow-leopard-and-rails/

sudo install_name_tool -change libmysqlclient.18.dylib /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/local/lib/ruby/gems/1.9.1/gems/mysql2-0.3.7/lib/mysql2/mysql2.bundle
At this point I can run the app as normal locally.  The next step was to push to heroku.  I created a separate heroku location in case I ran into issues or if they used a different stack for 3.1 vs 3.0.x.  I'll skip those steps.  The error I got when I pushed was: `rescue in establish_connection': Please install the postgresql adapter: `gem install activerecord-postgresql-adapter` (pg is not part of the bundle. Add it to Gemfile.) (RuntimeError)
Unbeknownst to me, heroku actually uses postgress and not mysql.  I guess it just worked before.  I didn't really want to change my local stuff if I didn't need to so I found a solution here: http://stackoverflow.com/questions/6410623/heroku-error-when-launch-rails3-1-app-missing-postgres-gem
The actual fix was to just add the below to the Gemfile and run bundle with the '--without production' flag:
group :production do
  gem 'pg'
end
After that pushing to heroku worked fine.
P.S. I must have run heroku db:migrate at some point, but I can't remember when.  I think it was before I got the postgress error.

Pretty Print XML in Java

I got my solution from http://stackoverflow.com/questions/1264849/pretty-printing-output-from-javax-xml-transform-transformer-with-only-standard-ja

import javax.xml.transform.OutputKeys;

import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

Transformer transformer = TransformerFactory.newInstance().newTransformer(); 
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");

Source xmlInput = new StreamSource(new StringReader(xml));


StreamResult xmlOutput = new StreamResult(new StringWriter());
transformer.transform(xmlInput, xmlOutput);
System.out.println(xmlOutput.getWriter().toString());