Ruby see, Ruby do!

Was reading some books and after a few discussions with a friend of mine, Greg, who's the boss of EyeTools and a Ruby fan. For a database-centric guy, this is one heck of an eye-opener: migrations, a Ruby on Rails tool (or a gem), that helps you manage your database through classes, no SQL Queries to write at all! Note, Ruby is a language, and Ruby on Rails is an entire framework that consists of ready-built classes that you rarely need to poke around with that rides on top of Ruby.

I won't go to much details on how to get going on using Ruby on Rails or setting up a database as there's well written documentations only a "Google" away.

Migrations is something a non-SQL programmer could potentially use to add columns, primary keys, etc to your tables. It can even be effective for guys in other langauges like C#, C++, VB.NET, etc... let me show you some code examples -- yeah, some of the Ruby on Rails code syntax may throw you off -- looks too easy!

Let's create a table:


class AddClientTable def self.up
createtable :clients do |table|
table.column :name, :string
table.column :address, :string, :null => false
table.column :city, :string, :limit => 32, :null => false
table.column :email, :string
end
end

def self.down
drop
table :clients
end
end

The self.up stuff is obviously the new stuff, and the self.down stuff is in case you need to backtrack from the changes you're making. Point is.. using migrations, all changes are self-documenting and easy to revert back. Excellent for DBAs with OCD.

Notice there's no ID field? Well, Ruby on Rails Migrations automatically adds it for you and sets the increment to 1. If you don't want an ID field, just replace the above crate table line to:

create_table :clients, :id => false do |table|

The possibilities are endless! unique constraints, text columns, varchar, char, foreign keys, default values, etc, etc... there are some limitations where you'll have to write up straight-up SQL like numeric(8,4) or whatever... but that SQL can be embedded in the class tho' so it still retains that self-documentation.

All in all, an awesome tool for the budding DBA.