0

I'm moving a legacy app from MS-SQL to Postgres which uses Rails to access the data.

The columns in MS-SQL are capitalised, and while using activerecord-sql-server-adapter, they are read like this:

var something = my_model.SomeAttribute

Even though it is constant, it doesn't appear to matter since the app only reads data from the MSSQL db.

The issue I'm having now is after moving to postgres, it converts all column names etc to lowercase (as SQL is not meant to be case-sensitive). Now when I try to access SomeAttribute on my model, it raises an ActiveModel::MissingAttributeError since it's now lowercase.

Some examples of the symptom:

p.SomeAttribute
=> ActiveModel::MissingAttributeError: missing attribute: SomeAttribute

p.read_attribute(:SomeAttribute)
=> nil

p.has_attribute?(:SomeAttribute)
=> false

p.read_attribute(:someattribute)
=> 'expected value'

Is there some way I can get ActiveRecord/ActiveModel to convert attribute names to lowercase before attempting to retrieve them?

arrtchiu
  • 1,076
  • 1
  • 10
  • 23

2 Answers2

0

Disclaimer: this was a very temporary solution - definitely not the "right" thing to do!

Created views like this:

CREATE VIEW mymodel AS
    SELECT
        at.someattribute
        , at.someattribute AS "SomeAttribute"
    FROM actual_table at

Using the double quotes in SQL preserves the case.

arrtchiu
  • 1,076
  • 1
  • 10
  • 23
0

I am currently working on two different projects using Rails to connect to a legacy MS SQL Server database...with table and column names that don't match up to what Rails expects.

In my most recent project I think I have just found the golden egg :-) And that is to not change anything on the Rails side at all to make things work--the golden egg is to use SQL Views to "transform" the legacy tables into something Rails understands--I currently have a project in dev right now that I'm working on where this is working fantastically, and I don't have to try and alias any table or columns names on the Rails side since everything looks peachy by the time it reaches my Rails app. I'm posting this now because I had many many issues to work through for my first project and I think this may make many other's lives much easier, and I haven't found this solution posted anywhere else.

So, for example, let's say you have a legacy table in Microsoft SQL Server 2008R2 named "Contact-Table" and it has weird column names like such:

Contact-Table:
    ID_Primary
    First Name
    Last Name

Using MS SQL table views you can 'recreate' this same table. Create a view based off of Legacy-Table; name the view whatever you want the 'table' to be called in Rails and use column aliases to rename the columns. So, here we could create a view called "contacts" (in alignment with Rails conventions) with the following columns:

contacts:
    id             (alias for ID_Primary)
    first_name     (alias for First Name)
    last_name      (alias for Last Name)

Then in your Rails model all you need to do link to your 'contacts' table in MS SQL and your column names are available as expected. So far I've done this and it works with the tiny-tds gem and free-tds. I can query, create and update records and Rails associations (has_many/belongs_to, etc.) work as well. I'm very excited about using MS SQL table views instead of other methods I've used before to get Rails to talk to legacy databases! I'd love to hear what others think.

FireDragon
  • 9,325
  • 4
  • 27
  • 34