ARM

Support ARM version: 0.0.x

ARM generates project skeleton for Java project to embed ActiveRecord Migration.

GEM Installation

Run the following if you haven’t already:

gem sources -a http://gems.github.com

Install the gem:

sudo gem install flextao-arm

Use it in your project

  • To install ARM and generate project skeleton, use cruby. I never tried it in jruby.

  • Install activerecord, activesupport and rails gems, by default, ARM takes version 2.3.2, you can specify a system environment variable ACTIVERECORD_VERSION to change the version.

  • Install activerecord-jdbc-adapter gem

  • go to your Java project root directory

  • run script ‘arm’, it will create project skeleton for you

  • add jruby complete jar and database jdbc jars into lib dir

  • add migration into db/migrate directory

  • config database.yml, see config/database.yml.example for example

  • do migrate: ‘./script/jrake db:migrate’

Project skeleton generated by ARM

-- db       => Same directory with Rails
-- config   => Same directory with Rails, you need create a database.yml here
-- lib      => Put jruby complete jar and jdbc jars here
-- script   => Jrake and jrake.bat inside
-- vendor
---- ext    => ActiveRecord 2.3.2 patch and activerecord-jdbc-adapter postgre fix patch inside
---- gems   => All gems need would be froze here
-- Rakefile => Initialize a Rails environment and load Rails database and misc tasks.
               Take a look after created, and customs it as you need.

Tips

  • Why version 2.3.2?

    ActiveRecord assumes type of table primary key (e.g. id) is integer, but you may need it as string type in a Hibernate project. ARM has a patch for activerecord 2.3.2 to dump database schema with string type primary key. If your Java project is using integer as table primary key, you should be able to use any version of activerecord.

  • Different convention between Java and Rails

    In Java, most of time use long over integer as column type and use double over float as column type. In Rails, they are opposite.

  • Integrate with Hibernate

    Change the property ‘hibernate.hbm2ddl.auto’ to be ‘validate’, so that Hibernate would validate the Java models with database schema on startup.

  • Run migration in Java (Web Application). The following is a simple Migrator implementation.

    public class Migrator {
      /**
       * this is a simple check method to verify directory to run migration
       */
      public static boolean isMigrationBaseDir(String dir) {
          return new File(path(dir, "db")).exists();
      }
    
      private final String base; // base directory to run migration
    
      public Migrator(String base) {
          this.base = base;
      }
    
      public void migrate() {
          Ruby runtime = JavaEmbedUtils.initialize(new ArrayList());
          runtime.evalScriptlet(script());
      }
    
      private String script() {
          StringBuffer buffer = new StringBuffer();
          buffer.append("require 'rubygems'\n");
          buffer.append("require 'rake'\n");
          if (".".equals(base)) {
              buffer.append(runRakeScript());
          } else {
              buffer.append("Dir.chdir('" + base + "') do\n");
              buffer.append("  " + runRakeScript() + "\n");
              buffer.append("end\n");
          }
          return buffer.toString();
      }
    
      private String runRakeScript() {
          // The rake file generated by ARM, defines db:migrate task as default task
          // so you could just run the default task here.
          return "Rake.application.run";
      }
    }
    

    And also, you may need the following packaging script in your ant war target, then you could write a Servlet to do migration on server startup:

    <webinf dir="${java.project.dir}">
     <include name="db/**/*"/>
     <include name="config/**/*"/>
     <include name="script/**/*"/>
     <include name="vendor/**/*"/>
     <include name="Rakefile"/>
    </webinf>
    

License

ARM is available under an MIT License Copyright © 2009 Flextao.

Author

Li Xiao <[email protected]>