rails-sqlserver/activerecord-sqlserver-adapter — pull request #298 by annaswims (2014-01-16 23:53:36+00:00)
Title: Rails 4 Support

# Tested with:
- Rails: 4.0.0, 4.0.1, 4.0.2
- Ruby: 1.9.3, 2.0.0, ruby-2.1.0-preview1
- OS: OSX
- DB: SQL Server 2008

# Todo's

* Fix the bug detailed below
* arel/visitors/sqlserver has only about 75% test coverage.
* Isolation levels are now supported in Rails itself so we should remove/refactor our Isolation level code.
* Tests haven't been run with odbc (I had trouble setting it up on my mac)
* DatabaseStatements#exec_query has a hacky regex in it.
* Change the tiny_tds version back to one that works
* I added Arel tests the rake task. However, many fail because SQL Server quoting tables with brackets. I made a regex to replace brackets with quotes in cases/arel_helper. It's not great, but better than not running the tests.
* Add more meaningful tests where the tests are "assert true"
* Rails now has a place for SQL Server rake tasks. We should consider moving our tasks there.

# Failed Tests & Segfault
* I've seen Segmentation faults with tiny_tds 0.6.0 & 0.6.1 but have successfully run the test suite with master (https://github.com/rails-sqlserver/tiny_tds.git)

* FinderTest#test_coerced_find_with_order_on_included_associations_with_construct_finder_sql_for_association_limiting_and_is_distinct does not pass

It generates different SQL in Rails 4.0.0 (good) and Rails 4.0.1-2 (failing).
```ruby
Post.all.merge!(
:includes => { :authors => :author_address },
:order => 'author_addresses.id DESC ',
:limit => 2)
```

* The SQL generated in Rails 4.0.0:

```
EXEC sp_executesql N'SELECT TOP (2) [posts].id FROM [posts] LEFT OUTER JOIN [categorizations] ON [categorizations].[category_id] = [posts].[id] LEFT OUTER JOIN [authors] ON [authors].[id] = [categorizations].[author_id] LEFT OUTER JOIN [author_addresses] ON [author_addresses].[id] = [authors].[author_address_id] GROUP BY [posts].id ORDER BY MAX(author_addresses.id)'

EXEC sp_executesql N'SELECT [posts].[id] AS t0_r0, [posts].[author_id] AS t0_r1, [posts].[title] AS t0_r2, [posts].[body] AS t0_r3, [posts].[type] AS t0_r4, [posts].[comments_count] AS t0_r5, [posts].[taggings_count] AS t0_r6, [posts].[taggings_with_delete_all_count] AS t0_r7, [posts].[taggings_with_destroy_count] AS t0_r8, [posts].[tags_count] AS t0_r9, [posts].[tags_with_destroy_count] AS t0_r10, [posts].[tags_with_nullify_count] AS t0_r11, [authors].[id] AS t1_r0, [authors].[name] AS t1_r1, [authors].[author_address_id] AS t1_r2, [authors].[author_address_extra_id] AS t1_r3, [authors].[organization_id] AS t1_r4, [authors].[owned_essay_id] AS t1_r5, [author_addresses].[id] AS t2_r0 FROM [posts] LEFT OUTER JOIN [categorizations] ON [categorizations].[category_id] = [posts].[id] LEFT OUTER JOIN [authors] ON [authors].[id] = [categorizations].[author_id] LEFT OUTER JOIN [author_addresses] ON [author_addresses].[id] = [authors].[author_address_id] WHERE [posts].[id] IN (3, 2) ORDER BY author_addresses.id DESC'
```

* The SQL generated in Rails 4.0.1:

```
EXEC sp_executesql N'SELECT DISTINCT TOP (2) [posts].[id], author_addresses.id AS alias_0 FROM [posts] LEFT OUTER JOIN [categorizations] ON [categorizations].[category_id] = [posts].[id] LEFT OUTER JOIN [authors] ON [authors].[id] = [categorizations].[author_id] LEFT OUTER JOIN [author_addresses] ON [author_addresses].[id] = [authors].[author_address_id] ORDER BY author_addresses.id DESC'

EXEC sp_executesql N'SELECT [posts].[id] AS t0_r0, [posts].[author_id] AS t0_r1, [posts].[title] AS t0_r2, [posts].[body] AS t0_r3, [posts].[type] AS t0_r4, [posts].[comments_count] AS t0_r5, [posts].[taggings_count] AS t0_r6, [posts].[taggings_with_delete_all_count] AS t0_r7, [posts].[taggings_with_destroy_count] AS t0_r8, [posts].[tags_count] AS t0_r9, [posts].[tags_with_destroy_count] AS t0_r10, [posts].[tags_with_nullify_count] AS t0_r11, [authors].[id] AS t1_r0, [authors].[name] AS t1_r1, [authors].[author_address_id] AS t1_r2, [authors].[author_address_extra_id] AS t1_r3, [authors].[organization_id] AS t1_r4, [authors].[owned_essay_id] AS t1_r5, [author_addresses].[id] AS t2_r0 FROM [posts] LEFT OUTER JOIN [categorizations] ON [categorizations].[category_id] = [posts].[id] LEFT OUTER JOIN [authors] ON [authors].[id] = [categorizations].[author_id] LEFT OUTER JOIN [author_addresses] ON [author_addresses].[id] = [authors].[author_address_id] WHERE [posts].[id] IN (1, 1) ORDER BY author_addresses.id DESC'
```