Sha256: 420bb89d915120c0d03611ae33723f6810db096e7820a222733c6f55abda20b7

Contents?: true

Size: 1.38 KB

Versions: 6

Compression:

Stored size: 1.38 KB

Contents

module RedHillConsulting::Core::ActiveRecord::ConnectionAdapters
  # MySQL4-specific behaviors
  module Mysql4Adapter
    def reverse_foreign_keys(table_name, name = nil)
      tables = execute("SHOW TABLES")

      foreign_keys = []
      
      tables.each do |table|
        results = execute("SHOW CREATE TABLE #{table}")
        results.each do |row|
          row[1].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.downcase.gsub(' ', '_').to_sym if on_update
              on_delete = on_delete.downcase.gsub(' ', '_').to_sym if on_delete

              if references_table_name == table_name
                foreign_keys << ForeignKeyDefinition.new(name,
                                               table, column_names.gsub('`', '').split(', '),
                                               references_table_name, references_column_names.gsub('`', '').split(', '),
                                               on_update, on_delete)
              end
            end
          end
        end
      end

      foreign_keys
    end
  end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
frolic-redhillonrails_core-0.0.1 lib/red_hill_consulting/core/active_record/connection_adapters/mysql4_adapter.rb
redhillonrails_core-1.0.3.1 lib/red_hill_consulting/core/active_record/connection_adapters/mysql4_adapter.rb
redhillonrails_core-1.0.3 lib/red_hill_consulting/core/active_record/connection_adapters/mysql4_adapter.rb
redhillonrails_core-1.0.2 lib/red_hill_consulting/core/active_record/connection_adapters/mysql4_adapter.rb
redhillonrails_core-1.0.1 lib/red_hill_consulting/core/active_record/connection_adapters/mysql4_adapter.rb
redhillonrails_core-1.0.0 lib/red_hill_consulting/core/active_record/connection_adapters/mysql4_adapter.rb