Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
9ff57a9
The tests now run with rails 4.1.0
Jan 17, 2014
444b0f9
Fixed bind type casting to re-set the casted value back to the bind, …
wbond May 15, 2014
01011a6
Added the :authors fixture to AdapterTest to fix a failure in test_se…
wbond May 15, 2014
c1469b0
Type casting is now happening, so the test regex needs to be updated
wbond May 15, 2014
dbf384b
The test test_to_sql_on_eager_join was seeing slightly different resu…
wbond May 15, 2014
8035c19
Rails 4.1 seems to use strings for database config keys nows
wbond May 15, 2014
e439d83
Fix how we are checking bind tests, can't use coerce since the test m…
wbond May 15, 2014
a451b28
Fixed tests in rails/activerecord/test/cases/connection_adapters/conn…
wbond May 15, 2014
efbead1
Unescape quotes from default values
wbond May 15, 2014
be71de1
Added support for create_table :as, which fixes tests MigrationTest#t…
wbond May 15, 2014
c1e1db1
Clear the schema cache for a table after creating it to prevent a cac…
wbond May 15, 2014
a9b715a
PersistencesTest was renamed to PersistenceTest in rails 4.1, so we h…
wbond May 16, 2014
2f509a6
Use Arel::Nodes::BindParam instead of Arel.sql for substitute_at so t…
wbond May 16, 2014
fa2443e
Coerced test for ActiveRecord::PredicateBuilderTest#test_registering_…
wbond May 16, 2014
f417421
Fixed test NamedScopingTest#test_scopes_honor_current_scopes_from_whe…
wbond May 16, 2014
0b638b7
Added savepoint name param to create_savepoint, release_savepoint and…
wbond May 16, 2014
d176046
Updated UUID enhancements to work with rails 4.1 API
wbond May 16, 2014
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ group :development do
gem 'bcrypt-ruby', '~> 3.0.0'
gem 'bench_press'
gem 'mocha'
gem 'minitest-spec-rails'
# TODO: Change back when it's ready
gem 'minitest-spec-rails', git: "https://github.com/metaskills/minitest-spec-rails.git"
gem 'nokogiri'
gem 'rake', '~> 0.9.2'
gem 'rubocop'
Expand Down
5 changes: 5 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ AREL_PATH = Gem.loaded_specs['arel'].full_gem_path
# Notes for cross compile:
# $ gcla ; bundle install ; rake compile ; rake cross compile ; rake cross native gem

# Since the Gemfile for this project requires, rails, it ends up causing
# Rails.env to be defined, which affects some of the unit tests. We fix this
# by setting the RAILS_ENV to "default_env"
ENV['RAILS_ENV'] = 'default_env'

def test_libs
['lib',
'test',
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4.0.0
4.1.0.pre
29 changes: 15 additions & 14 deletions activerecord-sqlserver-adapter.gemspec
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
$LOAD_PATH.push File.expand_path('../lib', __FILE__)
$:.push File.expand_path("../lib", __FILE__)

Gem::Specification.new do |s|
s.platform = Gem::Platform::RUBY
s.name = 'activerecord-sqlserver-adapter'
s.version = File.read(File.expand_path('../VERSION', __FILE__)).strip
s.summary = 'ActiveRecord SQL Server Adapter. For SQL Server 2005 And Higher.'
s.description = 'ActiveRecord SQL Server Adapter. For SQL Server 2005 And Higher.'

s.authors = ['Ken Collins', 'Anna Carey', 'Murray Steele', 'Shawn Balestracci', 'Joe Rafaniello', 'Tom Ward']
s.email = 'ken@metaskills.net'
s.homepage = 'http://github.com/rails-sqlserver/activerecord-sqlserver-adapter'

s.files = Dir['CHANGELOG', 'MIT-LICENSE', 'README.rdoc', 'VERSION', 'lib/**/*']
s.name = "activerecord-sqlserver-adapter"
s.version = File.read(File.expand_path("../VERSION",__FILE__)).strip
s.summary = "ActiveRecord SQL Server Adapter. For SQL Server 2005 And Higher."
s.description = "ActiveRecord SQL Server Adapter. For SQL Server 2005 And Higher."
s.authors = ['Ken Collins', 'Murray Steele', 'Shawn Balestracci', 'Joe Rafaniello', 'Tom Ward']
s.email = "ken@metaskills.net"
s.homepage = "http://github.com/rails-sqlserver/activerecord-sqlserver-adapter"
s.files = Dir['CHANGELOG', 'MIT-LICENSE', 'README.rdoc', 'VERSION', 'lib/**/*' ]
s.require_path = 'lib'
s.rubyforge_project = 'activerecord-sqlserver-adapter'

s.add_dependency('activerecord', '~> 4.0.0')
s.add_dependency('arel', '~> 4.0.1')
s.add_dependency('activerecord', '~> 4.1.0')
s.add_dependency('arel')
end

Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ def rollback_db_transaction
do_execute 'IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION'
end

def create_savepoint
disable_auto_reconnect { do_execute "SAVE TRANSACTION #{current_savepoint_name}" }
def create_savepoint(name = current_savepoint_name)
disable_auto_reconnect { do_execute "SAVE TRANSACTION #{name}" }
end

def release_savepoint
def release_savepoint(name = current_savepoint_name)
end

def rollback_to_savepoint
disable_auto_reconnect { do_execute "ROLLBACK TRANSACTION #{current_savepoint_name}" }
def rollback_to_savepoint(name = current_savepoint_name)
disable_auto_reconnect { do_execute "ROLLBACK TRANSACTION #{name}" }
end

def add_limit_offset!(_sql, _options)
Expand Down Expand Up @@ -334,7 +334,11 @@ def do_exec_query(sql, name, binds, options = {})
next if ar_column && column.sql_type == 'timestamp'
v = value
names_and_types << if ar_column
v = value.to_i if column.is_integer? && value.present?
if column.is_integer? && value.present?
v = value.to_i
# Reset the casted value to the bind as required by Rails 4.1
binds[index] = [column, v]
end
"@#{index} #{column.sql_type_for_statement}"
elsif column.acts_like?(:string)
"@#{index} nvarchar(max)"
Expand Down
2 changes: 1 addition & 1 deletion lib/active_record/connection_adapters/sqlserver/quoting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def substitute_at(column, index)
if column.respond_to?(:sql_type) && column.sql_type == 'timestamp'
nil
else
Arel.sql "@#{index}"
Arel::Nodes::BindParam.new "@#{index}"
end
end

Expand Down
28 changes: 28 additions & 0 deletions lib/active_record/connection_adapters/sqlserver/schema_creation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,34 @@ def add_column_options!(sql, options)
super
end
end

def visit_TableDefinition(o)
quoted_name = "#{quote_table_name((o.temporary ? '#' : '') + o.name.to_s)} "

if o.as
if o.as.is_a?(ActiveRecord::Relation)
select = o.as.to_sql
elsif o.as.is_a?(String)
select = o.as
else
raise "Only able to generate a table from a SELECT statement passed as a String or ActiveRecord::Relation"
end

create_sql = 'SELECT * INTO '
create_sql << quoted_name
create_sql << 'FROM ('
create_sql << select
create_sql << ') AS __sq'

else
create_sql = "CREATE TABLE "
create_sql << quoted_name
create_sql << "(#{o.columns.map { |c| accept c }.join(', ')}) "
create_sql << "#{o.options}"
end

create_sql
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ def table_exists?(table_name)
super || tables.include?(unquoted_table_name) || views.include?(unquoted_table_name)
end

def create_table(table_name, options = {})
res = super
schema_cache.clear_table_cache!(table_name)
res
end

def indexes(table_name, name = nil)
data = select("EXEC sp_helpindex #{quote(table_name)}", name) rescue []
data.reduce([]) do |indexes, index|
Expand Down Expand Up @@ -256,7 +262,7 @@ def column_definitions(table_name)
nil
else
match_data = ci[:default_value].match(/\A\(+N?'?(.*?)'?\)+\Z/m)
match_data ? match_data[1] : nil
match_data ? match_data[1].gsub("''", "'") : nil
end
ci[:null] = ci[:is_nullable].to_i == 1
ci.delete(:is_nullable)
Expand Down Expand Up @@ -408,8 +414,8 @@ def identity_column(table_name)

private

def create_table_definition(name, temporary, options)
TableDefinition.new native_database_types, name, temporary, options
def create_table_definition(name, temporary, options, as = nil)
TableDefinition.new native_database_types, name, temporary, options, as
end
end
end
Expand Down
2 changes: 2 additions & 0 deletions test/cases/adapter_test_sqlserver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -812,5 +812,7 @@ class AdapterTest < ActiveRecord::TestCase
# This is not run for PostgreSQL at the rails level and the same should happen for SQL Server
# Until that patch is made to rails we are preventing this test from running in this gem.
include SqlserverCoercedTest

fixtures :authors
end
end
50 changes: 29 additions & 21 deletions test/cases/bind_parameter_test_sqlserver.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,33 @@
require 'cases/sqlserver_helper'
require 'models/topic'

class BindParameterTestSqlserver < ActiveRecord::TestCase
end

class ActiveRecord::BindParameterTest < ActiveRecord::TestCase

fixtures :topics

COERCED_TESTS = [
:test_binds_are_logged
]

include SqlserverCoercedTest

# TODO: put a real test here
def test_coerced_binds_are_logged
assert true, 'they are!'
require 'models_sqlserver/topic'
require 'cases/bind_parameter_test'

# We don't coerce here because these tests are located inside of an if block
# and don't seem to be able to be properly overriden with the coerce
# functionality
module ActiveRecord
class BindParameterTest
def test_binds_are_logged
sub = @connection.substitute_at(@pk, 0)
binds = [[@pk, 1]]
sql = "select * from topics where id = #{sub}"

@connection.exec_query(sql, 'SQL', binds)

message = @listener.calls.find { |args| args[4][:sql].include? sql }
assert_equal binds, message[4][:binds]
end

def test_binds_are_logged_after_type_cast
sub = @connection.substitute_at(@pk, 0)
binds = [[@pk, "3"]]
sql = "select * from topics where id = #{sub}"

@connection.exec_query(sql, 'SQL', binds)

message = @listener.calls.find { |args| args[4][:sql].include? sql }
assert_equal [[@pk, 3]], message[4][:binds]
end
end


end


2 changes: 1 addition & 1 deletion test/cases/inheritance_test_sqlserver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def test_coerced_a_bad_type_column

def test_coerced_eager_load_belongs_to_primary_key_quoting
con = Account.connection
assert_sql(/\[companies\]\.\[id\] IN \(N''1''\)/) do
assert_sql(/\[companies\]\.\[id\] IN \(1\)/) do
Account.includes(:firm).find(1)
end
end
Expand Down
6 changes: 6 additions & 0 deletions test/cases/named_scoping_test_sqlserver.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require 'cases/sqlserver_helper'
require 'models/post'
require 'models_sqlserver/post'

# This file is really just to ensure we fix the order by on the
# :ranked_by_comments scope of Post
4 changes: 2 additions & 2 deletions test/cases/persistence_test_sqlserver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
require 'models_sqlserver/topic'
require 'rexml/document'

class PersistencesTestSqlserver < ActiveRecord::TestCase
class PersistenceTestSqlserver < ActiveRecord::TestCase
end

class PersistencesTest < ActiveRecord::TestCase
class PersistenceTest < ActiveRecord::TestCase

fixtures :topics, :companies, :developers, :projects, :computers, :accounts, :minimalistics, 'warehouse-things', :authors, :categorizations, :categories, :posts, :minivans

Expand Down
20 changes: 20 additions & 0 deletions test/cases/predicate_builder_test_sqlserver.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require 'cases/sqlserver_helper'
require 'models/topic'
require 'models_sqlserver/topic'

module ActiveRecord
class PredicateBuilderTest < ActiveRecord::TestCase

COERCED_TESTS = [:test_registering_new_handlers]

include SqlserverCoercedTest

def test_coerced_registering_new_handlers
ActiveRecord::PredicateBuilder.register_handler(Regexp, proc do |column, value|
Arel::Nodes::InfixOperation.new('~', column, value.source)
end)

assert_match %r{\[topics\].\[title\] ~ N'rails'}i, Topic.where(title: /rails/).to_sql
end
end
end
15 changes: 13 additions & 2 deletions test/cases/relations_test_sqlserver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@


class RelationTest < ActiveRecord::TestCase
COERCED_TESTS = [:test_merging_reorders_bind_params]
COERCED_TESTS = [
:test_merging_reorders_bind_params,
:test_to_sql_on_eager_join
]
# Until that patch is made to rails we are preventing this test from running in this gem.
include SqlserverCoercedTest
fixtures :posts
Expand All @@ -24,4 +27,12 @@ def test_coerced_merging_reorders_bind_params
merged = left.merge(right)
assert_equal post, merged.first
end
end

def test_coerced_to_sql_on_eager_join
expected = assert_sql {
Post.eager_load(:last_comment).order('comments.id DESC').to_a
}.first
actual = Post.eager_load(:last_comment).order('comments.id DESC').to_sql
assert_equal expected.include?(actual), true
end
end
22 changes: 11 additions & 11 deletions test/cases/resolver_test_sqlserver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,27 @@ class ResolverTest < ActiveRecord::TestCase
def test_coerced_test_url_host_no_db
spec = resolve 'sqlserver://foo?encoding=utf8'
assert_equal({
adapter: "sqlserver",
host: "foo",
encoding: "utf8" }, spec)
"adapter" => "sqlserver",
"host" => "foo",
"encoding" => "utf8" }, spec)
end

def test_coerced_test_url_host_db
spec = resolve 'sqlserver://foo/bar?encoding=utf8'
assert_equal({
adapter: "sqlserver",
database: "bar",
host: "foo",
encoding: "utf8" }, spec)
"adapter" => "sqlserver",
"database" => "bar",
"host" => "foo",
"encoding" => "utf8" }, spec)
end

def test_coerced_test_url_port
spec = resolve 'sqlserver://foo:123?encoding=utf8'
assert_equal({
adapter: "sqlserver",
port: 123,
host: "foo",
encoding: "utf8" }, spec)
"adapter" => "sqlserver",
"port" => 123,
"host" => "foo",
"encoding" => "utf8" }, spec)
end
end

Expand Down
11 changes: 2 additions & 9 deletions test/cases/schema_dumper_test_sqlserver.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
require 'cases/sqlserver_helper'
require 'cases/schema_dumper_test'
require 'stringio'


class SchemaDumperTestSqlserver < ActiveRecord::TestCase

setup :find_all_tables
Expand Down Expand Up @@ -96,15 +98,6 @@ def test_coerced_types_line_up
end
end

private

def standard_dump
stream = StringIO.new
ActiveRecord::SchemaDumper.ignore_tables = []
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream)
stream.string
end

end


1 change: 1 addition & 0 deletions test/cases/sqlserver_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
require 'active_record/version'
require 'active_record/connection_adapters/abstract_adapter'
require 'minitest-spec-rails'
require 'minitest-spec-rails/init/active_support'
require 'minitest-spec-rails/init/mini_shoulda'
require 'cases/helper'
require 'models/topic'
Expand Down
2 changes: 1 addition & 1 deletion test/cases/sqlserver_test_case.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require 'active_record/test_case.rb'
require 'cases/test_case.rb'

# TODO: I'm struggling to figure out how to unsubscribe from only one 'sql.active_record'
# This is a temporary hack until we can just get the sqlserver_ignored regex in rails
Expand Down
Loading