Skip to content

Commit 8a7d3c2

Browse files
committed
[mysql] backport rename_index from Rails (corectly handling MariaDB as well)
rails/rails@1133818
1 parent 7789453 commit 8a7d3c2

File tree

1 file changed

+29
-4
lines changed

1 file changed

+29
-4
lines changed

lib/arjdbc/mysql/adapter.rb

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -238,9 +238,14 @@ def supports_views?
238238
version[0] && version[0] >= 5
239239
end
240240

241+
def supports_rename_index?
242+
return false if mariadb? || ! version[0]
243+
(version[0] == 5 && version[1] >= 7) || version[0] >= 6
244+
end
245+
241246
# @override
242247
def supports_transaction_isolation?(level = nil)
243-
version[0] >= 5 # MySQL 5+
248+
version[0] && version[0] >= 5 # MySQL 5+
244249
end
245250

246251
# NOTE: handled by JdbcAdapter we override only to have save-point in logs :
@@ -411,6 +416,10 @@ def create_table(name, options = {})
411416
super(name, {:options => "ENGINE=InnoDB DEFAULT CHARSET=utf8"}.merge(options))
412417
end
413418

419+
def drop_table(table_name, options = {})
420+
execute "DROP#{' TEMPORARY' if options[:temporary]} TABLE #{quote_table_name(table_name)}"
421+
end
422+
414423
# @override
415424
def rename_table(table_name, new_name)
416425
execute "RENAME TABLE #{quote_table_name(table_name)} TO #{quote_table_name(new_name)}"
@@ -423,6 +432,15 @@ def remove_index!(table_name, index_name)
423432
execute "DROP INDEX #{quote_column_name(index_name)} ON #{quote_table_name(table_name)}"
424433
end
425434

435+
# @override
436+
def rename_index(table_name, old_name, new_name)
437+
if supports_rename_index?
438+
execute "ALTER TABLE #{quote_table_name(table_name)} RENAME INDEX #{quote_table_name(old_name)} TO #{quote_table_name(new_name)}"
439+
else
440+
super
441+
end
442+
end
443+
426444
# @override
427445
def add_column(table_name, column_name, type, options = {})
428446
add_column_sql = "ALTER TABLE #{quote_table_name(table_name)} ADD #{quote_column_name(column_name)} #{type_to_sql(type, options[:limit], options[:precision], options[:scale])}"
@@ -616,6 +634,8 @@ def column_for(table_name, column_name)
616634
column
617635
end
618636

637+
def mariadb?; !! ( full_version =~ /mariadb/i ) end
638+
619639
def version
620640
return @version ||= begin
621641
version = []
@@ -625,9 +645,7 @@ def version
625645
version << jdbc_connection.serverMinorVersion
626646
version << jdbc_connection.serverSubMinorVersion
627647
else
628-
result = execute 'SELECT VERSION()', 'SCHEMA'
629-
result = result.first.values.first # [{"VERSION()"=>"5.5.37-0ubuntu..."}]
630-
if match = result.match(/^(\d)\.(\d+)\.(\d+)/)
648+
if match = full_version.match(/^(\d)\.(\d+)\.(\d+)/)
631649
version << match[1].to_i
632650
version << match[2].to_i
633651
version << match[3].to_i
@@ -637,6 +655,13 @@ def version
637655
end
638656
end
639657

658+
def full_version
659+
@full_version ||= begin
660+
result = execute 'SELECT VERSION()', 'SCHEMA'
661+
result.first.values.first # [{"VERSION()"=>"5.5.37-0ubuntu..."}]
662+
end
663+
end
664+
640665
end
641666
end
642667

0 commit comments

Comments
 (0)