From f61a544bd34c1d65a81a9416ebaee7702d76849a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marius=20B=C4=82LTEANU?= Date: Sun, 7 Sep 2025 12:29:11 +0300 Subject: [PATCH] In production mode, check for pending migration on boot and halt or just shown an warning message. Control this behaviour using the new configuration flag redmine_halt_on_pending_migrations. (#36933). --- config/additional_environment.rb.example | 2 ++ config/application.rb | 4 ++++ .../05-check_pending_migrations.rb | 24 +++++++++++++++++++ 3 files changed, 30 insertions(+) create mode 100644 config/initializers/05-check_pending_migrations.rb diff --git a/config/additional_environment.rb.example b/config/additional_environment.rb.example index 57ca1021a..ef7a0f85f 100644 --- a/config/additional_environment.rb.example +++ b/config/additional_environment.rb.example @@ -13,6 +13,8 @@ # # set up external systems like Sidekiq and Redis. # config.active_job.queue_adapter = :inline # +# Halt boot if there are pending migrations +# config.redmine_halt_on_pending_migrations = true # ... # diff --git a/config/application.rb b/config/application.rb index 96c6f9fb4..3d494cc46 100644 --- a/config/application.rb +++ b/config/application.rb @@ -107,6 +107,10 @@ module RedmineApp :same_site => :lax ) + # Configure redmine_halt_on_pending_migrations only for production so that additional environment file + # can change it (environments/ENV.rb would take precedence over it) + config.redmine_halt_on_pending_migrations = false if Rails.env.production? + if File.exist?(File.join(File.dirname(__FILE__), 'additional_environment.rb')) instance_eval File.read(File.join(File.dirname(__FILE__), 'additional_environment.rb')) end diff --git a/config/initializers/05-check_pending_migrations.rb b/config/initializers/05-check_pending_migrations.rb new file mode 100644 index 000000000..c57548d40 --- /dev/null +++ b/config/initializers/05-check_pending_migrations.rb @@ -0,0 +1,24 @@ +if Rails.env.production? && (defined?(Rails::Server) || defined?(Rails::Console)) + begin + ActiveRecord::Migration.check_all_pending! + + rescue ActiveRecord::PendingMigrationError + if Rails.configuration.redmine_halt_on_pending_migrations + error_message = "\e[31m[HALT] Pending migrations found. Boot process is stopping.\e[0m" + + puts "\n#{'=' * 60}\n" + puts error_message + puts "Please run `bundle exec rake db:migrate` to apply migrations." + puts "#{'=' * 60}\n\n" + + # Abort the process with a non-zero exit code to indicate failure. + abort + else + warning_message = "\e[33m[WARNING] You have pending migrations. Run `bundle exec rake db:migrate` to apply them.\e[0m" + + puts "\n#{'=' * 60}\n" + puts warning_message + puts "#{'=' * 60}\n\n" + end + end +end -- 2.39.5 (Apple Git-154)