Class: PluginAWeek::PluginMigrations::Migrator

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

Overview

Responsible for migrating plugins. current_plugin indicates which plugin is currently being migrated.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.current_versionObject

:nodoc:



20
21
22
# File 'lib/plugin_migrations/migrator.rb', line 20

def current_version #:nodoc:
  current_plugin.current_version
end

.migrate_plugin(plugin, version = nil) ⇒ Object

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



11
12
13
14
# File 'lib/plugin_migrations/migrator.rb', line 11

def migrate_plugin(plugin, version = nil)
  self.current_plugin = plugin
  migrate(plugin.migration_path, version)
end

.schema_info_table_nameObject

:nodoc:



16
17
18
# File 'lib/plugin_migrations/migrator.rb', line 16

def schema_info_table_name #:nodoc:
  ActiveRecord::Base.table_name_prefix + 'plugin_schema_info' + ActiveRecord::Base.table_name_suffix
end

Instance Method Details

#set_schema_version(version) ⇒ Object

Sets the version of the current plugin



26
27
28
29
30
31
32
33
34
35
# File 'lib/plugin_migrations/migrator.rb', line 26

def set_schema_version(version)
  version = down? ? version.to_i - 1 : version.to_i
  
  if ActiveRecord::Base.connection.select_one("SELECT version FROM #{self.class.schema_info_table_name} WHERE plugin_name = '#{current_plugin.name}'")
    ActiveRecord::Base.connection.update("UPDATE #{self.class.schema_info_table_name} SET version = #{version} WHERE plugin_name = '#{current_plugin.name}'")
  else
    # We need to create the entry since it doesn't exist
    ActiveRecord::Base.connection.execute("INSERT INTO #{self.class.schema_info_table_name} (version, plugin_name) VALUES (#{version},'#{current_plugin.name}')")
  end
end