rails-mssql-tools released

migrations with mssql 2005

My latest plugin, rails-mssql-tools, is now on Google Code. The README file is a good start. The plugin is a very simple solution to the 2 biggest challenges my team faces when using mssql with Rails:

  1. migrating procs, views, and triggers and
  2. conventionalizing model object attribute names for legacy db’s, with typical Microsoft development camel-casing for column names.

The plugin resolves both in a highly simplistic fashion, with the former relying on minimal code and maximum directory structure convention, while the latter overrides method_missing on ActiveRecord’s Base class to employ ActiveSupport’s string extensions.

README
=== Tools for Microsoft Sql Server 2005 in Rails ===

A set of tools and conventions for using mssql 2005 with Rails.

=====================
Author: Luke Wendling
Created: 2007-10-09
Version: 0.0.1 beta
=====================

== Project home: http://code.google.com/p/rails-mssql-tools/

== Installation

ruby script/plugin install http://rails-mssql-tools.googlecode.com/svn/trunk/

== Usage

The objective of this plugin is to:

1. implement a conventional method for migrating mssql procs, views, and triggers.
2. for legacy mssql databases, resolve camel-cased attributes to Rails' conventional 
    underscored names, allowing developers to refer to model object attributes in 
    the conventional manner:
    model.user_name vs. model.UserName (camel-case is a common naming convention 
    in the MS dev community)

= Camel-case resolution assumptions
    - the legacy db consistently uses camel-casing for column names (not a mix of 
    camel-case and underscore)

= An example Rails migration file might now look like the following:

class AddNewColumnsAndUpdateTriggers < ActiveRecord::Migration
  def self.up
      # column with an associated trigger is changed
    rename_column :widgets, :old_col_name, :new_col_name

    # new column added to table with triggers
    add_column :widgets, :another_new_column, :string

    # this method is added by the plugin
    update_routines('triggers/widgets/tg_update_widgets.2.sql', 
        'triggers/widgets/tg_delete_widgets.2.sql', 
        'triggers/widgets/tg_insert_widgets.2.sql')
  end

  def self.down
    rename_column :widgets, :new_col_name, :old_col_name

    remove_column :widgets, :another_new_column, :string

    update_routines('triggers/widgets/tg_update_widgets.1.sql', 
        'triggers/widgets/tg_delete_widgets.1.sql', 
        'triggers/widgets/tg_insert_widgets.1.sql')
  end
end

Note that the ...1.sql and ...2.sql file naming convention does not have to follow
to the <999>_my_new_migration convention of standard Rails' migration files. Versions 
of proc/view/trigger scripts need only increment as the scripts are changed. The 
version number is not stored in a table, a la schema_info in the prod/dev db. You are
free to manage script version numbering as you wish.

== TODO
Most of the ActiveRecord finders and methods have been overridden to map underscored
attributes to camel-case before querying the db. Known remaining fixes include:
    * methods accepting string sql commands or snippets such as 
    delete_all("UserId = 1") or find_by_sql("select UserId from Users")
    * unit testing

== License
Released under the MIT license (download your own if you need it)

Posted by Luke on Tuesday, October 09, 2007

Comments (0)


No comments.