Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

current MySQL driver fails to fetch PK name when the PK has USING option #5900

Merged
merged 1 commit into from Apr 19, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -516,7 +516,7 @@ def show_variable(name)
def pk_and_sequence_for(table)
execute_and_free("SHOW CREATE TABLE #{quote_table_name(table)}", 'SCHEMA') do |result|
create_table = each_hash(result).first[:"Create Table"]
if create_table.to_s =~ /PRIMARY KEY\s+\((.+)\)/
if create_table.to_s =~ /PRIMARY KEY\s+(?:USING\s+\w+\s+)?\((.+)\)/
keys = $1.split(",").map { |key| key.delete('`"') }
keys.length == 1 ? [keys.first, nil] : nil
else
Expand Down
30 changes: 30 additions & 0 deletions activerecord/test/cases/adapters/mysql/mysql_adapter_test.rb
Expand Up @@ -56,6 +56,36 @@ def test_tables_quoting
end
end

def test_pk_and_sequence_for
pk, seq = @conn.pk_and_sequence_for('ex')
assert_equal 'id', pk
assert_equal @conn.default_sequence_name('ex', 'id'), seq
end

def test_pk_and_sequence_for_with_non_standard_primary_key
@conn.exec_query('drop table if exists ex_with_non_standard_pk')
@conn.exec_query(<<-eosql)
CREATE TABLE `ex_with_non_standard_pk` (
`code` INT(11) DEFAULT NULL auto_increment,
PRIMARY KEY (`code`))
eosql
pk, seq = @conn.pk_and_sequence_for('ex_with_non_standard_pk')
assert_equal 'code', pk
assert_equal @conn.default_sequence_name('ex_with_non_standard_pk', 'code'), seq
end

def test_pk_and_sequence_for_with_custom_index_type_pk
@conn.exec_query('drop table if exists ex_with_custom_index_type_pk')
@conn.exec_query(<<-eosql)
CREATE TABLE `ex_with_custom_index_type_pk` (
`id` INT(11) DEFAULT NULL auto_increment,
PRIMARY KEY USING BTREE (`id`))
eosql
pk, seq = @conn.pk_and_sequence_for('ex_with_custom_index_type_pk')
assert_equal 'id', pk
assert_equal @conn.default_sequence_name('ex_with_custom_index_type_pk', 'id'), seq
end

private
def insert(ctx, data)
binds = data.map { |name, value|
Expand Down