Project

General

Profile

Defect #37686 ยป 0001-Fix-stale-cached-versions-when-a-plugin-is-migrated-.patch

Go MAEDA, 2026-09-19 04:11

View differences:

lib/redmine/plugin.rb
544 544
        def current_version(plugin = current_plugin)
545 545
          get_all_versions(plugin).last || 0
546 546
        end
547

  
548
        # Discards the cached versions of the given plugin so that they are
549
        # reloaded from the database. Discards those of all plugins if no plugin is given
550
        def clear_cached_versions(plugin = nil)
551
          if plugin
552
            @all_versions&.delete(plugin.id.to_s)
553
          else
554
            @all_versions = nil
555
          end
556
        end
547 557
      end
548 558
      def load_migrated
......
552 562
      def record_version_state_after_migrating(version)
553 563
        super(version.to_s + "-" + current_plugin.id.to_s)
564
        self.class.clear_cached_versions(current_plugin)
554 565
      end
555 566
    end
556 567
  end
test/unit/lib/redmine/plugin_test.rb
226 226
    assert Redmine::Plugin.migrate('foo_plugin')
227 227
  end
228
  def test_migrate_should_not_use_stale_cached_versions
229
    plugin = @klass.register :foo_plugin do
230
      name 'Foo plugin'
231
      version '0.0.1'
232
    end
233
    migrator = Redmine::Plugin::Migrator
234

  
235
    Dir.mktmpdir do |dir|
236
      {'001_foo_plugin_stale_cache_first.rb' => 'FooPluginStaleCacheFirst',
237
       '002_foo_plugin_stale_cache_second.rb' => 'FooPluginStaleCacheSecond'}.each do |filename, class_name|
238
        File.write(
239
          File.join(dir, filename),
240
          "class #{class_name} < ActiveRecord::Migration[8.1]\n  def up; end\n\n  def down; end\nend\n"
241
        )
242
      end
243
      plugin.stubs(:migration_directory).returns(dir)
244

  
245
      ActiveRecord::Migration.suppress_messages do
246
        plugin.migrate(1)
247
        assert_equal 1, migrator.current_version(plugin)
248
        plugin.migrate
249
        assert_equal 2, migrator.current_version(plugin)
250
        plugin.migrate(0)
251
        assert_equal 0, migrator.current_version(plugin)
252
        plugin.migrate
253
        assert_equal 2, migrator.current_version(plugin)
254
      end
255
    end
256
  ensure
257
    Redmine::Plugin::Migrator.clear_cached_versions
258
  end
259

  
228 260
  def test_migration_context_should_override_current_version
229 261
    plugin = @klass.register :foo_plugin do
230 262
      name 'Foo plugin'
    (1-1/1)