Monday, January 12, 2009

How to remove windows carriage return (^M) on Mac

I don't want to install dos2unix via port and I know it's just a matter of search and replace those ^M chars.

Here are a couple of command that should do the job of removing those trailing ^M (carriage return) chars.

perl -pi -e's/\r//g' filename
sed -e's/^M//g' filename > newfile

To type ^M in Terminal do Ctrl+V then Ctrl+M.

Friday, January 9, 2009

Use ActiveRecord migration to manage database changes in Java projects

A sort of sequel to dumping schema into ruby. I am going to show how we can use ActiveRecord migration to manage our database change in Java projects.

Problem:
Automatically track which schema change has been made to a database.
Make it easy to recreate another database with the same schema.

Requirements:
bsf.jar (from jruby binary download)
jruby-complete jar
jdbc jar
activerecord gem
activerecord-jdbc-adapter gem

Resources:
http://www.ics.muni.cz/~makub/ruby/#c2

Steps:
install jruby
jruby -S gem install --no-rdoc --no-ri activerecord
jruby -S gem install --no-rdoc --no-ri activerecord-jdbc-adapter


Known issues:

The jruby-complete jar contains rubygems and you can package up the other gems (above) into the jruby-complete jar and distribute it (check it into vcs) that way.

Another way is to set GEM_PATH env variable to the location of the jruby gem directory.
e.g. /Users/andy/dev/tools/jruby/lib/ruby/gems/1.8

At the moment, you need to set GEM_PATH to be the jruby gem directory, which means each machine will require jruby to be installed plus the gems used.
It would be more ideal if we can just repackage jruby-complete jar with the additional gems to make deployment easier.


Update: Thanks to Nick's blog, you no longer have to set GEM_PATH to get the gems required included by the base application.

How to do mass renaming of file extension

Problem:
You want to rename all the .wiki files to be .textile instead.

Solution:
\ls -1 *.wiki | while read f; do mv $f ${f%.wiki}.textile; done

Easy way to backup codebase on USB key with git

Requirements:
git (via cygwin)
$ git --version
git version 1.6.0.4

On USB key
mkdir project.init and cd into it
git --bare init

In project codebase's directory
git init
git add .
git ci -m'first commit'
git remote add origin /path/to/project.git
git push origin master


Modify you local master to track remote by appending the following to your project's .git/config file:
[branch "master"]
remote = origin
merge = refs/heads/master

Then you can just do the following in the future to push new files to your USB.
git add .
git ci -m'update repos'
git push


Gotchas when using git on cygwin

$ git push origin master
Counting objects: 6669, done.
Compressing objects: 100% (6570/6570), done.
Writing objects: 100% (6669/6669), 7.90 MiB | 348 KiB/s, done.
Total 6669 (delta 4488), reused 0 (delta 0)
*** Project description file hasn't been set
error: hooks/update exited with error code 1
error: hook declined to update refs/heads/master
To /cygdrive/e/westpac/compass.git
! [remote rejected] master -> master (hook declined)
error: failed to push some refs to '/cygdrive/e/project.git'


The above problem is caused by a hook script (.git/hooks/update).
Rename update to something like update.bak so it does not get executed solves the above problem.

p.s. with the newer version of cygwin git the hook scripts are all suffixed with .sample in the file name, which means they don't get executed hence won't have the above problem.

Use jruby to dump DB schema to ruby

Problem:

I have a database that rails doesn't have an easy way to connect to.

Solution:

Use jruby & JDBC

Requirements:

jruby
activerecord
activerecord-jdbc-adapter
jdbc driver for the database you want to connect to

Resources:

http://jruby-extras.rubyforge.org/activerecord-jdbc-adapter/

Steps:

jruby -S gem install activerecord
jruby -S gem install activerecord-jdbc-adapter

Put the jdbc jar in Classpath for jruby. e.g. inside C:\work\tools\jruby-1.1.6\lib

Run this script:

require 'rubygems'
gem 'activerecord-jdbc-adapter'
require 'jdbc_adapter'
require 'active_record'

ActiveRecord::Base.establish_connection(
:adapter => 'jdbc',
:driver => 'com.microsoft.sqlserver.jdbc.SQLServerDriver',
:url => 'jdbc:sqlserver://localhost:1433;DatabaseName=FineosSixApp',
:username => 'username',
:password => 'password'
)

ActiveRecord::SchemaDumper.dump


To load the schema into say mysql
require 'rubygems'
require 'active_record'

ActiveRecord::Base.establish_connection(
:adapter => "mysql",
:host => "localhost",
:username => 'username',
:password => 'password',
:database => 'database'
)

load('schema.rb')

Future Improvements:

Instead of using jruby on command line, create an ant task that uses jruby.jar and do the same thing.
Basically integrating the schema dump into the build file.

It'll be useful to store the client code base along with schema that runs with the code base.