rails-sqlserver/activerecord-sqlserver-adapter

7.0.2.0 identity insert issue

rgeerts opened this issue · 0 comments

Issue

I just upgraded from 7.0.1.0 to 7.0.2.0 and I am trying to run seed some data in a database that calls
exec_insert in activerecord-sqlserver-adapter-7.0.2.0/lib/active_record/connection_adapters/sqlserver/database_statements.rb
with a query that looks something like "insert into [schema_x].[table_y](id, value) values(1,'abc')". unfortunately, I am getting an error "ActiveRecord::ActiveRecordError: IDENTITY_INSERT could not be turned ON for table [table_y]"

I put in a quick monkey patch that looked like this and the things I was working on started working.

def query_requires_identity_insert?(sql)
          return false unless insert_sql?(sql)
          raw_table_name = get_raw_table_name(sql)
          id_column = identity_columns(raw_table_name).first
          #id_column && sql =~ /^\s*(INSERT|EXEC sp_executesql N'INSERT)[^(]+\([^)]*\b(#{id_column.name})\b,?[^)]*\)/i ? SQLServer::Utils.extract_identifiers(raw_table_name).object : false
          id_column && sql =~ /^\s*(INSERT|EXEC sp_executesql N'INSERT)[^(]+\([^)]*\b(#{id_column.name})\b,?[^)]*\)/i ? raw_table_name : false
end

Expected behavior

It looks like it is generating sql that looks like this

"SET IDENTITY_INSERT [table_y] ON" and I would expect it to generate code that looks like "SET IDENTITY_INSERT [schema_x].[table_y] ON"

Our database has many schemas
Our model looks like
class TableY
self.table_name = "schema_x.table_y"

...
end

Actual behavior

How to reproduce

Details

  • Rails version: 7.0.4.3

  • SQL Server adapter version: 7.0.2.0

  • TinyTDS version: 2.1.5

  • FreeTDS details:

    Compile-time settings (established with the "configure" script)
                              Version: freetds v1.3.13
               freetds.conf directory: /usr/local/etc
       MS db-lib source compatibility: no
          Sybase binary compatibility: yes
                        Thread safety: yes
                        iconv library: yes
                          TDS version: 7.3
                                iODBC: no
                             unixodbc: yes
                SSPI "trusted" logins: no
                             Kerberos: yes
                              OpenSSL: yes
                               GnuTLS: no
                                 MARS: yes