@@ -23,49 +23,37 @@ def add_rails_engine_migration_paths
2323 # @see ActiveRecord::MigrationContext.migrate
2424 def migrate ( config = nil , verbose = false )
2525 ran = [ ]
26- # Rails 5 changes ActiveRecord parents means to migrate outside
27- # the `rake` task framework has to dig a little lower into ActiveRecord
28- # to set up the DB connection capable of interacting with migration.
29- previouslyConnected = ActiveRecord ::Base . connected?
30- unless previouslyConnected
31- ApplicationRecord . remove_connection
32- ActiveRecord ::Base . establish_connection ( config )
33- end
26+
3427 ActiveRecord ::Migration . verbose = verbose
3528 ActiveRecord ::Base . connection_pool . with_connection do
3629 begin
37- context = default_migration_context
38- if needs_migration? ( context )
39- ran = context . migrate
30+ with_migration_context do |context |
31+ if context . needs_migration?
32+ ran = context . migrate
33+ end
4034 end
41- # ActiveRecord::Migrator#migrate rescues all errors and re-raises them
42- # as StandardError
35+ # ActiveRecord::Migrator#migrate rescues all errors and re-raises them as StandardError
4336 rescue StandardError => error
4437 self . error = error
4538 elog ( 'DB.migrate threw an exception' , error : error )
4639 end
4740 end
4841
49- unless previouslyConnected
50- ActiveRecord ::Base . remove_connection
51- ApplicationRecord . establish_connection ( config )
52- end
5342 # Since the connections that existed before the migrations ran could
5443 # have outdated column information, reset column information for all
5544 # ApplicationRecord descendents to prevent missing method errors for
5645 # column methods for columns created in migrations after the column
5746 # information was cached.
5847 reset_column_information
5948
60- return ran
49+ ran
6150 end
6251
6352 # Determine if the currently established database connection needs migration
6453 #
65- # @param [ActiveRecord::MigrationContext,snil] context The migration context to check. Will default if not supplied
6654 # @return [Boolean] True if migration is required, false otherwise
67- def needs_migration? ( context = default_migration_context )
68- ActiveRecord :: Base . connection_pool . with_connection do
55+ def needs_migration?
56+ with_migration_context do | context |
6957 return context . needs_migration?
7058 end
7159 end
@@ -77,6 +65,10 @@ def needs_migration?(context = default_migration_context)
7765
7866 private
7967
68+ def with_migration_context
69+ yield ActiveRecord ::MigrationContext . new ( gather_engine_migration_paths )
70+ end
71+
8072 # @return [ActiveRecord::MigrationContext]
8173 def default_migration_context
8274 ActiveRecord ::MigrationContext . new ( gather_engine_migration_paths , ActiveRecord ::SchemaMigration )
0 commit comments