Bringing Ruby’s ActiveRecord to Java

Engineering

Learn from our challenges and triumphs as our talented engineering team offers insights for discussion and sharing.

Bringing Ruby’s ActiveRecord to Java

Engineering

LiveRamp started out using Ruby and Rails extensively to build out our systems. We loved the flexibility that it gave us to quickly put together a functional application. Tools like ActiveRecord are huge productivity boosters, saving us the trouble of hand-coding database interaction and letting us focus directly on our application.

However, we evolved to need something more than Ruby. While we continue to use Rails for our website and our internal administration site, we’ve moved to using Java (and Hadoop/Cascading) to power our processing pipeline. Oftentimes, our Java applications need to access the same databases as our Ruby sites.

This seems like a simple problem, but we’ve encountered a couple issues. Firstly, although Java has a number of database abstraction packages, we didn’t find them quite up to the level of ActiveRecord in terms of ease of use or flexibility. Packages like Hibernate are cumbersome and complex compared to ActiveRecord, and that’s led to our developers using them inconsistently or avoiding frameworks altogether.

Framework aside, there’s still an even more significant problem – namely, how do we scalably share our growing schema across two separate languages/frameworks? Between our two main production databases, we have over 220 tables. At that number, manually maintaining more than one version of our data definition for different frameworks is tedious and error-prone. In practice, they won’t be synchronized correctly, leading to confusion at deployment time when problems are finally uncovered.

Our initial solution was to limp along with carefully hand-coded Java models that mirrored their Ruby counterparts. This was a pretty poor solution, as it meant that only the minimum set of models required for the current application would get built, and features were lacking. We finally decided that we’d had enough. What if we could establish a parallel framework in Java that leveraged the work we already do in ActiveRecord?

It turns out that this is easier than it sounds. Over the course of the last month, we’ve build Jack, a set of Ruby scripts and a concordant library of Java classes that together allow you to convert your Ruby ActiveRecord migrations and models into automatically generated Java classes. The general idea is that we parse schema.rb for the table schema and each of the models for their associations and other configuration elements. Once we’ve got it all parsed up, it’s trivial to generate brand-new Java classes.

We’ve decided to open source this project in hopes that others can benefit from and contribute to the project. You can find the code and some documentation on GitHub. We look forward to hearing your thoughts!