Class: Redmine::Plugin::Migrator

Inherits:
ActiveRecord::Migrator
  • Object
show all
Defined in:
lib/redmine/plugin.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.current_version(plugin = current_plugin) ⇒ Object


557
558
559
# File 'lib/redmine/plugin.rb', line 557

def current_version(plugin = current_plugin)
  get_all_versions(plugin).last || 0
end

.get_all_versions(plugin = current_plugin) ⇒ Object


545
546
547
548
549
550
551
552
553
554
555
# File 'lib/redmine/plugin.rb', line 545

def get_all_versions(plugin = current_plugin)
  # Delete migrations that don't match .. to_i will work because the number comes first
  @all_versions ||= {}
  @all_versions[plugin.id.to_s] ||= begin
    sm_table = ::ActiveRecord::SchemaMigration.table_name
    migration_versions  = ActiveRecord::Base.connection.select_values("SELECT version FROM #{sm_table}")
    versions_by_plugins = migration_versions.group_by { |version| version.match(/-(.*)$/).try(:[], 1) }
    @all_versions       = versions_by_plugins.transform_values! {|versions| versions.map!(&:to_i).sort! }
    @all_versions[plugin.id.to_s] || []
  end
end

.migrate_plugin(plugin, version) ⇒ Object

Runs the migrations from a plugin, up (or down) to the version given


538
539
540
541
542
543
# File 'lib/redmine/plugin.rb', line 538

def migrate_plugin(plugin, version)
  self.current_plugin = plugin
  return if current_version(plugin) == version

  MigrationContext.new(plugin.migration_directory).migrate(version)
end

Instance Method Details

#load_migratedObject


562
563
564
# File 'lib/redmine/plugin.rb', line 562

def load_migrated
  @migrated_versions = Set.new(self.class.get_all_versions(current_plugin))
end

#record_version_state_after_migrating(version) ⇒ Object


566
567
568
# File 'lib/redmine/plugin.rb', line 566

def record_version_state_after_migrating(version)
  super(version.to_s + "-" + current_plugin.id.to_s)
end