package org.jruby.webapp;

import javax.servlet.ServletContextListener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContext;

import org.jruby.Ruby;

/**
 * MigratorContextListener - migrates the production schema upon web deploy
 *
 * @author Mike Hagedorn, http://www.silverchairsolutions.com
 * @version 0.9
 */
public class MigratorContextListener implements ServletContextListener {

    
    

		public void contextInitialized(ServletContextEvent event) {
			ServletContext context = event.getServletContext();
			
				System.out.println("Entering: MigratorContentListener.contextIntialized \n");

					Ruby runtime = Ruby.getDefaultInstance();

					/* This script runs the migration when the context loads */

					String script = 
						"require 'java'\n" +
						"require 'rubygems'\n"+
						"gem 'activerecord'\n"+
						"ActiveRecord::Migrator.migrate('db/migrate/',nil)\n";
							
					runtime.evalScriptlet(script);
	   
		}

		public void contextDestroyed(ServletContextEvent event) {
		}

	}


@@ -741,16 +741,18 @@
 */%0A%0A%09%09%09%09
+/*
 %09String 
@@ -908,16 +908,26 @@
 nil)%5Cn%22;
+*/%0A%09%09%09%0A%09%09%09
 %0A%09%09%09%09%09%09%09
@@ -932,16 +932,18 @@
 %09%09%0A%09%09%09%09%09
+//
 runtime.
@@ -970,16 +970,63 @@
 );%0A%09   %0A
+%09%09%09   System.exec(%22jruby -S rake db:migrate%22);%0A
 %09%09%7D%0A%0A%09%09p

make a straight system exec call for the db:migrate command