Class: Desert::PluginMigrations::Migrator

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

Overview

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.current_versionObject

:nodoc:



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/desert/plugin_migrations/1.2/migrator.rb', line 5

def current_version #:nodoc:
  if legacy_schema_table_exists?
    result = ActiveRecord::Base.connection.select_one("SELECT version FROM #{schema_info_table_name} WHERE plugin_name = '#{current_plugin.name}'")
  else
    result = nil
  end
  if result
    result['version'].to_i
  else
    # There probably isn't an entry for this plugin in the migration info table.
    0
  end
end

.get_all_versionsObject



15
16
17
# File 'lib/desert/plugin_migrations/2.1/migrator.rb', line 15

def get_all_versions
  ActiveRecord::Base.connection.select_values("SELECT version FROM #{schema_migrations_table_name} where plugin_name='#{current_plugin.name}'").map(&:to_i).sort
end

.legacy_schema_table_exists?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/desert/plugin_migrations/migrator.rb', line 27

def legacy_schema_table_exists?
  ActiveRecord::Base.connection.tables.include? schema_info_table_name
end

.migrate_plugin(plugin, version = nil) ⇒ Object

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



11
12
13
14
15
16
17
# File 'lib/desert/plugin_migrations/migrator.rb', line 11

def migrate_plugin(plugin, version = nil)
  self.current_plugin = plugin
  if ActiveRecord::Base.connection.respond_to?(:initialize_schema_migrations_table)
    ActiveRecord::Base.connection.initialize_schema_migrations_table
  end
  migrate(plugin.migration_path, version)
end

.schema_info_table_nameObject

:nodoc:



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

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

.schema_migrations_table_nameObject



23
24
25
# File 'lib/desert/plugin_migrations/migrator.rb', line 23

def schema_migrations_table_name
  ActiveRecord::Base.table_name_prefix + 'plugin_schema_migrations' + ActiveRecord::Base.table_name_suffix
end

Instance Method Details

#migratedObject



33
34
35
36
# File 'lib/desert/plugin_migrations/1.2/migrator.rb', line 33

def migrated
  current_plugin_version = self.class.current_version
  (1..current_plugin_version).to_a
end

#record_version_state_after_migrating(version) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/desert/plugin_migrations/2.1/migrator.rb', line 20

def record_version_state_after_migrating(version)
  sm_table = self.class.schema_migrations_table_name

  if down?
    ActiveRecord::Base.connection.update("DELETE FROM #{sm_table} WHERE version = '#{version}' AND plugin_name = '#{current_plugin.name}'")
  else
    ActiveRecord::Base.connection.insert("INSERT INTO #{sm_table} (plugin_name, version) VALUES ('#{current_plugin.name}', '#{version}')")
  end
end

#set_schema_version(version) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/desert/plugin_migrations/1.2/migrator.rb', line 20

def set_schema_version(version)
  version = down? ? version.to_i - 1 : version.to_i

  if self.class.legacy_schema_table_exists?
    if ActiveRecord::Base.connection.select_one("SELECT version FROM #{self.class.schema_info_table_name} WHERE plugin_name = '#{current_plugin.name}'").nil?
      # 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}')")
    else
      ActiveRecord::Base.connection.update("UPDATE #{self.class.schema_info_table_name} SET version = #{version} WHERE plugin_name = '#{current_plugin.name}'")
    end
  end
end