Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use fully qualified database name when retrieving column definitions … #426

Merged
merged 1 commit into from
Nov 23, 2015
Merged

Use fully qualified database name when retrieving column definitions … #426

merged 1 commit into from
Nov 23, 2015

Conversation

jippeholwerda
Copy link

…to support linked servers.

We have to retrieve data from SQL Server 2008. Since that doesn't work with the latest activerecord-sqlserver-adapter and we do have the possibility of using a SQL Server 2012 instance, we've configured the 2012 database engine to be able to execute queries/commands against the 2008 instance. This can be achieved by adding the 2008 server as a "linked server" in the 2012 instance.

However, querying a linked server requires using the fully qualified database name:

select * from [server].[database].[dbo].[table]

In the current master, using a linked server fails with the following error:
TinyTds::Error: Invalid object name 'database.INFORMATION_SCHEMA.COLUMNS'
when retrieving the table definition of a linked server.

This pull request should fix that by using the fully qualified database name.

An example of a model that uses a linked server is:

class LinkedServerTest < ActiveRecord::Base
  self.table_name = "[server].table.dbo.linked_server_test"

  # Setting the table alias used in Arel is necessary since otherwise the
  # fully qualified table name is used in the select_list of the select clause,
  # causing SQL Server to complain about the maximum number of prefixes.
  self.arel_table.table_alias = "linked_server_test"
end

metaskills added a commit that referenced this pull request Nov 23, 2015
Use fully qualified database name when retrieving column definitions …
@metaskills metaskills merged commit c3b2082 into rails-sqlserver:master Nov 23, 2015
@metaskills
Copy link
Member

Brilliant side step solution for 2008 and Rails 4.2 and up! I just merged this in and published v4.2.6 of the adapter. Cheers!

@metaskills
Copy link
Member

@jippeholwerda Also... may I introduce you to my favorite minitest/rails gem :) https://github.com/metaskills/minitest-spec-rails

@theangryangel
Copy link

Sorry for intruding, but I've got a question for @jippeholwerda - I had assumed that linked servers were passed SQL verbatim. Unfortunately I'm not in a position where I can test this until the end of the week and I'm dying to know the answer 😅 Are you saying that with a small 2012 (or newer instance) with a linked 2008 R2 server, I can issue FETCH and OFFSET queries (for example), and they're appropriately translated for 2008 R2?

@metaskills
Copy link
Member

That was my assumption too. I'd like to hear the answer @jippeholwerda

@jippeholwerda
Copy link
Author

@theangryangel That is correct. The 2012 engine correctly translates the FETCH and OFFSET queries so that 2008 understands.

@theangryangel
Copy link

Shit. I wish I'd actually tried that rather than assumed it wouldn't. Would've saved me a bunch of trouble 😆 Cheers for the update @jippeholwerda I appreciate it ❤️

@jippeholwerda
Copy link
Author

However, when testing I did run into an issue with INSERT statements. Apparently, specifying the Arel table alias adds it to the INSERT as well, resulting in a syntax error. Will have to dive further into that.

@rept
Copy link

rept commented Nov 23, 2015

Great stuff. And out-of-the box solution!

metaskills added a commit that referenced this pull request Dec 23, 2015
* Rename `remote_server?` to `database_prefix_remote_server?`
* Make `database_prefix_remote_server?` stronger by checking if #object is left blank.

Misc refactors.

* Remove `@config` init ivar. Not used.
* Use `connection_options` helper in tests for terse win.

cc @jippeholwerda
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants