Bug in the activerecord-sqlserver-adapter-2.2.21 gem

There is a bug in the activerecord-sqlserver-adapter-2.2.21 gem.

The error is in the file:

\lib\active_record\connection_adapters\sqlserver_adapter.rb

Line 1058 reads:

FROM #{db_name}INFORMATION_SCHEMA.COLUMNS columns

But should read

FROM #{db_name}.INFORMATION_SCHEMA.COLUMNS columns

The lack of a full stop between the db_name and INFORMATION_SCHEMA causes an error because the database name gets appended to INFORMATION_SCHEMA and you get an error. For example, if the database was ‘db_one’ you’d get the error:

Invalid object name 'db_oneINFORMATION_SCHEMA.COLUMNS'

When you try a connection.select (e.g. connection.select_all(sql))

An update
There could be an issue with the simple solution of just adding a period. The issue is that if a db_name isn’t speciftied the SQL code could need to be just:

FROM INFORMATION_SCHEMA.COLUMNS columns

Therefore add a private method to sqlserver_adapter.rb:

def db_name_with_period(db_name)
  "#{db_name}."
end

And then update the problem line to:

FROM #{db_name_with_period(db_name) if db_name}INFORMATION_SCHEMA.COLUMNS columns

The problem has been posted to github:

http://github.com/rails-sqlserver/2000-2005-adapter/issues/closed/#issue/3

update 15-Jan-10
The problem is still present in the latest version of the adapter and therefore I have added a new issue.

This entry was posted in Ruby. Bookmark the permalink.