To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / vendor / plugins / engines / lib / engines / plugin / migrator.rb @ 442:753f1380d6bc

History | View | Annotate | Download (1.63 KB)

1
# The Plugin::Migrator class contains the logic to run migrations from
2
# within plugin directories. The directory in which a plugin's migrations
3
# should be is determined by the Plugin#migration_directory method.
4
#
5
# To migrate a plugin, you can simple call the migrate method (Plugin#migrate)
6
# with the version number that plugin should be at. The plugin's migrations
7
# will then be used to migrate up (or down) to the given version.
8
#
9
# For more information, see Engines::RailsExtensions::Migrations
10
class Engines::Plugin::Migrator < ActiveRecord::Migrator
11

    
12
  # We need to be able to set the 'current' engine being migrated.
13
  cattr_accessor :current_plugin
14

    
15
  class << self
16
    # Runs the migrations from a plugin, up (or down) to the version given
17
    def migrate_plugin(plugin, version)
18
      self.current_plugin = plugin
19
      return if current_version(plugin) == version
20
      migrate(plugin.migration_directory, version)
21
    end
22
    
23
    def current_version(plugin=current_plugin)
24
      # Delete migrations that don't match .. to_i will work because the number comes first
25
      ::ActiveRecord::Base.connection.select_values(
26
        "SELECT version FROM #{schema_migrations_table_name}"
27
      ).delete_if{ |v| v.match(/-#{plugin.name}/) == nil }.map(&:to_i).max || 0
28
    end
29
  end
30
       
31
  def migrated
32
    sm_table = self.class.schema_migrations_table_name
33
    ::ActiveRecord::Base.connection.select_values(
34
      "SELECT version FROM #{sm_table}"
35
    ).delete_if{ |v| v.match(/-#{current_plugin.name}/) == nil }.map(&:to_i).sort
36
  end
37
  
38
  def record_version_state_after_migrating(version)
39
    super(version.to_s + "-" + current_plugin.name)
40
  end
41
end