Skip to content

Commit

Permalink
Fixes #29498 - improve pending_migrations check
Browse files Browse the repository at this point in the history
  • Loading branch information
ezr-ondrej committed Apr 7, 2020
1 parent bad5f87 commit 77361ea
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
15 changes: 12 additions & 3 deletions app/registries/foreman/plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,14 @@ def initialize(id)
@status_extension = nil
end

def engine
@engine ||= Rails::Engine.find(path) if path
end

def migrations_paths
engine.paths['db/migrate'].existent
end

def fact_importer_registry
self.class.fact_importer_registry
end
Expand Down Expand Up @@ -344,12 +352,13 @@ def add_all_permissions_to_default_roles

def pending_migrations
return true if Foreman.in_setup_db_rake?
return @pending_migrations unless @pending_migrations.nil?

pending_migrations = ActiveRecord::Base.connection.migration_context.needs_migration?
@pending_migrations = ActiveRecord::MigrationContext.new(migrations_paths, ActiveRecord::SchemaMigration).needs_migration?

Rails.logger.debug("There are pending migrations. Please run foreman-rake db:migrate.") if pending_migrations
Rails.logger.debug("There are pending migrations. Please run foreman-rake db:migrate.") if @pending_migrations

pending_migrations
@pending_migrations
end

# List of helper methods allowed for templates in safe mode
Expand Down
6 changes: 1 addition & 5 deletions app/services/foreman_seeder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,7 @@ def foreman_seeds

def plugin_seeds
Foreman::Plugin.registered_plugins.collect do |name, plugin|
engine = (name.to_s.tr('-', '_').camelize + '::Engine').constantize
Dir.glob(engine.root + 'db/seeds.d/*.rb')
rescue NameError => e
Foreman::Logging.exception("Failed to register plugin #{name}", e)
nil
Dir.glob(plugin.engine.root + 'db/seeds.d/*.rb') if plugin.engine
end.flatten.compact
end

Expand Down
17 changes: 17 additions & 0 deletions config/initializers/z_rails_5_compatibility.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,24 @@ def call(template, source = nil)
super(template, source)
end
end

module PluginMigrations
def pending_migrations
if Rails.version.start_with?('5.2')
return true if Foreman.in_setup_db_rake?

@pending_migrations = ActiveRecord::MigrationContext.new(migrations_paths).needs_migration? if @pending_migrations.nil?

Rails.logger.debug("There are pending migrations. Please run foreman-rake db:migrate.") if @pending_migrations

@pending_migrations
else
super
end
end
end
end
end

ActionView::Template::Handlers::Rabl.singleton_class.prepend Foreman::Rails5::RablTemplateHandlerExt
Foreman::Plugin.prepend Foreman::Rails5::PluginMigrations

0 comments on commit 77361ea

Please sign in to comment.