From f3a2627e21fee6ab08183466ff24e2e93bfc0ecc Mon Sep 17 00:00:00 2001 From: "Alvaro F. Lara" Date: Fri, 9 Jan 2015 14:02:43 +0000 Subject: [PATCH] Ensure Foreigner is loaded for Rails 4.1.9 When using Foreigner on Rails 4.1.9 and trying to run `schema::load` Foreigner has not been initialized yet so this methods don't exist. This change was introduced by the following Rails commit: * (https://github.com/rails/rails/commit/5d6bb89f) So in order to ensure Foreigner has been initialized and methods are present for `schema::load` to work correctly, we include `Foreigner.load` inside the `schema.rb` file before any of the foreign key methods. Also: * Introduce Foreigner::Helper to store class level helpers * Simplify the way to determine which version of ActiveRecord is using --- lib/foreigner.rb | 1 + lib/foreigner/helper.rb | 13 +++++++++++++ lib/foreigner/schema_dumper.rb | 4 ++++ test/foreigner/schema_dumper_test.rb | 12 +++++++++++- 4 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 lib/foreigner/helper.rb 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_'