Module: SchemaPlus::ActiveRecord::ConnectionAdapters::MysqlAdapter
- Defined in:
- lib/schema_plus/active_record/connection_adapters/mysql_adapter.rb
Overview
SchemaPlus includes a MySQL implementation of the AbstractAdapater extensions. (This works with both the mysql</t> and <tt>mysql2
gems.)
Class Method Summary collapse
-
.included(base) ⇒ Object
:enddoc:.
Instance Method Summary collapse
- #foreign_keys(table_name, name = nil) ⇒ Object
- #remove_column_with_schema_plus(table_name, column_name) ⇒ Object
- #remove_foreign_key(table_name, foreign_key_name, options = {}) ⇒ Object
- #reverse_foreign_keys(table_name, name = nil) ⇒ Object
- #tables_with_schema_plus(name = nil, *args) ⇒ Object
- #view_definition(view_name, name = nil) ⇒ Object
- #views(name = nil) ⇒ Object
Class Method Details
.included(base) ⇒ Object
:enddoc:
11 12 13 14 15 16 |
# File 'lib/schema_plus/active_record/connection_adapters/mysql_adapter.rb', line 11 def self.included(base) base.class_eval do alias_method_chain :tables, :schema_plus alias_method_chain :remove_column, :schema_plus end end |
Instance Method Details
#foreign_keys(table_name, name = nil) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/schema_plus/active_record/connection_adapters/mysql_adapter.rb', line 34 def foreign_keys(table_name, name = nil) results = execute("SHOW CREATE TABLE #{quote_table_name(table_name)}", name) foreign_keys = [] results.each do |row| row[1].lines.each do |line| if line =~ /^ CONSTRAINT [`"](.+?)[`"] FOREIGN KEY \([`"](.+?)[`"]\) REFERENCES [`"](.+?)[`"] \((.+?)\)( ON DELETE (.+?))?( ON UPDATE (.+?))?,?$/ name = $1 column_names = $2 references_table_name = $3 references_column_names = $4 on_update = $8 on_delete = $6 on_update = on_update ? on_update.downcase.gsub(' ', '_').to_sym : :restrict on_delete = on_delete ? on_delete.downcase.gsub(' ', '_').to_sym : :restrict foreign_keys << ForeignKeyDefinition.new(name, table_name, column_names.gsub('`', '').split(', '), references_table_name, references_column_names.gsub('`', '').split(', '), on_update, on_delete) end end end foreign_keys end |
#remove_column_with_schema_plus(table_name, column_name) ⇒ Object
22 23 24 25 26 27 |
# File 'lib/schema_plus/active_record/connection_adapters/mysql_adapter.rb', line 22 def remove_column_with_schema_plus(table_name, column_name) foreign_keys(table_name).select { |foreign_key| foreign_key.column_names.include?(column_name.to_s) }.each do |foreign_key| remove_foreign_key(table_name, foreign_key.name) end remove_column_without_schema_plus(table_name, column_name) end |
#remove_foreign_key(table_name, foreign_key_name, options = {}) ⇒ Object
29 30 31 |
# File 'lib/schema_plus/active_record/connection_adapters/mysql_adapter.rb', line 29 def remove_foreign_key(table_name, foreign_key_name, = {}) execute "ALTER TABLE #{quote_table_name(table_name)} DROP FOREIGN KEY #{foreign_key_name}" end |
#reverse_foreign_keys(table_name, name = nil) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/schema_plus/active_record/connection_adapters/mysql_adapter.rb', line 62 def reverse_foreign_keys(table_name, name = nil) results = execute(<<-SQL, name) SELECT constraint_name, table_name, column_name, referenced_table_name, referenced_column_name FROM information_schema.key_column_usage WHERE table_schema = SCHEMA() AND referenced_table_schema = table_schema ORDER BY constraint_name, ordinal_position; SQL current_foreign_key = nil foreign_keys = [] results.each do |row| next unless table_name.casecmp(row[3]) == 0 if current_foreign_key != row[0] foreign_keys << ForeignKeyDefinition.new(row[0], row[1], [], row[3], []) current_foreign_key = row[0] end foreign_keys.last.column_names << row[2] foreign_keys.last.references_column_names << row[4] end foreign_keys end |
#tables_with_schema_plus(name = nil, *args) ⇒ Object
18 19 20 |
# File 'lib/schema_plus/active_record/connection_adapters/mysql_adapter.rb', line 18 def tables_with_schema_plus(name=nil, *args) tables_without_schema_plus(name, *args) - views(name) end |
#view_definition(view_name, name = nil) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/schema_plus/active_record/connection_adapters/mysql_adapter.rb', line 95 def view_definition(view_name, name = nil) result = execute("SELECT view_definition, check_option FROM information_schema.views WHERE table_schema = SCHEMA() AND table_name = #{quote(view_name)}", name) return nil unless (result.respond_to?(:num_rows) ? result.num_rows : result.to_a.size) > 0 # mysql vs mysql2 row = result.respond_to?(:fetch_row) ? result.fetch_row : result.first sql = row[0] sql.gsub!(%r{#{quote_table_name(current_database)}[.]}, '') case row[1] when "CASCADED" then sql += " WITH CASCADED CHECK OPTION" when "LOCAL" then sql += " WITH LOCAL CHECK OPTION" end sql end |
#views(name = nil) ⇒ Object
87 88 89 90 91 92 93 |
# File 'lib/schema_plus/active_record/connection_adapters/mysql_adapter.rb', line 87 def views(name = nil) views = [] execute("SELECT table_name FROM information_schema.views WHERE table_schema = SCHEMA()", name).each do |row| views << row[0] end views end |