提交 99eae3f8 编写于 作者: J Jeremy Kemper

Merge pull request #5900 from amatsuda/ar_mysql_pk_with_using_option

current MySQL driver fails to fetch PK name when the PK has USING option
......@@ -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
......
......@@ -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|
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册