Class: Engines::Plugin::Migrator

Inherits:
ActiveRecord::Migrator
  • Object
show all
Defined in:
lib/engines/plugin/migrator.rb

Overview

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 Method Summary collapse

Instance Method Summary collapse

Class Method Details

.current_version(plugin = current_plugin) ⇒ Object

Returns the current version of the given plugin



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/engines/plugin/migrator.rb', line 35

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

.migrate_plugin(plugin, version) ⇒ Object

Runs the migrations from a plugin, up (or down) to the version given



16
17
18
19
20
21
22
23
24
# File 'lib/engines/plugin/migrator.rb', line 16

def self.migrate_plugin(plugin, version)
  self.current_plugin = plugin
  # There seems to be a bug in Rails' own migrations, where migrating
  # to the existing version causes all migrations to be run where that
  # migration number doesn't exist (i.e. zero). We could fix this by
  # removing the line if the version hits zero...?
  return if current_version(plugin) == version
  migrate(plugin.migration_directory, version)
end

.schema_info_table_nameObject

Returns the name of the table used to store schema information about installed plugins.

See Engines.schema_info_table for more details.



30
31
32
# File 'lib/engines/plugin/migrator.rb', line 30

def self.schema_info_table_name
  proper_table_name Engines.schema_info_table
end

Instance Method Details

#migrated(plugin = current_plugin) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/engines/plugin/migrator.rb', line 55

def migrated(plugin=current_plugin)
  ActiveRecord::Base.connection.select_values(<<-ESQL
    SELECT version FROM #{self.class.schema_info_table_name}
    WHERE plugin_name = '#{plugin.name}'
  ESQL
  ).map(&:to_i).sort - [0]
end

#record_version_state_after_migrating(version) ⇒ Object

Sets the version of the plugin in Engines::Plugin::Migrator.current_plugin to the given version.



65
66
67
68
69
70
71
72
# File 'lib/engines/plugin/migrator.rb', line 65

def record_version_state_after_migrating(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