Skip to content

Commit

Permalink
Use show index from. We could fix pk_and_sequence_for method's pe…
Browse files Browse the repository at this point in the history
…rformance problem (GH rails#3678)
  • Loading branch information
kennyj committed Nov 18, 2011
1 parent 396ef44 commit f9b9179
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -576,24 +576,8 @@ def show_variable(name)

# Returns a table's primary key and belonging sequence.
def pk_and_sequence_for(table)
execute_and_free("DESCRIBE #{quote_table_name(table)}", 'SCHEMA') do |result|
keys = each_hash(result).select { |row| row[:Key] == 'PRI' }.map { |row| row[:Field] }
keys.length == 1 ? [keys.first, nil] : nil
end
end

def detailed_pk_and_sequence_for(table)
sql = <<-SQL
SELECT t.constraint_type, k.column_name
FROM information_schema.table_constraints t
JOIN information_schema.key_column_usage k
USING (constraint_name, table_schema, table_name)
WHERE t.table_schema = DATABASE()
AND t.table_name = '#{table}'
SQL

execute_and_free(sql, 'SCHEMA') do |result|
keys = each_hash(result).select { |row| row[:constraint_type] == 'PRIMARY KEY' }.map { |row| row[:column_name] }
execute_and_free("SHOW INDEX FROM #{quote_table_name(table)} WHERE Key_name = 'PRIMARY'", 'SCHEMA') do |result|
keys = each_hash(result).map { |row| row[:Column_name] }
keys.length == 1 ? [keys.first, nil] : nil
end
end
Expand Down
4 changes: 1 addition & 3 deletions activerecord/lib/active_record/schema_dumper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,7 @@ def table(table, stream)
tbl = StringIO.new

# first dump primary key column
if @connection.respond_to?(:detailed_pk_and_sequence_for)
pk, _ = @connection.detailed_pk_and_sequence_for(table)
elsif @connection.respond_to?(:pk_and_sequence_for)
if @connection.respond_to?(:pk_and_sequence_for)
pk, _ = @connection.pk_and_sequence_for(table)
elsif @connection.respond_to?(:primary_key)
pk = @connection.primary_key(table)
Expand Down

0 comments on commit f9b9179

Please sign in to comment.