diff --git a/lib/foreigner.rb b/lib/foreigner.rb index 2b732d6..62f1902 100644 --- a/lib/foreigner.rb +++ b/lib/foreigner.rb @@ -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) diff --git a/lib/foreigner/helper.rb b/lib/foreigner/helper.rb new file mode 100644 index 0000000..c6cce92 --- /dev/null +++ b/lib/foreigner/helper.rb @@ -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 diff --git a/lib/foreigner/schema_dumper.rb b/lib/foreigner/schema_dumper.rb index 35d20d7..93fd211 100644 --- a/lib/foreigner/schema_dumper.rb +++ b/lib/foreigner/schema_dumper.rb @@ -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 diff --git a/test/foreigner/schema_dumper_test.rb b/test/foreigner/schema_dumper_test.rb index 340fead..84651ba 100644 --- a/test/foreigner/schema_dumper_test.rb +++ b/test/foreigner/schema_dumper_test.rb @@ -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 = [] @@ -21,6 +21,7 @@ def tables(stream) end def foreign_keys(table, stream) + self.stream = stream processed_tables << table end @@ -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_'