Norman Kraft
Norman Kraft

Reputation: 183

Connection fails, database has '.' in name

I've been trying to debug a SQL Server database error in one of our rails apps. The problem is that the database name, as added in database.yaml, has a '.' in it ('testdb.v1'). It results in the error:

TinyTds::Error: Database 'testdb' does not exist. Make sure that the name is entered correctly.

Thinking that this was a YAML vs. Rails issue with quoting of the '.' character, I opened the app in rails console and tried every kind of Ruby quoting I could think of, but I get the same error each time.

Somewhere down the line from the assignment of the database name in my model.connection_config, the '.' is being interpreted as a format specifier and the suffix is dropped from the name. model.connection_config['database'] has 'testdb.v1', as it should, but clearly that's not what's being handed to SQL Server.

I wasn't sure if this is a rails issue or a SQL Server issue, though a C# app I tossed together manages to open the database without problems. Thus, I assume it's a rails/ActiveRecord issue. Unfortunately, I don't have the option of changing the database name.

Is there a way to quote the '.' character so that it will maintain the '.' and the suffix in the database name?

Upvotes: 3

Views: 115

Answers (1)

Norman Kraft
Norman Kraft

Reputation: 183

Further investigation took me to the github page for the activerecord-sqlserver-adapter

There, I found issues 230 and 226, with similar problems to mine. Apparently using '.' in database, table or column names will present similar problems. As these were created in 2012 and are still open, it doesn't look like a fix is coming anytime soon. In the comments there are a few ideas to monkey-patch it into working. Guess that's the approach I'll have to take to get this working.

UPDATE: For anyone stuck with a similar problem, and the database name is the only issue (not tables or columns), the solution we went with was an end-run around the sqlserver-adapter. We created a SQL user for each database, and assigned a default database to that user. Then, we connected with the user login but left the database name null. SQL Server will connect to the default database in that case.

Upvotes: 2

Related Questions