activerecord-sqlserver-adapter icon indicating copy to clipboard operation
activerecord-sqlserver-adapter copied to clipboard

Poor support of lowercase_schema_reflection

Open one-more-alex opened this issue 8 years ago • 2 comments

I started use of adapter and from first days enabled lowercase_schema_reflection = true

But I faced with a bug in primary keys and proposed fix #https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/639

After several days of development I mentioned indexes case became uppercase again in schema.

I think mistakes are here:

        def indexes(table_name, name = nil)
          data = select("EXEC sp_helpindex #{quote(table_name)}", name) rescue []
          data.reduce([]) do |indexes, index|
            index = index.with_indifferent_access
            if index[:index_description] =~ /primary key/
              indexes
            else
              name    = index[:index_name]
              unique  = index[:index_description] =~ /unique/
              where   = select_value("SELECT [filter_definition] FROM sys.indexes WHERE name = #{quote(name)}")
              columns = index[:index_keys].split(',').map do |column|
                column.strip!
                column.gsub! '(-)', '' if column.ends_with?('(-)')
                column
              end
              indexes << IndexDefinition.new(table_name, name, unique, columns, nil, nil, where)
            end
          end
        end

Variables "name" and "column" should respect lowercase_schema_reflection. But they did not.

What I think, it worth to note in documentation, that lowercase_schema_reflection is rather experimental. Finally I turned off lowercase_schema_reflection in my code...

one-more-alex avatar Mar 10 '18 21:03 one-more-alex

If the intention of lowercase_schema_reflection is really just for convention to allow lower case method names and that is all. Then the simplest patch I can come up with is leave this option turned off and then add the following to ApplicationRecord:

def self.inherited(subclass)
  super
  subclass.class_eval do
    attribute_names.each do |attr|
      alias_attribute attr.downcase, attr
    end
  end
end

This seems to be the least breaking change while still allowing all method names to be accessed accordingly. Additionally this would allow for conversion to snake case as well.

 def self.inherited(subclass)
  super
  subclass.class_eval do
    attribute_names.each do |attr|
      alias_attribute attr.gsub(/(?<!^|[A-Z])[A-Z]/){"_#$&"}.downcase, attr
    end
  end
end

Now if you have columns like "SomeTableNameId" or even "SomeTableNameID" it will be converted to:

 some_table_name_id

engineersmnky avatar Nov 25 '19 16:11 engineersmnky

I agree with @engineersmnky and I think lowercase_schema_reflection should not be used. I am thinking of deprecating this API in favor of something more "rails way" as mentioned above.

I am closing this for now as a wont-fix, but I'd be happy to review a PR with a better solution if someone has one.

wpolicarpo avatar May 19 '20 18:05 wpolicarpo