Chris@909
|
1 # The Plugin::Migrator class contains the logic to run migrations from
|
Chris@909
|
2 # within plugin directories. The directory in which a plugin's migrations
|
Chris@909
|
3 # should be is determined by the Plugin#migration_directory method.
|
Chris@909
|
4 #
|
Chris@909
|
5 # To migrate a plugin, you can simple call the migrate method (Plugin#migrate)
|
Chris@909
|
6 # with the version number that plugin should be at. The plugin's migrations
|
Chris@909
|
7 # will then be used to migrate up (or down) to the given version.
|
Chris@909
|
8 #
|
Chris@909
|
9 # For more information, see Engines::RailsExtensions::Migrations
|
Chris@909
|
10 class Engines::Plugin::Migrator < ActiveRecord::Migrator
|
Chris@909
|
11
|
Chris@909
|
12 # We need to be able to set the 'current' engine being migrated.
|
Chris@909
|
13 cattr_accessor :current_plugin
|
Chris@909
|
14
|
Chris@909
|
15 class << self
|
Chris@909
|
16 # Runs the migrations from a plugin, up (or down) to the version given
|
Chris@909
|
17 def migrate_plugin(plugin, version)
|
Chris@909
|
18 self.current_plugin = plugin
|
Chris@909
|
19 return if current_version(plugin) == version
|
Chris@909
|
20 migrate(plugin.migration_directory, version)
|
Chris@909
|
21 end
|
Chris@909
|
22
|
Chris@909
|
23 def current_version(plugin=current_plugin)
|
Chris@909
|
24 # Delete migrations that don't match .. to_i will work because the number comes first
|
Chris@909
|
25 ::ActiveRecord::Base.connection.select_values(
|
Chris@909
|
26 "SELECT version FROM #{schema_migrations_table_name}"
|
Chris@909
|
27 ).delete_if{ |v| v.match(/-#{plugin.name}/) == nil }.map(&:to_i).max || 0
|
Chris@909
|
28 end
|
Chris@909
|
29 end
|
Chris@909
|
30
|
Chris@909
|
31 def migrated
|
Chris@909
|
32 sm_table = self.class.schema_migrations_table_name
|
Chris@909
|
33 ::ActiveRecord::Base.connection.select_values(
|
Chris@909
|
34 "SELECT version FROM #{sm_table}"
|
Chris@909
|
35 ).delete_if{ |v| v.match(/-#{current_plugin.name}/) == nil }.map(&:to_i).sort
|
Chris@909
|
36 end
|
Chris@909
|
37
|
Chris@909
|
38 def record_version_state_after_migrating(version)
|
Chris@909
|
39 super(version.to_s + "-" + current_plugin.name)
|
Chris@909
|
40 end
|
Chris@909
|
41 end
|