rails-sqlserver / activerecord-sqlserver-adapter

SQL Server Adapter For Rails
MIT License
972 stars 558 forks source link

Bug introduced by PR #1206 (allow spaces in table names) #1237

Open zurchpet opened 14 hours ago

zurchpet commented 14 hours ago

Issue

PR #1206 introduced support for spaces in table names.

Expected behavior

Table names with spaces should be correctly escaped and interpreted.

For example:

Actual behavior

The method ActiveRecord::ConnectionAdapters::SQLServer::SchemaStatements#get_raw_table_name does not correctly identify table names in INSERT statements of the format INSERT INTO tablename SELECT * FROM other_table.

Currently, it returns the entire string tablename SELECT * FROM other_table as the table name, rather than stopping at tablename.

How to reproduce

require "bundler/inline"

gemfile(true) do
  source "https://rubygems.org"
  gem "tiny_tds"
  gem "activerecord", "7.2.1"
  gem "activerecord-sqlserver-adapter", "7.2.0"
end

require "active_record"
require "minitest/autorun"
require "logger"

ActiveRecord::Base.establish_connection(
  adapter:  "sqlserver",
  timeout:  5000,
  pool:     100,
  encoding: "utf8",
  database: "master",
  username: "SA",
  password: "StrongPassword!",
  host:     "127.0.0.1",
  port:     1433,
  )
ActiveRecord::Base.logger = Logger.new(STDOUT)

ActiveRecord::Schema.define do
  drop_table :bug_tests rescue nil

  create_table :bug_tests, force: true do |t|
    t.bigint :external_id
  end
end

class BugTest < ActiveRecord::Base
end

class TestShouldBeWorking < Minitest::Test
  def setup
    BugTest.connection.execute("INSERT INTO bug_tests select('1')")
  end

  def test_should_be_working
    assert_equal 1, BugTest.count
  end
end

class TestWorking < Minitest::Test
  def setup
    BugTest.connection.execute("INSERT INTO bug_tests (external_id) VALUES ('1')")
  end

  def test_working
    assert_equal 1, BugTest.count
  end
end

Details