How to get the schema version number out of your Rails migrations

When you want to revert your Rails migration you need the version number so that you can perform the rollback:

rake db:migration VERSION=?
One way is to access the database and take a look in the schema_info table to find the version number.

An easier way is to add an additional task to Rake (found via the Quoted-Printable):
namespace :db do
desc 'Print the current database migration version number'
task :version => :environment do
puts ActiveRecord::Migrator.current_version
end
end
Copy this code into a file called db_version.rake and place it in the lib/tasks directory within your Rails application.

To find out the current version number, simply run:
rake db:version
Technorati Tags: , , , , ,

How to truncate a log file using Ubuntu Linux

Have you ever wanted to truncate a log file that was being written to by a running process without stopping the process to do so?

If so then all you need is the following to clear out the active log file:

cat /dev/null > some_log_file.log
There are some other great tips over on the Bash Cures Cancer blog.

Technorati Tags: , , , , ,

How to add system-wide environment variables to Ubuntu & Debian Linux

Following on from my last post on how to add shared libraries to Linux's system library path I wanted to share a related tip on system-wide environment variables.

Common reasons for doing this are setting your locale, setting the location of a Java installation or specifying the character set that your database is using (if you use Oracle then that would be the NLS_LANG environment variable). Debian actually has a policy against system-wide environment variables stating "a program must not depend on environment variables to get reasonable defaults" and suggests that all programs should have wrapper shell scripts to set the relevant variables.

Ignoring this sound advice I tried adding them to /etc/profile but this didn't work when ssh was used from a remote machine to execute some commands (I was using Capistrano).

I couldn't find a complete way to do it (it wasn't picked up by any start scripts in /etc/init.d) but I achieved the most success by placing my variables in /etc/environment:

LANG=en_GB
JAVA_HOME=/usr/local/src/java
Just remember that you don't need to export anything!

Technorati Tags: , , , , ,