Skip to content

Commit

Permalink
Merge pull request #162 from guiman/fix_foreigner_loading_problem
Browse files Browse the repository at this point in the history
Ensure Foreigner is loaded for Rails 4.1.9
  • Loading branch information
matthuhiggins committed Jan 10, 2015
2 parents d1cdc84 + f3a2627 commit 6ca2c29
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/foreigner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ module Migration
Foreigner::Adapter.register 'sqlite3', 'foreigner/connection_adapters/noop_adapter'

require 'foreigner/loader'
require 'foreigner/helper'
require 'foreigner/railtie' if defined?(Rails)
13 changes: 13 additions & 0 deletions lib/foreigner/helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Foreigner
module Helper
def self.active_record_version
if ::ActiveRecord.respond_to? :version
ActiveRecord.version.to_s
elsif ::ActiveRecord::VERSION
::ActiveRecord::VERSION
else
raise "Unknown ActiveRecord Version API"
end
end
end
end
4 changes: 4 additions & 0 deletions lib/foreigner/schema_dumper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ def remove_prefix_and_suffix(table_name)

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
Expand Down
12 changes: 11 additions & 1 deletion test/foreigner/schema_dumper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def tables
class MockSchemaDumper
cattr_accessor :ignore_tables

attr_accessor :processed_tables
attr_accessor :processed_tables, :stream
def initialize
@connection = MockConnection.new
@processed_tables = []
Expand All @@ -21,6 +21,7 @@ def tables(stream)
end

def foreign_keys(table, stream)
self.stream = stream
processed_tables << table
end

Expand Down Expand Up @@ -49,6 +50,15 @@ def foreign_keys(table, stream)
assert_equal ['bar'].to_set, dumper.processed_tables.to_set
end

test '4.1.9 loading error' do
Foreigner::Helper.stubs(:active_record_version).returns("4.1.9")
MockSchemaDumper.ignore_tables = []
dumper = MockSchemaDumper.new
dumper.tables(StringIO.new)

assert_match(/Foreigner\.load/, dumper.stream.string)
end

test 'removes table name suffix and prefix' do
begin
ActiveRecord::Base.table_name_prefix = 'pre_'
Expand Down

0 comments on commit 6ca2c29

Please sign in to comment.