Skip to content

Commit ac180b6

Browse files
committed
Works under Rails v4.0.0
1 parent e95d1f4 commit ac180b6

File tree

4 files changed

+25
-32
lines changed

4 files changed

+25
-32
lines changed

activerecord-sqlserver-adapter.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ Gem::Specification.new do |s|
1616
s.require_path = 'lib'
1717
s.rubyforge_project = 'activerecord-sqlserver-adapter'
1818

19-
s.add_dependency('activerecord', '~> 3.2.0')
19+
s.add_dependency('activerecord', '4')
2020
end
2121

lib/active_record/connection_adapters/sqlserver/core_ext/database_statements.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,15 @@ def transaction_with_retry_deadlock_victim(options = {})
4444
if open_transactions == 0
4545
if database_transaction_rollback.is_a?(::ActiveRecord::DeadlockVictim)
4646
# SQL Server has already rolled back, so rollback activerecord's history
47-
rollback_transaction_records(true)
47+
rollback_transaction
4848
retry
4949
else
5050
rollback_db_transaction
5151
rollback_transaction_records(true)
5252
end
5353
else
5454
rollback_to_savepoint
55-
rollback_transaction_records(false)
55+
rollback_transaction
5656
end
5757
end
5858
raise unless database_transaction_rollback.is_a?(::ActiveRecord::Rollback)
@@ -67,7 +67,7 @@ def transaction_with_retry_deadlock_victim(options = {})
6767
begin
6868
if open_transactions == 0
6969
commit_db_transaction
70-
commit_transaction_records
70+
# commit_transaction_records
7171
else
7272
release_savepoint
7373
save_point_records = @_current_transaction_records.pop

lib/active_record/connection_adapters/sqlserver/database_statements.rb

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,16 @@ def execute(sql, name = nil)
1818
end
1919

2020
def exec_query(sql, name = 'SQL', binds = [], sqlserver_options = {})
21+
# This is so no id is tried to be updated
22+
sql.gsub! /, \[id\] = @[0-9]*/, '' if sql =~ /UPDATE/ && sql =~ /, \[id\] = /
2123
if id_insert_table_name = sqlserver_options[:insert] ? query_requires_identity_insert?(sql) : nil
2224
with_identity_insert_enabled(id_insert_table_name) { do_exec_query(sql, name, binds) }
2325
else
2426
do_exec_query(sql, name, binds)
2527
end
2628
end
2729

28-
def exec_insert(sql, name, binds)
30+
def exec_insert(sql, name, binds, pk = nil, sequence_name = nil)
2931
exec_query sql, name, binds, :insert => true
3032
end
3133

@@ -135,9 +137,11 @@ def use_database(database=nil)
135137

136138
def user_options
137139
return {} if sqlserver_azure?
138-
select_rows("dbcc useroptions",'SCHEMA').inject(HashWithIndifferentAccess.new) do |values,row|
139-
set_option = row[0].gsub(/\s+/,'_')
140-
user_value = row[1]
140+
select_rows("dbcc useroptions",'SCHEMA').inject(HashWithIndifferentAccess.new) do |values,row|
141+
set_option = row.values[0].gsub(/\s+/,'_') if row.instance_of? Hash
142+
set_option = row[0].gsub(/\s+/,'_') if row.instance_of? Array
143+
user_value = row.values[1] if row.instance_of? Hash
144+
user_value = row[1] if row.instance_of? Array
141145
values[set_option] = user_value
142146
values
143147
end
@@ -294,7 +298,7 @@ def charset
294298
protected
295299

296300
def select(sql, name = nil, binds = [])
297-
exec_query(sql, name, binds).to_a
301+
exec_query(sql, name, binds)
298302
end
299303

300304
def sql_for_insert(sql, pk, id_value, sequence_name, binds)

lib/active_record/connection_adapters/sqlserver/schema_statements.rb

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -47,39 +47,28 @@ def columns(table_name, name = nil)
4747
def rename_table(table_name, new_name)
4848
do_execute "EXEC sp_rename '#{table_name}', '#{new_name}'"
4949
end
50-
51-
def remove_column(table_name, *column_names)
52-
raise ArgumentError.new("You must specify at least one column name. Example: remove_column(:people, :first_name)") if column_names.empty?
53-
ActiveSupport::Deprecation.warn 'Passing array to remove_columns is deprecated, please use multiple arguments, like: `remove_columns(:posts, :foo, :bar)`', caller if column_names.flatten!
54-
column_names.flatten.each do |column_name|
55-
remove_check_constraints(table_name, column_name)
56-
remove_default_constraint(table_name, column_name)
57-
remove_indexes(table_name, column_name)
58-
do_execute "ALTER TABLE #{quote_table_name(table_name)} DROP COLUMN #{quote_column_name(column_name)}"
59-
end
50+
51+
def remove_column(table_name, column_name, type = nil)
52+
raise ArgumentError.new("You must specify at least one column name. Example: remove_column(:people, :first_name)") if (column_name.is_a? Array)
53+
ActiveSupport::Deprecation.warn 'Passing multiple arguments to remove_columns is deprecated, please use just one column name, like: `remove_columns(:posts, :column_name, :type)`', caller if column_name
54+
remove_check_constraints(table_name, column_name)
55+
remove_default_constraint(table_name, column_name)
56+
remove_indexes(table_name, column_name)
57+
do_execute "ALTER TABLE #{quote_table_name(table_name)} DROP COLUMN #{quote_column_name(column_name)}"
6058
end
6159

6260
def change_column(table_name, column_name, type, options = {})
6361
sql_commands = []
64-
indexes = []
6562
column_object = schema_cache.columns[table_name].detect { |c| c.name.to_s == column_name.to_s }
66-
63+
change_column_sql = "ALTER TABLE #{quote_table_name(table_name)} ALTER COLUMN #{quote_column_name(column_name)} #{type_to_sql(type, options[:limit], options[:precision], options[:scale])}"
64+
change_column_sql << " NOT NULL" if options[:null] == false
65+
sql_commands << change_column_sql
6766
if options_include_default?(options) || (column_object && column_object.type != type.to_sym)
68-
remove_default_constraint(table_name,column_name)
69-
indexes = indexes(table_name).select{ |index| index.columns.include?(column_name.to_s) }
70-
remove_indexes(table_name, column_name)
67+
remove_default_constraint(table_name,column_name)
7168
end
72-
sql_commands << "UPDATE #{quote_table_name(table_name)} SET #{quote_column_name(column_name)}=#{quote(options[:default])} WHERE #{quote_column_name(column_name)} IS NULL" if !options[:null].nil? && options[:null] == false && !options[:default].nil?
73-
sql_commands << "ALTER TABLE #{quote_table_name(table_name)} ALTER COLUMN #{quote_column_name(column_name)} #{type_to_sql(type, options[:limit], options[:precision], options[:scale])}"
74-
sql_commands[-1] << " NOT NULL" if !options[:null].nil? && options[:null] == false
7569
if options_include_default?(options)
7670
sql_commands << "ALTER TABLE #{quote_table_name(table_name)} ADD CONSTRAINT #{default_constraint_name(table_name,column_name)} DEFAULT #{quote(options[:default])} FOR #{quote_column_name(column_name)}"
7771
end
78-
79-
#Add any removed indexes back
80-
indexes.each do |index|
81-
sql_commands << "CREATE INDEX #{quote_table_name(index.name)} ON #{quote_table_name(table_name)} (#{index.columns.collect {|c|quote_column_name(c)}.join(', ')})"
82-
end
8372
sql_commands.each { |c| do_execute(c) }
8473
end
8574

0 commit comments

Comments
 (0)