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


589
590
591
# File 'lib/redmine/plugin.rb', line 589

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

.get_all_versions(plugin = current_plugin) ⇒ Object


577
578
579
580
581
582
583
584
585
586
587
# File 'lib/redmine/plugin.rb', line 577

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


570
571
572
573
574
575
# File 'lib/redmine/plugin.rb', line 570

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

  MigrationContext.new(plugin.migration_directory, ::ActiveRecord::Base.connection.schema_migration).migrate(version)
end

Instance Method Details

#load_migratedObject


594
595
596
# File 'lib/redmine/plugin.rb', line 594

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

#record_version_state_after_migrating(version) ⇒ Object


598
599
600
# File 'lib/redmine/plugin.rb', line 598

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