Jason Fox

Icon

programming, products, and pontifications…

Backgroundjob (Bj) Won’t Start

Recently I had a problem with Bj where it would not start up.  Nothing was written to the backgroundjob log or Rails log and no exception was being thrown.  To make the problem even stranger, Bj would start-up just fine in development but not in production but worked just fine in production from script/console.  After digging into the Bj code and adding some debug statements I found the problem.

# database.yml
development:
  adapter: mysql
  database: my_development
  username: me
  password: password
  host: localhost
  port: 3306

test:
  adapter: mysql
  database: my_test
  username: me
  password: password
  host: localhost
  port: 3306

production:
  development

Bj was getting an ActiveRecord::ConnectionNotEstablished exception but was swallowing it.  The solution was to explicitly define the production database connection in database.yml.

acts_as_universally_unique

I recently found the need to provide UUIDs for ActiveRecord models in a service that I’m developing.  I wasn’t able to find a suitable soution, so, I rolled my own.  Enter acts_as_universally_unique.  The plugin simply adds a (customizabe) UUID field to all ActiveRecord models that act_as_universally_unique.  I will be adding additional methods (and test cases) to it shortly.

Check Constraints and MySQL

Unfortunately, MySQL does not support check constraints out of the box.  This makes the task of enforcing business logic in the database layer difficult, but not impossible.  I recently found this approach to implementing check constraints in MySQL.  It’s not as pretty and clean as I’d like, but, it’s the best approach that I’ve found so far.

Now. why would you want to encode business logic in the database?  Can’t you make due with your ActiveRecord::Validations?

Well, have you ever updated the database directly?  Have you ever called update_attribute on an object?  How about save_with_validation(false)?   Yeah, I thought so.  Read more about why you should treat your database as a fortress in Dan Chak’s recently released book, Enterprise Rails (review coming soon).