You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When a view is defined with the entire FROM clause wrapped in parentheses, the table name parsing (ActiveRecord::ConnectionAdapters::SQLServer::SchemaStatements#get_raw_table_name) throws an exception NoMethodError: undefined method '[]' for nil.
Expected behavior
The view is queried and returned without error.
Actual behavior
NoMethodError: undefined method '[]' for nil
How to reproduce
Tested with Ruby 3.3.6.
require"bundler/inline"gemfile(true)dosource"https://rubygems.org"gem"tiny_tds"gem"activerecord","7.1.5"gem"activerecord-sqlserver-adapter","7.1.8"endrequire"active_record"require"minitest/autorun"require"logger"ActiveRecord::Base.establish_connection(adapter: "sqlserver",timeout: 5000,pool: 100,encoding: "utf8",database: "test_database",username: ENV['username'],password: ENV['password'],host: ENV['host'],port: 1433,)ActiveRecord::Base.logger=Logger.new(STDOUT)ActiveRecord::Schema.definedodrop_table:test_table_asrescuenildrop_table:test_table_bsrescuenilcreate_table:test_table_as,force: truecreate_table:test_table_bs,force: truedo |t|
t.bigint:test_table_a_idendexecute'DROP VIEW IF EXISTS test_views'execute<<~SQL.squish CREATE VIEW test_views ( test_table_a_id, test_table_b_id ) AS SELECT test_table_as.id as test_table_a_id, test_table_bs.id as test_table_b_id FROM (test_table_as with(nolock) LEFT JOIN test_table_bs with(nolock) ON (test_table_as.id = test_table_bs.test_table_a_id)) SQLendclassTestTableA < ActiveRecord::Basehas_many:test_views,foreign_key: :test_table_a_idhas_many:test_table_bsendclassTestTableB < ActiveRecord::Basebelongs_to:test_table_aendclassTestView < ActiveRecord::Baseself.primary_key=:test_table_a_idendclassTestBugTest < Minitest::Testdefsetup@object_a=TestTableA.create!@object_b=TestTableB.create!(test_table_a: @object_a)enddeftest_countassert_equal1,@object_a.test_views.countendend
Note the following monkey patch seems to resolve the issue (though I can't verify that this handles all necessary cases):
moduleActiveRecordmoduleConnectionAdaptersmoduleSQLServermoduleSchemaStatementsdefget_raw_table_name(sql)returnifsql.blank?s=sql.gsub(/^\s*EXEC sp_executesql N'/i,"")ifs.match?(/^\s*INSERT INTO.*/i)s.split(/INSERT INTO/i)[1].split(/OUTPUT INSERTED/i)[0].split(/(DEFAULT)?\s+VALUES/i)[0].match(/\s*([^(]*)/i)[0]elsifs.match?(/^\s*UPDATE\s+.*/i)s.match(/UPDATE\s+([^\(\s]+)\s*/i)[1]# Check for a match before accessing []elsifs.match?(/FROM\s+((\[[^\(\]]+\])|[^\(\s]+)\s*/i)s.match(/FROM\s+((\[[^\(\]]+\])|[^\(\s]+)\s*/i)[1]# Return nil if no match foundelsereturnend.stripendendendendend
Details
Rails version: 7.1.5
SQL Server adapter version: 7.1.8
TinyTDS version: 2.1.7
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: no
Thread safety: yes
iconv library: yes
TDS version: 7.3
iODBC: no
unixodbc: no
SSPI "trusted" logins: no
Kerberos: no
OpenSSL: yes
GnuTLS: no
MARS: yes
The text was updated successfully, but these errors were encountered:
Issue
When a view is defined with the entire FROM clause wrapped in parentheses, the table name parsing (
ActiveRecord::ConnectionAdapters::SQLServer::SchemaStatements#get_raw_table_name
) throws an exceptionNoMethodError: undefined method '[]' for nil
.Expected behavior
The view is queried and returned without error.
Actual behavior
NoMethodError: undefined method '[]' for nil
How to reproduce
Tested with Ruby 3.3.6.
Note the following monkey patch seems to resolve the issue (though I can't verify that this handles all necessary cases):
Details
7.1.5
7.1.8
2.1.7
The text was updated successfully, but these errors were encountered: