module Foreigner::SchemaDumper

Public Instance Methods

tables_with_foreign_keys(stream) click to toggle source
# File lib/foreigner/schema_dumper.rb, line 43
def tables_with_foreign_keys(stream)
  tables_without_foreign_keys(stream)
  # Ensure Foreigner to be initialized before running foreign_keys.
  # This is required since schema::load is not initializing the environment
  # anymore in Rails 4.1.9 (https://github.com/rails/rails/commit/5d6bb89f)
  stream.puts '  Foreigner.load' if Foreigner::Helper.active_record_version == '4.1.9'
  @connection.tables.sort.each do |table|
    next if ['schema_migrations', ignore_tables].flatten.any? do |ignored|
      case ignored
      when String; table == ignored
      when Regexp; table =~ ignored
      else
        raise StandardError, 'ActiveRecord::SchemaDumper.ignore_tables accepts an array of String and / or Regexp values.'
      end
    end
    foreign_keys(table, stream)
  end
end

Private Instance Methods

foreign_keys(table_name, stream) click to toggle source
# File lib/foreigner/schema_dumper.rb, line 63
def foreign_keys(table_name, stream)
  if (foreign_keys = @connection.foreign_keys(table_name)).any?
    add_foreign_key_statements = foreign_keys.map do |foreign_key|
      '  ' + self.class.dump_foreign_key(foreign_key)
    end

    stream.puts add_foreign_key_statements.sort.join("\n")
    stream.puts
  end
end