Class: ActiveColumn::Migrator

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Migrator.



100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/active_column/migrator.rb', line 100

def initialize(direction, migrations_path, target_version = nil)
  cf_tasks = ActiveColumn.column_family_tasks
  sm_cf = self.class.schema_migrations_column_family

  unless cf_tasks.exists?(sm_cf)
    cf_tasks.create(sm_cf) do |cf|
      cf.comparator_type = 'LongType'
    end
  end

  @direction, @migrations_path, @target_version = direction, migrations_path, target_version
end

Class Method Details

.current_versionObject



76
77
78
79
80
81
82
83
# File 'lib/active_column/migrator.rb', line 76

def self.current_version
  sm_cf = schema_migrations_column_family
  if ActiveColumn.column_family_tasks.exists?(sm_cf)
    get_all_versions.max || 0
  else
    0
  end
end

.down(migrations_path, target_version = nil) ⇒ Object



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

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

.forward(migrations_path, steps = 1) ⇒ Object



47
48
49
# File 'lib/active_column/migrator.rb', line 47

def self.forward(migrations_path, steps = 1)
  move(:up, migrations_path, steps)
end

.get_all_versionsObject



71
72
73
74
# File 'lib/active_column/migrator.rb', line 71

def self.get_all_versions
  cas = ActiveColumn.connection
  cas.get(schema_migrations_column_family, 'all').map {|(name, _value)| name.to_i}.sort
end

.migrate(migrations_path, target_version = nil) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/active_column/migrator.rb', line 31

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

.migrations_pathObject



63
64
65
# File 'lib/active_column/migrator.rb', line 63

def self.migrations_path
  'ks/migrate'
end

.rollback(migrations_path, steps = 1) ⇒ Object



43
44
45
# File 'lib/active_column/migrator.rb', line 43

def self.rollback(migrations_path, steps = 1)
  move(:down, migrations_path, steps)
end

.run(direction, migrations_path, target_version) ⇒ Object



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

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

.schema_migrations_column_familyObject



67
68
69
# File 'lib/active_column/migrator.rb', line 67

def self.schema_migrations_column_family
  :schema_migrations
end

.up(migrations_path, target_version = nil) ⇒ Object



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

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

Instance Method Details

#current_migrationObject



117
118
119
# File 'lib/active_column/migrator.rb', line 117

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

#current_versionObject



113
114
115
# File 'lib/active_column/migrator.rb', line 113

def current_version
  migrated.last || 0
end

#migrateObject



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/active_column/migrator.rb', line 130

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|
    #puts "Migrating to #{migration.name} (#{migration.version})"

    # 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)
  end
end

#migratedObject



197
198
199
# File 'lib/active_column/migrator.rb', line 197

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

#migrationsObject



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/active_column/migrator.rb', line 162

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

      migration = MigrationProxy.new
      migration.name     = to_camel name
      migration.version  = version
      migration.filename = file
      klasses << migration
    end

    migrations = migrations.sort_by { |m| m.version }
    down? ? migrations.reverse : migrations
  end
end

#pending_migrationsObject



192
193
194
195
# File 'lib/active_column/migrator.rb', line 192

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

#runObject



121
122
123
124
125
126
127
128
# File 'lib/active_column/migrator.rb', line 121

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)
  end
end