Project

General

Profile

Patch #28934 » use_migration_context_with_test2.patch

Tatsuya Saito, 2018-07-07 04:23

View differences:

lib/redmine/plugin.rb (working copy)
470 470
      end
471 471
    end
472 472

  
473
    class MigrationContext < ActiveRecord::MigrationContext
474
      def up(target_version = nil)
475
        selected_migrations = if block_given?
476
          migrations.select { |m| yield m }
477
        else
478
          migrations
479
        end
480

  
481
        Migrator.new(:up, selected_migrations, target_version).migrate
482
      end
483

  
484
      def down(target_version = nil)
485
        selected_migrations = if block_given?
486
          migrations.select { |m| yield m }
487
        else
488
          migrations
489
        end
490

  
491
        Migrator.new(:down, selected_migrations, target_version).migrate
492
      end
493

  
494
      def run(direction, target_version)
495
        Migrator.new(direction, migrations, target_version).run
496
      end
497

  
498
      def open
499
        Migrator.new(:up, migrations, nil)
500
      end
501
    end
502

  
473 503
    class Migrator < ActiveRecord::Migrator
474 504
      # We need to be able to set the 'current' plugin being migrated.
475 505
      cattr_accessor :current_plugin
......
479 509
        def migrate_plugin(plugin, version)
480 510
          self.current_plugin = plugin
481 511
          return if current_version(plugin) == version
482
          migrate(plugin.migration_directory, version)
512

  
513
          MigrationContext.new(plugin.migration_directory).migrate(version)
483 514
        end
484 515

  
485
        def current_version(plugin=current_plugin)
516
        def get_all_versions(plugin = current_plugin)
486 517
          # Delete migrations that don't match .. to_i will work because the number comes first
487
          sm_table = ::ActiveRecord::SchemaMigration.table_name
488
          ::ActiveRecord::Base.connection.select_values(
489
            "SELECT version FROM #{sm_table}"
490
          ).delete_if{ |v| v.match(/-#{plugin.id}$/) == nil }.map(&:to_i).max || 0
518
          @all_versions ||= {}
519
          @all_versions[plugin.id.to_s] ||= begin
520
            sm_table = ::ActiveRecord::SchemaMigration.table_name
521
            migration_versions  = ActiveRecord::Base.connection.select_values("SELECT version FROM #{sm_table}")
522
            versions_by_plugins = migration_versions.group_by { |version| version.match(/-(.*)$/).try(:[], 1) }
523
            @all_versions       = versions_by_plugins.transform_values! {|versions| versions.map!(&:to_i).sort! }
524
            @all_versions[plugin.id.to_s] || []
525
          end
491 526
        end
527

  
528
        def current_version(plugin = current_plugin)
529
          get_all_versions(plugin).last || 0
530
        end
492 531
      end
493 532

  
494
      def migrated
495
        sm_table = ::ActiveRecord::SchemaMigration.table_name
496
        ::ActiveRecord::Base.connection.select_values(
497
          "SELECT version FROM #{sm_table}"
498
        ).delete_if{ |v| v.match(/-#{current_plugin.id}$/) == nil }.map(&:to_i).sort
533
      def load_migrated
534
        @migrated_versions = Set.new(self.class.get_all_versions(current_plugin))
499 535
      end
500 536

  
501 537
      def record_version_state_after_migrating(version)
test/unit/lib/redmine/plugin_test.rb (working copy)
183 183
    Rails.logger.expects(:warn)
184 184
    @klass.register(:bar) { settings :partial => 'foo/settings' }
185 185
  end
186

  
187
  def test_migrate_redmine_plugin
188
    @klass.register :foo do
189
      name 'Foo plugin'
190
      version '0.0.1'
191
    end
192

  
193
    assert Redmine::Plugin.migrate('foo')
194
  end
186 195
end
(4-4/4)