Skip to content

Commit 58be119

Browse files
committed
Rails2.3 - Back passing tests on 2.2 work. Includes: (1) Created new test helpers that check ActiveRecord version strings so we can conditionally run 2.2 and 2.3 tests. (2) Making TransactionTestSqlserver use Ship vs Bird model. Also made it conditional run a few blocks for different versions of ActiveRecord. (3) Previous JoinDependency#aliased_table_name_for is now only patched in ActiveRecord equal or greater than 2.3.
1 parent 2b348d5 commit 58be119

File tree

5 files changed

+80
-66
lines changed

5 files changed

+80
-66
lines changed

CHANGELOG

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11

22
MASTER
33

4+
* Rails2.3 - Back passing tests on 2.2 work. Includes: (1) Created new test helpers that check ActiveRecord
5+
version strings so we can conditionally run 2.2 and 2.3 tests. (2) Making TransactionTestSqlserver use Ship vs
6+
Bird model. Also made it conditional run a few blocks for different versions of ActiveRecord. (3) Previous
7+
JoinDependency#aliased_table_name_for is now only patched in ActiveRecord equal or greater than 2.3. [Ken Collins]
8+
49
* Rails2.3 - Implement new savepoint support [Ken Collins]
510
http://rails.lighthouseapp.com/projects/8994/tickets/383
611
http://www.codeproject.com/KB/database/sqlservertransactions.aspx

lib/core_ext/active_record.rb

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require 'active_record/version'
2+
13
module ActiveRecord
24
module ConnectionAdapters
35
module SQLServerActiveRecordExtensions
@@ -85,45 +87,47 @@ def add_order_with_sqlserver_unique_checking!(sql, order, scope = :auto)
8587

8688

8789

88-
require 'active_record/associations'
90+
if ActiveRecord::VERSION::MAJOR == 2 && ActiveRecord::VERSION::MINOR >= 3
8991

90-
module ActiveRecord
91-
module ConnectionAdapters
92-
module SQLServerJoinAssociationChanges
93-
94-
def self.included(klass)
95-
klass.class_eval do
96-
include InstanceMethods
97-
alias_method_chain :aliased_table_name_for, :sqlserver_support
98-
end
99-
end
100-
101-
module InstanceMethods
102-
103-
protected
92+
require 'active_record/associations'
93+
module ActiveRecord
94+
module ConnectionAdapters
95+
module SQLServerJoinAssociationChanges
10496

105-
# An exact copy, except this method has a Regexp escape on the quoted table name.
106-
def aliased_table_name_for_with_sqlserver_support(name,suffix=nil)
107-
if !parent.table_joins.blank? && parent.table_joins.to_s.downcase =~ %r{join(\s+\w+)?\s+#{Regexp.escape(active_record.connection.quote_table_name(name.downcase))}\son}i
108-
@join_dependency.table_aliases[name] += 1
97+
def self.included(klass)
98+
klass.class_eval do
99+
include InstanceMethods
100+
alias_method_chain :aliased_table_name_for, :sqlserver_support
109101
end
110-
unless @join_dependency.table_aliases[name].zero?
111-
# if the table name has been used, then use an alias
112-
name = active_record.connection.table_alias_for "#{pluralize(reflection.name)}_#{parent_table_name}#{suffix}"
113-
table_index = @join_dependency.table_aliases[name]
114-
@join_dependency.table_aliases[name] += 1
115-
name = name[0..active_record.connection.table_alias_length-3] + "_#{table_index+1}" if table_index > 0
116-
else
117-
@join_dependency.table_aliases[name] += 1
102+
end
103+
104+
module InstanceMethods
105+
106+
protected
107+
108+
# An exact copy, except this method has a Regexp escape on the quoted table name.
109+
def aliased_table_name_for_with_sqlserver_support(name,suffix=nil)
110+
if !parent.table_joins.blank? && parent.table_joins.to_s.downcase =~ %r{join(\s+\w+)?\s+#{Regexp.escape(active_record.connection.quote_table_name(name.downcase))}\son}i
111+
@join_dependency.table_aliases[name] += 1
112+
end
113+
unless @join_dependency.table_aliases[name].zero?
114+
# if the table name has been used, then use an alias
115+
name = active_record.connection.table_alias_for "#{pluralize(reflection.name)}_#{parent_table_name}#{suffix}"
116+
table_index = @join_dependency.table_aliases[name]
117+
@join_dependency.table_aliases[name] += 1
118+
name = name[0..active_record.connection.table_alias_length-3] + "_#{table_index+1}" if table_index > 0
119+
else
120+
@join_dependency.table_aliases[name] += 1
121+
end
122+
name
118123
end
119-
name
124+
120125
end
121126

122127
end
123-
124128
end
125129
end
126-
end
130+
ActiveRecord::Associations::ClassMethods::JoinDependency::JoinAssociation.send :include, ActiveRecord::ConnectionAdapters::SQLServerJoinAssociationChanges
127131

128-
ActiveRecord::Associations::ClassMethods::JoinDependency::JoinAssociation.send :include, ActiveRecord::ConnectionAdapters::SQLServerJoinAssociationChanges
132+
end
129133

test/cases/eager_association_test_sqlserver.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def test_coerced_eager_with_has_many_and_limit_and_high_offset_and_multiple_hash
3636
:conditions => { 'authors.name' => 'David', 'comments.body' => 'go crazy' })
3737
assert_equal 0, posts.size
3838
end
39-
end
39+
end unless active_record_2_point_2?
4040

4141

4242
end

test/cases/sqlserver_helper.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require 'mocha'
44
require 'cases/helper'
55
require 'models/topic'
6+
require 'active_record/version'
67

78
SQLSERVER_TEST_ROOT = File.expand_path(File.join(File.dirname(__FILE__),'..'))
89
SQLSERVER_ASSETS_ROOT = SQLSERVER_TEST_ROOT + "/assets"
@@ -94,6 +95,8 @@ class TestCase < ActiveSupport::TestCase
9495
class << self
9596
def sqlserver_2000? ; ActiveRecord::Base.connection.sqlserver_2000? ; end
9697
def sqlserver_2005? ; ActiveRecord::Base.connection.sqlserver_2005? ; end
98+
def active_record_2_point_2? ; ActiveRecord::VERSION::MAJOR == 2 && ActiveRecord::VERSION::MINOR == 2 ; end
99+
def active_record_2_point_3? ; ActiveRecord::VERSION::MAJOR == 2 && ActiveRecord::VERSION::MINOR == 3 ; end
97100
end
98101
def assert_sql(*patterns_to_match)
99102
$queries_executed = []
@@ -107,6 +110,8 @@ def assert_sql(*patterns_to_match)
107110
end
108111
def sqlserver_2000? ; self.class.sqlserver_2000? ; end
109112
def sqlserver_2005? ; self.class.sqlserver_2005? ; end
113+
def active_record_2_point_2? ; self.class.active_record_2_point_2? ; end
114+
def active_record_2_point_3? ; self.class.active_record_2_point_3? ; end
110115
end
111116
end
112117

test/cases/transaction_test_sqlserver.rb

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,34 @@
11
require 'cases/sqlserver_helper'
2-
require 'models/bird'
2+
require 'models/ship'
33
require 'models/developer'
44

55
class TransactionTestSqlserver < ActiveRecord::TestCase
66

77
self.use_transactional_fixtures = false
88

9-
setup :delete_birds
9+
setup :delete_ships
1010

1111
context 'Testing transaction basics' do
1212

1313
should 'allow ActiveRecord::Rollback to work in 1 transaction block' do
14-
Bird.transaction do
15-
Bird.create! :name => 'Crow', :pirate_id => 1
14+
Ship.transaction do
15+
Ship.create! :name => 'Black Pearl'
1616
raise ActiveRecord::Rollback
1717
end
18-
assert_no_birds
18+
assert_no_ships
1919
end
2020

2121
should 'allow nested transactions to totally rollback' do
2222
begin
23-
Bird.transaction do
24-
Bird.create! :name => 'Crow', :pirate_id => 1
25-
Bird.transaction do
26-
Bird.create! :name => 'Dog', :pirate_id => 1
23+
Ship.transaction do
24+
Ship.create! :name => 'Black Pearl'
25+
Ship.transaction do
26+
Ship.create! :name => 'Flying Dutchman'
2727
raise 'HELL'
2828
end
2929
end
3030
rescue Exception => e
31-
assert_no_birds
31+
assert_no_ships
3232
end
3333
end
3434

@@ -37,56 +37,56 @@ class TransactionTestSqlserver < ActiveRecord::TestCase
3737
context 'Testing #outside_transaction?' do
3838

3939
should 'work in simple usage' do
40-
assert Bird.connection.outside_transaction?
41-
Bird.connection.begin_db_transaction
42-
assert !Bird.connection.outside_transaction?
43-
Bird.connection.rollback_db_transaction
44-
assert Bird.connection.outside_transaction?
40+
assert Ship.connection.outside_transaction?
41+
Ship.connection.begin_db_transaction
42+
assert !Ship.connection.outside_transaction?
43+
Ship.connection.rollback_db_transaction
44+
assert Ship.connection.outside_transaction?
4545
end
4646

4747
should 'work inside nested transactions' do
48-
assert Bird.connection.outside_transaction?
49-
Bird.transaction do
50-
assert !Bird.connection.outside_transaction?
51-
Bird.transaction do
52-
assert !Bird.connection.outside_transaction?
48+
assert Ship.connection.outside_transaction?
49+
Ship.transaction do
50+
assert !Ship.connection.outside_transaction?
51+
Ship.transaction do
52+
assert !Ship.connection.outside_transaction?
5353
end
5454
end
55-
assert Bird.connection.outside_transaction?
55+
assert Ship.connection.outside_transaction?
5656
end
5757

5858
should 'not call rollback if no transaction is active' do
5959
assert_raise RuntimeError do
60-
Bird.transaction do
61-
Bird.connection.rollback_db_transaction
62-
Bird.connection.expects(:rollback_db_transaction).never
60+
Ship.transaction do
61+
Ship.connection.rollback_db_transaction
62+
Ship.connection.expects(:rollback_db_transaction).never
6363
raise "Rails doesn't scale!"
6464
end
6565
end
6666
end
6767

6868
should 'test_open_transactions_count_is_reset_to_zero_if_no_transaction_active' do
69-
Bird.transaction do
70-
Bird.transaction do
71-
Bird.connection.rollback_db_transaction
69+
Ship.transaction do
70+
Ship.transaction do
71+
Ship.connection.rollback_db_transaction
7272
end
73-
assert_equal 0, Bird.connection.open_transactions
73+
assert_equal 0, Ship.connection.open_transactions
7474
end
75-
assert_equal 0, Bird.connection.open_transactions
75+
assert_equal 0, Ship.connection.open_transactions
7676
end
7777

78-
end
78+
end unless active_record_2_point_2?
7979

8080

8181

8282
protected
8383

84-
def delete_birds
85-
Bird.delete_all
84+
def delete_ships
85+
Ship.delete_all
8686
end
8787

88-
def assert_no_birds
89-
assert Bird.count.zero?, "Expected Bird to have no models but it did have:\n#{Bird.all.inspect}"
88+
def assert_no_ships
89+
assert Ship.count.zero?, "Expected Ship to have no models but it did have:\n#{Ship.all.inspect}"
9090
end
9191

9292
end

0 commit comments

Comments
 (0)