Posted by Mike Blake
Thu, 26 Apr 2007 11:30:00 GMT
Update 4/30
I finally did find the real Hibernate 3 Migration Guide. My fun with Hibernate is over though, I've found an easier persistence solution with the clients homegrown method. I believe every single java project I've worked on in the past 10 years has used a different and unique Persistence mechanism. My favorite was Francois' PersistentEntity at ViaFone.
I’ve been strugling for the last day trying to get a Java application working, mainly, trying to get Hibernate to see a DataSource. Now I’m at a point where I get this error message:
[junit] (cfg.Configuration 1312) configuring from resource: /hibernate.cfg.xml
[junit] (cfg.Configuration 1289) Configuration resource: /hibernate.cfg.xml
[junit] (util.DTDEntityResolver 30 ) Don’t use old DTDs, read the Hibernate 3.x Migration Guide!
A quick search does not turn up a Hibernate Migration Guide, so I decided to create this one.
The Hibernate 3.x Migration Guide
Get Ruby
Install Rails
Rebuild your application.
Have fun again!
Posted in Create | Tags hibernate, migration, rails, ruby | no comments | no trackbacks
Posted by Mike Blake
Sat, 31 Mar 2007 13:11:00 GMT
Rails developers working on enterprise software projects are suprised to discover the lack of automated tests in many mature web applications. As frustrating as this can be, A lack of automated tests is also a tremendous opportunity to
- Learn your non-rails applications underlying database structure.
- Demonstrate to a devlopment team the power of The Rails Framework.
- Encourage automated testing.
Rails may be the quickest path to automate some basic test of your non-rails applications data model. These steps will get you all set up to write your automated tests in Ruby.
- Connect to Your Enterprise Database
- Set DB Conventions
- Safety Net
- Duplicate the Schema
- Extract Development Data
I. Connect to Your Enterprise Database from Rails.
Download and install any os driver, ruby gems, and rails adapters needed to connect to your database:
Supported Rails Databases:
MySQL
DB2
Oracle
Sybase
SQLServer
PostgreSQL
Firebird
II. Set Your Existing Database Conventions
Rails Recipes
, Recipie #16 can walk you through this very quickly.
Identify any conventions used by your legacy database. Application wide conventions can be set in the config/environment.rb file.
They are specified by calling the appropriate class methods available on ActiveRecord::Base. The methods you need to call depend on how your database is configured:
ActiveRecord::Base.table_name_prefix 'myapp_'
ActiveRecord::Base.table_name_suffix = '_def'
ActiveRecord::Base.sequence_name = 'dev_company'
ActiveRecord::Base.pluralize_table_names = false
ActiveRecord::Base.primary_key_prefix_type = :table_name_with_underscore # or :table_name
III. Safety Net
Load the SafetyNet Plugin into your rails app to prevent from destroying your development database.
IV. Duplicate Your Database Schema
OK , with your dev and test development databases set correctly, let’s try duplicting the schema.
rake db:test:clone
If this works right off the bat, you’re one of the lucky ones. Skip to Extracting Development Data
If you recieved errors from the clone command, do steps A through C .
A. You can now correct any errors you may have had by manually modifying schema.rb . See Oracle Errors for some problems I had with the Oracle Database. Since schema.rb is generated, it’s a good idea to rename it when you modify it manually.
B. The clone command above may have started loading data; you need may to purge the test db.
rake db:test:purge
C. Tell rake to load your new schema file by passing the new name, relative to RAILS_ROOT in the SCHEMA environment variable:
rake db:schema:load RAILS_ENV=test SCHEMA=db/oracle_schema.rb
Copy the code from
Rails Recipe #42: extract_fixtures.rake to your RAILS_ROOT/lib/tasks directory.
TIP: If your develoment database has lots of data, Modify the SQL Select in this code to limit the amount of data you copy using the SQL limit statement:
sql = "SELECT * FROM %s limit = 100"
Or for a proprietary database, the equivalent command:
sql = "SELECT * FROM %s WHERE ROWNUM<=100"
Then run
rake extract_fixtures
You’ve made your testbed so you can lie in it.
You now have and exact copy of your development database, and a collection of sample data in Yaml format, and are ready to begin writing some Unit Tests. Stay tuned for some examples. Rail on!
Posted in Create | Tags database, rails, rake, ruby, unit tests, yml | no comments
Posted by Mike Blake
Thu, 29 Mar 2007 21:15:00 GMT
When rails on jruby and connecting to Oracle via JDBC ,the following error eventually appears:
Update:
java.sql.SQLException: Io exception: Broken pipe
Fix:
You need a dedicated connection from Oracle. Change the url: line in database.yml from
url: jdbc:oracle:thin:@localhost:1521:XE
to
url: jdbc:oracle:thin:@(DESCRIPTION = (ADDRESSLIST = (ADDRESS =(PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))) (CONNECTDATA =(SERVER = DEDICATED) (SID = XE)))
Workarounds
Duplicating an existing Oracle Database using the build in task rake db:test:clone presented a few problems. Here’s what I did to work around them.
ORA-01727: numeric precision specifier is out of range
OCIError: ORA-01727: numeric precision specifier is out of range (1 to 38): CREATE TABLE employee (id NUMBER(38) NOT NULL PRIMARY KEY, createddate DATE DEFAULT NULL, startdate DATE DEFAULT NULL, jobid DECIMAL DEFAULT NULL, totalhours NUMBER(126) DEFAULT NULL)
Fix:
For some reason whne rails dumps the schema, it reports Oracle type FLOAT as NUMBER(126), so you just need to changed that back to FLOAT if you want to import that schema.
OCIError: ORA-00972 identifier is too long:
OCIError: ORA-00972: identifier is too long: CREATE SEQUENCE gametime_responsibility_ref_seq START WITH 10000
Fix:
Rails tries to create sequences in Oracle to handle AUTOINCREMENT id fields. It uses TABLENAME = ’SEQ’ for the sequence name. If a sqequence name is too long then you have to shorten it in schema.rb .
OCIError: ORA-00907: missing right parenthesis
OCIError: ORA-00907: missing right parenthesis: CREATE TABLE board (id NUMBER(38) NOT NULL PRIMARY KEY, name VARCHAR2(150) NOT NULL, name VARCHAR2(150) NOT NULL, parent VARCHAR2(150) DEFAULT NULL, loglevel DECIMAL DEFAULT NULL, modifieddate DATE(6) DEFAULT NULL, token DECIMAL DEFAULT NULL)
Fix:
For some reason Rails assigns Date fields a size in schema.rb . You’ll need to change all occurances of DATE(6) to DATE .
Posted in Create | Tags oracle, rails, rb, schema | 1 comment
Posted by Mike Blake
Mon, 05 Mar 2007 17:35:00 GMT
Ideas from the Margins
My brothers alternative news weekly in Colombia, SC focuses on stories no one else would write about including a regular feature called Bum of the Week, or and occasional interview with a Nazi.

Content Management
He asked me to help him find an open source Content Management System, and after exploring a few, we choose Mephisto. I was impressed with Mephistos usability and online documentation. I also noticed that there we’re several integration tests written for it’s caching system, and used them as an example in a recent presentation. It has slickly designed admin interface, and the writers at City Paper love it. My brothers not afraid to edit some HTML or CSS, so he quickly got comfortable with the design templates.
Surprise
But the best surprises came after we went live with the site last week. First the visitors to the site began commenting on stories like never before. For some reason the Ocadia template we chose with it’s subtle use of color was inviting.

Ironically one commenter who said
I’m not going to make this a forum for debate. Those who want to debate me can come onto a regular white website …
went on and on after that to post the largest comment posted by anyone, and made that article a forum for debate. It turns out that integration of color is much more appealing to him than he ever knew.
College Football Frenzy
The second surprise came just 2 days later. Editor Todd Morehead published a story early Friday about USC’s quarterback keying a car in the school library parking lot. The story was index by Google’s news bot, and went on to get picked up by the the local daily paper that night, hit the Associated Press wire, and was then published on Sport’s Illustrated’s site along with dozens of sites nationwide.
City Paper received a record number of unique visits over the weekend. Mephisto’s caching performed beautifully. It was a great weekend here in the margins. Congratulations to City Paper for breaking a national new story, and to Mephisto for not breaking.
Posted in Write, Create | Tags cms, Columbia City Paper, content management system, marginal, Mephisto, usc | 2 comments
Posted by Mike Blake
Thu, 22 Feb 2007 14:50:00 GMT
Practice.
Music
When Tony Macaluso invites me to play bass at a gig, I need to prepare. Before I even start rehearsing songs, I break out my Rickenbacker and warm up with some scales.

Programming
When I’m invited to help out with a software project I’m expected to hit the ground running. Each project might require a different area of expertise. How do I warm up? I usually have reserched the problem domain beging even before interviewing for the gig. But now there’s a more precise method analogous to playing scales.
Pragmatic Programmer Dave Thomas has begun a blog called CodeKata . He already has about 15 different exercises created to help us programmers practice our craft.
The Kata touch on a variety of common problem domains in software development. There are the standard programming 101 issues including Sorting it Out and Karate Chop. And there are several design exercises like Hashes vs. Classes and Supermarket Pricing.
Posted in Create | Tags CodeKata, Dave Thomas, Programming Musician, Tony Macaluso | no comments
Posted by Mike Blake
Mon, 19 Feb 2007 18:23:00 GMT
The assert_select method for testing views was added in Rails 1.2, so the first thing to check if you’re getting the error below is that you’ve upgraded to 1.2
> 2) Error:
>test_expire_old_ads(AdExpirationTest):
>NoMethodError: undefined method `assert_select' for #
> c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.5/lib/action_controller/test_process.rb:432:in `method_missing'
Now I upgraded to 1.2 and still kept getting the above error. The problem was that my application was set to use Rails 1.1.6 in the config/environment.rb file:
RAILS_GEM_VERSION = '1.1.6'
So the solution is to comment out that line, or alternatively ,set it to the latest version. Also make sure you’re not frozen to a specific Rails version in the vendor directory.
Posted in Create | Tags assert_select, NoMethodError, rails, testing | no comments
Posted by Mike Blake
Wed, 14 Feb 2007 21:07:00 GMT
ORUG
At this months Orlando Ruby User’s Group meeting, I did my Testing on Rails presentation. In it I touched on just some of the benefits of having an automated suite of Unit, Functional, and Integration tests.

Later I thought of another major benefit. Tests help other people on the project, in addition to the author of the test.
Good Habits are Contagious
Recently I had the opportunity to work on a project with Harris Reynolds. Harris did something very important that sadly, you don’t see on every Rails Project. He wrote a unit test. Lot’s of us wrote tests , but this particular test stood out for two reasons. The first was when it was written.
Writing Tests First
Harris had found a bug in a model object that I had created. To notify me, he checked in an assertion that failed, then emailed me. This saved me time searching thought and remembering code I had written. I simply typed rake test:recent which runs all recently changed tests in a Rails application . The error took me right to the problem, and I immediately fixed the bug.
Testing a Deployment
The second reason I remember this test happened at least a month later. The application was being deployed to a new machine, and the test failed. The particular model object being tested pointed to a different database that the rest of the application, and it turned out that that database hadn’t been installed correctly on the new machine. Harris’s test told the problem straight away, and saved time once again.
Unit Testing: the gift that keeps on giving.
Thanks Harris!
Posted in Create | Tags ORUG, rails, ruby, unit tests | no comments
Posted by Mike Blake
Wed, 07 Feb 2007 21:14:00 GMT
Curent Mood: Bored with Deleting the Production Database
I thought I was the only clown working on Rails projects and ignoring the warning in config/database.yml and using rake to wipe out perfectly good databases. But I’ve seen it happen to others now, and enough’s enough. So I created the
SafetyNet Plugin
.
You can install Safety Net in your app, by running
ruby script/plugin install svn://rubyforge.org/var/svn/apptrain/trunk/vendor/plugins/safety_net
That’s it. Now, If the test database points to the same database as development or production running rake will display the following message:

rake aborted!
The name of your test database matches production or development.
How it Works
Purging the poor innocent database is prevented in two ways.
- By adding a prerequisite check to the rake script that normaly performs this task.
- By modifying the fixtures method on ActiveRecord to avoid the same fate when running individual tests with the ruby command.
Posted in Create | Tags apptrain, plugin, rails, rails test database, rake, safetynet | 1 comment
Posted by Mike Blake
Wed, 31 Jan 2007 16:25:00 GMT
As you develop a nice well organized Rails application, you’re sure to utilize some of the great open source code already written by Rails developers.
A common practice is to install a plugin or some other code into your application, then add that code to subversion. Some problems with this are that a checkout of your app now takes longer, and if the plugin improves, you are using old code in your application. A neater solution is to access third party code using SVN Externals.
Using Tortoise SVN, right click inside the vendor directory of your app, then choose ‘properties’ from the menu.

Click the ‘Subversion’ Tab with the little turtle. Then choose svn:externals from the dropdown in the middle of the screen.

Now in the text area, enter the plugin you want to include.
acts_as_authenticated http://svn.techno-weenie.net/projects/plugins/acts_as_authenticated
The format here is important, the first word the entry, before the first space will be the directory name where the code will live. After the first space is the url for the repository. Click ‘OK to save the external. Then go up one directory and do a checkout of the ‘vendor’ directory.
Now:
- If the plugin code changes, you’ll receive the new code whenever you do an Update.
- Additional coders who check out the repository have the option to ‘Omit externals’ if they don’t need the portion of the code you’ve included.
- There’s a clear distinction in your repository between the code you’re maintaining, and third party code.
Posted in Create | Tags externals, rails, subversion, svn, tortoise | no comments
Posted by Mike Blake
Wed, 25 Oct 2006 14:42:00 GMT
A source control server such as Subversion or CVS is such an invaluable tool It’s hard to believe I’ve ever worked without it. Even when I’m the only coder on a project, I routinely check in my code, and refer back to old code when something new I’ve tried goes awry. I’ve made this a habit, and haven’t worked without source control for years now. Until last week.
I just started with a new client that I’m very excited about. There is no source control system in place yet, but I was asked to email some ruby code demonstrating some simple integration with an existing app. After creating an object model, I emailed some files. A day later I did some cool code that connected to another app via Web Services, which pretty much demonstrated what my client wanted to see.
Then I had a clever idea about making it a framework to easily connect to just about any web service. Well my framework is coming along, but guess what, while happily hacking I blew away my simple example that actually worked. Too bad I never emailed that example, or better yet, checked it in!
It’s probably less that an hours worth of work, so let’s hope I’ve learned my lesson: Working without source control is like going on a drinking binge without having a good dinner first. It’s a lesson you should have learned years ago and the next day, you won’t remember a thing.
Posted in Create | Tags control, source | 3 comments