Skip to content

Commit eb0dea1

Browse files
committed
Include guid and default function support from rails 2-3-stable branch.
1 parent f3439ee commit eb0dea1

File tree

6 files changed

+72
-2
lines changed

6 files changed

+72
-2
lines changed

Diff for: CHANGELOG

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11

22
MASTER
33

4+
* 3.0.0
5+
6+
* Release rails 3 version!
7+
8+
49
* 2.3.8
510

611
* Properly quote all database names in rake helper methods. [Ken Collins]
@@ -15,7 +20,7 @@ MASTER
1520

1621
* Allow DNS's to not contain a database and use what is in database.yml [Marco Mastrodonato]
1722

18-
* Rake tasks methods for vanallia rails :db namespace parity. [Ken Collins]
23+
* Rake tasks methods for vanilla rails :db namespace parity. [Ken Collins]
1924

2025
* IronRuby integrated security fixes [Jimmy Schementi]
2126

@@ -40,7 +45,7 @@ MASTER
4045

4146
* Qualify INFORMATION_SCHEMA.COLUMNS with a correct period DB name if present.
4247

43-
* Allow adapter to return multipe results sets, for example from stored procedures. [Chris Hall]
48+
* Allow adapter to return multiple results sets, for example from stored procedures. [Chris Hall]
4449

4550

4651
* 2.3.4

Diff for: lib/active_record/connection_adapters/sqlserver/database_statements.rb

+8
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,14 @@ def run_with_isolation_level(isolation_level)
115115
end if block_given?
116116
end
117117

118+
def newid_function
119+
select_value "SELECT NEWID()"
120+
end
121+
122+
def newsequentialid_function
123+
select_value "SELECT NEWSEQUENTIALID()"
124+
end
125+
118126
# === SQLServer Specific (Rake/Test Helpers) ==================== #
119127

120128
def recreate_database

Diff for: lib/active_record/connection_adapters/sqlserver/schema_statements.rb

+3
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,9 @@ def column_definitions(table_name)
217217
ci[:default_value] = case ci[:default_value]
218218
when nil, '(null)', '(NULL)'
219219
nil
220+
when /\A\((\w+\(\))\)\Z/
221+
ci[:default_function] = $1
222+
nil
220223
else
221224
match_data = ci[:default_value].match(/\A\(+N?'?(.*?)'?\)+\Z/m)
222225
match_data ? match_data[1] : nil

Diff for: lib/active_record/connection_adapters/sqlserver_adapter.rb

+4
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ def is_utf8?
105105
sql_type =~ /nvarchar|ntext|nchar/i
106106
end
107107

108+
def default_function
109+
@sqlserver_options[:default_function]
110+
end
111+
108112
def table_name
109113
@sqlserver_options[:table_name]
110114
end

Diff for: test/cases/specific_schema_test_sqlserver.rb

+46
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
class StringDefault < ActiveRecord::Base; end;
44
class SqlServerEdgeSchema < ActiveRecord::Base; end;
5+
class SqlServerEdgeSchema < ActiveRecord::Base
6+
attr_accessor :new_id_setting
7+
before_create :set_new_id
8+
protected
9+
def set_new_id
10+
self[:guid_newid] ||= connection.newid_function if new_id_setting
11+
end
12+
end
513

614
class SpecificSchemaTestSqlserver < ActiveRecord::TestCase
715

@@ -91,7 +99,45 @@ class SpecificSchemaTestSqlserver < ActiveRecord::TestCase
9199

92100
end
93101

102+
context 'with uniqueidentifier column' do
103+
104+
setup do
105+
@newid = ActiveRecord::Base.connection.newid_function
106+
assert_guid @newid
107+
end
108+
109+
should 'allow a simple insert and read of a column without a default function' do
110+
obj = @edge_class.create! :guid => @newid
111+
assert_equal @newid, @edge_class.find(obj.id).guid
112+
end
113+
114+
should 'record the default function name in the column definition but still show a nil real default, will use one day for insert/update' do
115+
newid_column = @edge_class.columns_hash['guid_newid']
116+
assert newid_column.default_function.present?
117+
assert_nil newid_column.default
118+
assert_equal 'newid()', newid_column.default_function
119+
newseqid_column = @edge_class.columns_hash['guid_newseqid']
120+
assert newseqid_column.default_function.present?
121+
assert_nil newseqid_column.default
122+
assert_equal 'newsequentialid()', newseqid_column.default_function
123+
end
124+
125+
should 'use model callback to set get a new guid' do
126+
obj = @edge_class.new
127+
obj.new_id_setting = true
128+
obj.save!
129+
assert_guid obj.guid_newid
130+
end
131+
132+
end
133+
94134
end
95135

96136

137+
protected
138+
139+
def assert_guid(guid)
140+
assert_match %r|\w{8}-\w{4}-\w{4}-\w{4}-\w{12}|, guid
141+
end
142+
97143
end

Diff for: test/schema/sqlserver_specific_schema.rb

+4
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,12 @@
6363
t.string :description
6464
t.column :bigint, :bigint
6565
t.column :tinyint, :tinyint
66+
t.column :guid, :uniqueidentifier
6667
end
6768

69+
execute %|ALTER TABLE [sql_server_edge_schemas] ADD [guid_newid] uniqueidentifier DEFAULT NEWID();|
70+
execute %|ALTER TABLE [sql_server_edge_schemas] ADD [guid_newseqid] uniqueidentifier DEFAULT NEWSEQUENTIALID();|
71+
6872
execute "IF EXISTS (SELECT TABLE_NAME FROM INFORMATION_SCHEMA.VIEWS WHERE TABLE_NAME = 'customers_view') DROP VIEW customers_view"
6973
execute <<-CUSTOMERSVIEW
7074
CREATE VIEW customers_view AS

0 commit comments

Comments
 (0)