Chris@909: # The Plugin::Migrator class contains the logic to run migrations from Chris@909: # within plugin directories. The directory in which a plugin's migrations Chris@909: # should be is determined by the Plugin#migration_directory method. Chris@909: # Chris@909: # To migrate a plugin, you can simple call the migrate method (Plugin#migrate) Chris@909: # with the version number that plugin should be at. The plugin's migrations Chris@909: # will then be used to migrate up (or down) to the given version. Chris@909: # Chris@909: # For more information, see Engines::RailsExtensions::Migrations Chris@909: class Engines::Plugin::Migrator < ActiveRecord::Migrator Chris@909: Chris@909: # We need to be able to set the 'current' engine being migrated. Chris@909: cattr_accessor :current_plugin Chris@909: Chris@909: class << self Chris@909: # Runs the migrations from a plugin, up (or down) to the version given Chris@909: def migrate_plugin(plugin, version) Chris@909: self.current_plugin = plugin Chris@909: return if current_version(plugin) == version Chris@909: migrate(plugin.migration_directory, version) Chris@909: end Chris@909: Chris@909: def current_version(plugin=current_plugin) Chris@909: # Delete migrations that don't match .. to_i will work because the number comes first Chris@909: ::ActiveRecord::Base.connection.select_values( Chris@909: "SELECT version FROM #{schema_migrations_table_name}" Chris@909: ).delete_if{ |v| v.match(/-#{plugin.name}/) == nil }.map(&:to_i).max || 0 Chris@909: end Chris@909: end Chris@909: Chris@909: def migrated Chris@909: sm_table = self.class.schema_migrations_table_name Chris@909: ::ActiveRecord::Base.connection.select_values( Chris@909: "SELECT version FROM #{sm_table}" Chris@909: ).delete_if{ |v| v.match(/-#{current_plugin.name}/) == nil }.map(&:to_i).sort Chris@909: end Chris@909: Chris@909: def record_version_state_after_migrating(version) Chris@909: super(version.to_s + "-" + current_plugin.name) Chris@909: end Chris@909: end