Project

General

Profile

Patch #28934 » plugin.rb.patch

Pavel Rosický, 2018-06-02 00:38

View differences:

plugin.rb (working copy)
470 470
      end
471 471
    end
472 472

  
473
    unless ActiveRecord.version.release() < Gem::Version.new('5.2.0')
474
      class MigrationContext < ActiveRecord::MigrationContext
475
        def up(target_version = nil)
476
          selected_migrations = if block_given?
477
            migrations.select { |m| yield m }
478
          else
479
            migrations
480
          end
481

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

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

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

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

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

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

  
515
          if ActiveRecord.version.release() < Gem::Version.new('5.2.0')
516
            migrate(plugin.migration_directory, version)
517
          else
518
            MigrationContext.new(plugin.migration_directory).migrate(version)
519
          end
483 520
        end
484 521

  
485
        def current_version(plugin=current_plugin)
522
        def get_all_versions(plugin = current_plugin)
486 523
          # 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
524
          @all_versions ||= {}
525
          @all_versions[plugin.id.to_s] ||= begin
526
            sm_table = ::ActiveRecord::SchemaMigration.table_name
527
            migration_versions  = ActiveRecord::Base.connection.select_values("SELECT version FROM #{sm_table}")
528
            versions_by_plugins = migration_versions.group_by { |version| version.match(/-(.*)$/).try(:[], 1) }
529
            @all_versions       = versions_by_plugins.transform_values! {|versions| versions.map!(&:to_i).sort! }
530
            @all_versions[plugin.id.to_s] || []
531
          end
491 532
        end
533

  
534
        def current_version(plugin = current_plugin)
535
          get_all_versions(plugin).last || 0
536
        end
492 537
      end
493 538

  
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
539
      def load_migrated
540
        @migrated_versions = Set.new(self.class.get_all_versions(current_plugin))
499 541
      end
500 542

  
501 543
      def record_version_state_after_migrating(version)
(1-1/4)