Skip to content

Commit 37e34a3

Browse files
committed
Update database version methods with tests. Also add mocha.
1 parent 67f4124 commit 37e34a3

File tree

3 files changed

+60
-22
lines changed

3 files changed

+60
-22
lines changed

lib/active_record/connection_adapters/sqlserver_adapter.rb

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -268,16 +268,15 @@ def new_time(year, mon, mday, hour, min, sec, microsec = 0)
268268
# [Linux strongmad 2.6.11-1.1369_FC4 #1 Thu Jun 2 22:55:56 EDT 2005 i686 i686 i386 GNU/Linux]
269269
class SQLServerAdapter < AbstractAdapter
270270

271-
ADAPTER_NAME = 'SQLServer'.freeze
271+
ADAPTER_NAME = 'SQLServer'.freeze
272+
DATABASE_VERSION_REGEXP = /Microsoft SQL Server\s+(\d{4})/
273+
SUPPORTED_VERSIONS = [2000,2005].freeze
272274

273275
def initialize(connection, logger, connection_options=nil)
274276
super(connection, logger)
275277
@connection_options = connection_options
276-
if database_version =~ /(2000|2005) - (\d+)\./
277-
@database_version_year = $1.to_i
278-
@database_version_major = $2.to_i
279-
else
280-
raise "Currently, only 2000 and 2005 are supported versions"
278+
unless SUPPORTED_VERSIONS.include?(database_year)
279+
raise NotImplementedError, "Currently, only #{SUPPORTED_VERSIONS.to_sentence} are supported."
281280
end
282281
end
283282

@@ -295,16 +294,32 @@ def supports_ddl_transactions?
295294
true
296295
end
297296

297+
def database_version
298+
select_value "SELECT @@version"
299+
end
298300

301+
def database_year
302+
DATABASE_VERSION_REGEXP.match(database_version)[1].to_i
303+
end
299304

305+
def sqlserver_2000?
306+
database_year == 2000
307+
end
308+
309+
def sqlserver_2005?
310+
database_year == 2005
311+
end
312+
313+
# QUOTING ==================================================#
314+
315+
316+
317+
318+
# SCHEMA STATEMENTS ========================================#
300319

301-
302320
def native_database_types
303-
# support for varchar(max) and varbinary(max) for text and binary cols if our version is 9 (2005)
304-
txt = @database_version_major >= 9 ? "varchar(max)" : "text"
305-
306-
# TODO: Need to verify image column works correctly with 2000 if string_to_binary stores a hex string
307-
bin = @database_version_major >= 9 ? "varbinary(max)" : "image"
321+
txt = sqlserver_2005? ? "varchar(max)" : "text"
322+
bin = sqlserver_2005? ? "varbinary(max)" : "image"
308323
{
309324
:primary_key => "int NOT NULL IDENTITY(1, 1) PRIMARY KEY",
310325
:string => { :name => "varchar", :limit => 255 },
@@ -321,12 +336,7 @@ def native_database_types
321336
}
322337
end
323338

324-
def database_version
325-
# returns string such as:
326-
# "Microsoft SQL Server 2000 - 8.00.2039 (Intel X86) \n\tMay 3 2005 23:18:38 \n\tCopyright (c) 1988-2003 Microsoft Corporation\n\tEnterprise Edition on Windows NT 5.2 (Build 3790: )\n"
327-
# "Microsoft SQL Server 2005 - 9.00.3215.00 (Intel X86) \n\tDec 8 2007 18:51:32 \n\tCopyright (c) 1988-2005 Microsoft Corporation\n\tStandard Edition on Windows NT 5.2 (Build 3790: Service Pack 2)\n"
328-
select_value("SELECT @@version")
329-
end
339+
330340

331341

332342

test/cases/adapter_test_sqlserver.rb

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ def setup
88
@connection = ActiveRecord::Base.connection
99
end
1010

11-
1211
context 'For abstract behavior' do
1312

1413
should 'be our adapter_name' do
@@ -23,6 +22,36 @@ def setup
2322
assert @connection.supports_ddl_transactions?
2423
end
2524

25+
context 'for database version' do
26+
27+
setup do
28+
@version_regexp = ActiveRecord::ConnectionAdapters::SQLServerAdapter::DATABASE_VERSION_REGEXP
29+
@supported_version = ActiveRecord::ConnectionAdapters::SQLServerAdapter::SUPPORTED_VERSIONS
30+
@sqlserver_2000_string = "Microsoft SQL Server 2000 - 8.00.2039 (Intel X86)"
31+
@sqlserver_2005_string = "Microsoft SQL Server 2005 - 9.00.3215.00 (Intel X86)"
32+
end
33+
34+
should 'return a string from #database_version that matches class regexp' do
35+
assert_match @version_regexp, @connection.database_version
36+
end
37+
38+
should 'return a 4 digit year fixnum for #database_year' do
39+
assert_instance_of Fixnum, @connection.database_year
40+
assert_contains @supported_version, @connection.database_year
41+
end
42+
43+
should 'return true to #sqlserver_2000?' do
44+
@connection.stubs(:database_version).returns(@sqlserver_2000_string)
45+
assert @connection.sqlserver_2000?
46+
end
47+
48+
should 'return true to #sqlserver_2005?' do
49+
@connection.stubs(:database_version).returns(@sqlserver_2005_string)
50+
assert @connection.sqlserver_2005?
51+
end
52+
53+
end
54+
2655
end
2756

2857
context 'For Quoting' do
@@ -38,9 +67,7 @@ def setup
3867
end
3968

4069

41-
42-
43-
70+
4471
should 'raise invalid statement error' do
4572
assert_raise(ActiveRecord::StatementInvalid) { Topic.connection.update_sql("UPDATE XXX") }
4673
end

test/cases/sqlserver_helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
require 'rubygems'
22
require 'shoulda'
3+
require 'mocha'
34
require 'cases/helper'
45

56
SQLSERVER_TEST_ROOT = File.expand_path(File.join('..',File.dirname(__FILE__)))

0 commit comments

Comments
 (0)