GitHub Sale: sign up for any paid plan this week and pay nothing until January 1, 2009!  [ hide ]

public
Fork of halorgium/mephisto
Description: A mirror of the mephisto code-base
Homepage: http://mephistoblog.com/
Clone URL: git://github.com/zmack/mephisto.git
svenfuchs (author)
Wed Feb 20 11:11:20 -0800 2008
commit  1ad1b56b4a6c9284534b0afbb9d5d87715ae4312
tree    adc435b61f05728a991fd314188bc3e75cfb6038
parent  2772ec18227b04b3cb618748a2900a7b93b84d94
mephisto / vendor / plugins / engines / lib / engines / plugin / migrator.rb
100644 61 lines (55 sloc) 2.193 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# The Plugin::Migrator class contains the logic to run migrations from
# within plugin directories. The directory in which a plugin's migrations
# should be is determined by the Plugin#migration_directory method.
#
# To migrate a plugin, you can simple call the migrate method (Plugin#migrate)
# with the version number that plugin should be at. The plugin's migrations
# will then be used to migrate up (or down) to the given version.
#
# For more information, see Engines::RailsExtensions::Migrations
class Engines::Plugin::Migrator < ActiveRecord::Migrator
 
  # We need to be able to set the 'current' engine being migrated.
  cattr_accessor :current_plugin
 
  # Runs the migrations from a plugin, up (or down) to the version given
  def self.migrate_plugin(plugin, version)
    self.current_plugin = plugin
    migrate(plugin.migration_directory, version)
  end
  
  # Returns the name of the table used to store schema information about
  # installed plugins.
  #
  # See Engines.schema_info_table for more details.
  def self.schema_info_table_name
    ActiveRecord::Base.wrapped_table_name Engines.schema_info_table
  end
 
  # Returns the current version of the given plugin
  def self.current_version(plugin=current_plugin)
    result = ActiveRecord::Base.connection.select_one(<<-ESQL
SELECT version FROM #{schema_info_table_name}
WHERE plugin_name = '#{plugin.name}'
ESQL
    )
    if result
      result["version"].to_i
    else
      # There probably isn't an entry for this engine in the migration info table.
      # We need to create that entry, and set the version to 0
      ActiveRecord::Base.connection.execute(<<-ESQL
INSERT INTO #{schema_info_table_name} (version, plugin_name)
VALUES (0,'#{plugin.name}')
ESQL
      )
      0
    end
  end
 
  # Sets the version of the plugin in Engines::Plugin::Migrator.current_plugin to
  # the given version.
  def set_schema_version(version)
    ActiveRecord::Base.connection.update(<<-ESQL
UPDATE #{self.class.schema_info_table_name}
SET version = #{down? ? version.to_i - 1 : version.to_i}
WHERE plugin_name = '#{self.current_plugin.name}'
ESQL
    )
  end
end