Class: MongoMapper::Migrator

Inherits:
Object
  • Object
show all
Defined in:
lib/mongrations/migrator.rb

Overview

:nodoc:

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(direction, migrations_path, target_version = nil) ⇒ Migrator

Returns a new instance of Migrator.



50
51
52
53
# File 'lib/mongrations/migrator.rb', line 50

def initialize(direction, migrations_path, target_version = nil)
  target_version = target_version.to_i unless target_version.nil?
  @direction, @migrations_path, @target_version = direction, migrations_path, target_version
end

Class Method Details

.current_versionObject



41
42
43
# File 'lib/mongrations/migrator.rb', line 41

def current_version
  get_all_versions.max || 0
end

.down(migrations_path, target_version = nil) ⇒ Object



29
30
31
# File 'lib/mongrations/migrator.rb', line 29

def down(migrations_path, target_version = nil)
  self.new(:down, migrations_path, target_version).migrate
end

.get_all_versionsObject



37
38
39
# File 'lib/mongrations/migrator.rb', line 37

def get_all_versions
  MongoMapper::SchemaMigration.all.map { |sm| sm.version.to_i }.sort
end

.migrate(migrations_path, target_version = nil) ⇒ Object



4
5
6
7
8
9
10
11
12
13
# File 'lib/mongrations/migrator.rb', line 4

def migrate(migrations_path, target_version = nil)
  case
  when target_version.nil? then
    up(migrations_path, target_version)
  when current_version > target_version then
    down(migrations_path, target_version)
  else
    up(migrations_path, target_version)
  end
end

.proper_table_name(name) ⇒ Object



45
46
47
# File 'lib/mongrations/migrator.rb', line 45

def proper_table_name(name)
  name.table_name rescue name.to_s
end

.rollback(migrations_path, steps = 1) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/mongrations/migrator.rb', line 15

def rollback(migrations_path, steps=1)
  migrator    = self.new(:down, migrations_path)
  start_index = migrator.migrations.index(migrator.current_migration)

  return unless start_index

  finish = migrator.migrations[start_index + steps]
  down(migrations_path, finish ? finish.version : 0)
end

.run(direction, migrations_path, target_version) ⇒ Object



33
34
35
# File 'lib/mongrations/migrator.rb', line 33

def run(direction, migrations_path, target_version)
  self.new(direction, migrations_path, target_version).run
end

.up(migrations_path, target_version = nil) ⇒ Object



25
26
27
# File 'lib/mongrations/migrator.rb', line 25

def up(migrations_path, target_version = nil)
  self.new(:up, migrations_path, target_version).migrate
end

Instance Method Details

#current_migrationObject



59
60
61
# File 'lib/mongrations/migrator.rb', line 59

def current_migration
  migrations.detect { |m| m.version == current_version }
end

#current_versionObject



55
56
57
# File 'lib/mongrations/migrator.rb', line 55

def current_version
  migrated.last || 0
end

#migrateObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/mongrations/migrator.rb', line 72

def migrate
  current = migrations.detect { |m| m.version == current_version }
  target  = migrations.detect { |m| m.version == @target_version }

  if target.nil? && !@target_version.nil? && @target_version > 0
    raise UnknownMigrationVersionError.new(@target_version)
  end

  start    = up? ? 0 : (migrations.index(current) || 0)
  finish   = migrations.index(target) || migrations.size - 1
  runnable = migrations[start..finish]

  # skip the last migration if we're headed down, but not ALL the way down
  runnable.pop if down? && !target.nil?

  runnable.each do |migration|
    # On our way up, we skip migrating the ones we've already migrated
    next if up? && migrated.include?(migration.version.to_i)

    # On our way down, we skip reverting the ones we've never migrated
    if down? && !migrated.include?(migration.version.to_i)
      migration.announce 'never migrated, skipping'; migration.write
      next
    end

    migration.migrate(@direction)
    record_version_state_after_migrating(migration.version)
  end
end

#migratedObject



137
138
139
# File 'lib/mongrations/migrator.rb', line 137

def migrated
  @migrated_versions ||= self.class.get_all_versions
end

#migrationsObject



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/mongrations/migrator.rb', line 102

def migrations
  @migrations ||= begin
    files      = Dir["#{@migrations_path}/[0-9]*_*.rb"]

    migrations = files.inject([]) do |klasses, file|
      version, name = file.scan(/([0-9]+)_([_a-z0-9]*).rb/).first

      raise IllegalMigrationNameError.new(file) unless version
      version = version.to_i

      if klasses.detect { |m| m.version == version }
        raise DuplicateMigrationVersionError.new(version)
      end

      if klasses.detect { |m| m.name == name.camelize }
        raise DuplicateMigrationNameError.new(name.camelize)
      end

      klasses << MigrationProxy.new.tap do |migration|
        migration.name     = name.camelize
        migration.version  = version
        migration.filename = file
      end
    end

    migrations = migrations.sort_by(&:version)
    down? ? migrations.reverse : migrations
  end
end

#pending_migrationsObject



132
133
134
135
# File 'lib/mongrations/migrator.rb', line 132

def pending_migrations
  already_migrated = migrated
  migrations.reject { |m| already_migrated.include?(m.version.to_i) }
end

#runObject



63
64
65
66
67
68
69
70
# File 'lib/mongrations/migrator.rb', line 63

def run
  target = migrations.detect { |m| m.version == @target_version }
  raise UnknownMigrationVersionError.new(@target_version) if target.nil?
  unless (up? && migrated.include?(target.version.to_i)) || (down? && !migrated.include?(target.version.to_i))
    target.migrate(@direction)
    record_version_state_after_migrating(target.version)
  end
end