Skip to content

Commit 38bbb6c

Browse files
adzaph-lame
authored andcommitted
Allowing SQL Server to dump non-standard primary keys via the schema dumper by implementing pk_and_sequence_for.
Note that this code originally came from http://github.com/adzap/sqlserver_adapter_mods
1 parent f7a5af6 commit 38bbb6c

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

lib/active_record/connection_adapters/sqlserver_adapter.rb

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ def add_lock!(sql, options)
668668
end
669669
end
670670

671-
def recreate_database(name)
671+
def recreate_database(name)
672672
# Switch to another database or we'll receive a "Database in use" error message.
673673
existing_database = current_database.to_s
674674
if name.to_s == existing_database
@@ -817,6 +817,21 @@ def remove_index(table_name, options = {})
817817
execute "DROP INDEX #{table_name}.#{quote_column_name(index_name(table_name, options))}"
818818
end
819819

820+
# Returns a table's primary key and belonging sequence (not applicable to SQL server).
821+
def pk_and_sequence_for(table_name)
822+
@connection["AutoCommit"] = false
823+
keys = []
824+
execute("EXEC sp_helpindex '#{table_name}'") do |handle|
825+
if handle.column_info.any?
826+
pk_index = handle.detect {|index| index[1] =~ /primary key/ }
827+
keys << pk_index[2] if pk_index
828+
end
829+
end
830+
keys.length == 1 ? [keys.first, nil] : nil
831+
ensure
832+
@connection["AutoCommit"] = true
833+
end
834+
820835
private
821836
def __indexes(table_name, name = nil)
822837
indexes = []

0 commit comments

Comments
 (0)