Two Problems when doing 'rake db:schema:dump'
Environment: JRuby 0.9, ActiveRecord 0.3.1, JTDS 1.2, SQL Server 2000
----------------------------------------------------------------------------
Problem 1 - noMethodError for method 'indexes' jdbc_mssql.rb, rake complete but in the generated schema.rb, you get
the following for all tables:
ActiveRecord::Schema.define() do
# Could not dump table "country" because of following NoMethodError
# undefined or inaccessible method `indexes' for #<ActiveRecord::ConnectionAdapters::JdbcAdapter:0x104bce3
@connection=#<ActiveRecord::ConnectionAdapters::JdbcConnection:0x176484e
@connection=#<Java::NetSourceforgeJtdsJdbc::ConnectionJDBC3:0x84fdbc
@java_object=net.sourceforge.jtds.jdbc.ConnectionJDBC3@e59e40>,
@adapter=#<ActiveRecord::ConnectionAdapters::JdbcAdapter:0x104bce3 ...>,...
Fix for Problem 1: add the following to jdbc_mssql.rb
-----------------------------------------------------------------------------
#### copy from mysql ###
def indexes(table_name, name = nil)#:nodoc:
r = @connection.indexes(table_name.upcase)
end
-----------------------------------------------------------------------------
Problem 2 - noMethodError for nil.downcase, rake complete but you get a different error in schema.rb,
ActiveRecord::Schema.define() do
# Could not dump table "country" because of following NoMethodError
# You have a nil object when you didn't expect it!
The error occurred while evaluating nil.downcase
------------------------------------------------------------------------------
Fix for Problem 2: For JTDS, the first result in the resultset returned from metadata.getIndexInfo does not contain
the normal index info (INDEX_NAME is null).
Making the following modification to jdbc_adapter.rb fixes the problem:
def indexes(table_name, name = nil)
metadata = @connection.getMetaData
unless String === table_name
table_name = table_name.to_s
else
table_name = table_name.dup
end
table_name.upcase! if metadata.storesUpperCaseIdentifiers
table_name.downcase! if metadata.storesLowerCaseIdentifiers
resultset = metadata.getIndexInfo(nil, nil, table_name, false, false)
primary_keys = primary_keys(table_name)
indexes = []
current_index = nil
while resultset.next
+ # index_name = resultset.get_string(Jdbc::IndexMetaData::INDEX_NAME).downcase
+ # The first resultset seems to contain information other than that of a index's column. INDEX_NAME is null.
+ # And you get a nil.downcase problem
+ index_name = resultset.get_string(Jdbc::IndexMetaData::INDEX_NAME)
+ # just ignore if index_name is null
+ next if !index_name
+ index_name.downcase!
column_name = resultset.get_string(Jdbc::IndexMetaData::COLUMN_NAME).downcase
|