Class: Themigrator::Migration

Inherits:
Object
  • Object
show all
Defined in:
lib/themigrator/migration.rb

Overview

Holds the plan of the migration

Constant Summary collapse

ACTIONS =
%w(setup pre-migrate migrate post-migrate cleanup).freeze
SCRIPTS =
ACTIONS | %w(rollback)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dir, options = {}) ⇒ Migration

Returns a new instance of Migration.



11
12
13
14
15
16
17
# File 'lib/themigrator/migration.rb', line 11

def initialize(dir, options = {})
  @dir = dir
  @action_and_roles = Hash.new { |hash, key| hash[key] = [] }
  @roles = []
  @user_roles = options[:roles] || []
  @actions = []
end

Instance Attribute Details

#actionsObject (readonly)

Returns the value of attribute actions.



9
10
11
# File 'lib/themigrator/migration.rb', line 9

def actions
  @actions
end

#rolesObject (readonly)

Returns the value of attribute roles.



9
10
11
# File 'lib/themigrator/migration.rb', line 9

def roles
  @roles
end

Instance Method Details

#analyze_project!Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/themigrator/migration.rb', line 19

def analyze_project!
  roles = Set.new
  used_actions = [] # Should be in order

  SCRIPTS.each do |action|
    path_match = File.join(@dir, "*/#{action}")
    Dir[path_match].select do |f|
      File.executable?(f)
    end.each do |f|
      role = File.basename(File.dirname(f))
      next unless @user_roles.none? || @user_roles.include?(role)
      roles.add(role)
      used_actions << action
      @action_and_roles[action] << role
    end
  end
  @roles = roles.to_a.sort
  @actions = used_actions.uniq
  @actions.delete('rollback')
end

#roles_for_action(action) ⇒ Object



40
41
42
# File 'lib/themigrator/migration.rb', line 40

def roles_for_action(action)
  @action_and_roles[action]
end