Class: BigRecord::Migrator

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

Overview

:nodoc:

Class Method Summary

Instance Method Summary

Constructor Details

- (Migrator) initialize(direction, migrations_path, target_version = nil)

A new instance of Migrator

Raises:

  • (StandardError)


250
251
252
253
254
# File 'lib/big_record/migration.rb', line 250

def initialize(direction, migrations_path, target_version = nil)
  raise StandardError.new("This database does not yet support migrations") unless Base.connection.supports_migrations?
  Base.connection.initialize_schema_migrations_table
  @direction, @migrations_path, @target_version = direction, migrations_path, target_version
end

Class Method Details

+ (Object) current_version



235
236
237
238
239
240
241
242
# File 'lib/big_record/migration.rb', line 235

def current_version
  sm_table = schema_migrations_table_name
  if Base.connection.table_exists?(sm_table)
    get_all_versions.max || 0
  else
    0
  end
end

+ (Object) down(migrations_path, target_version = nil)



219
220
221
# File 'lib/big_record/migration.rb', line 219

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

+ (Object) get_all_versions



231
232
233
# File 'lib/big_record/migration.rb', line 231

def get_all_versions
  Base.connection.get_all_schema_versions.sort
end

+ (Object) migrate(migrations_path, target_version = nil)



197
198
199
200
201
202
203
# File 'lib/big_record/migration.rb', line 197

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

+ (Object) proper_table_name(name)



244
245
246
247
# File 'lib/big_record/migration.rb', line 244

def proper_table_name(name)
  # Use the Active Record objects own table_name, or pre/suffix from ActiveRecord::Base if name is a symbol/string
  name.table_name rescue "#{BigRecord::Base.table_name_prefix}#{name}#{BigRecord::Base.table_name_suffix}"
end

+ (Object) rollback(migrations_path, steps = 1)



205
206
207
208
209
210
211
212
213
# File 'lib/big_record/migration.rb', line 205

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

+ (Object) run(direction, migrations_path, target_version)



223
224
225
# File 'lib/big_record/migration.rb', line 223

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

+ (Object) schema_migrations_table_name



227
228
229
# File 'lib/big_record/migration.rb', line 227

def schema_migrations_table_name
  Base.table_name_prefix + 'schema_migrations' + Base.table_name_suffix
end

+ (Object) up(migrations_path, target_version = nil)



215
216
217
# File 'lib/big_record/migration.rb', line 215

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

Instance Method Details

- (Object) current_migration



260
261
262
# File 'lib/big_record/migration.rb', line 260

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

- (Object) current_version



256
257
258
# File 'lib/big_record/migration.rb', line 256

def current_version
  migrated.last || 0
end

- (Object) migrate



273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/big_record/migration.rb', line 273

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|
    Base.logger.info "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

    begin
      ddl_transaction do
        migration.migrate(@direction)
        record_version_state_after_migrating(migration.version)
      end
    rescue => e
      canceled_msg = Base.connection.supports_ddl_transactions? ? "this and " : ""
      raise StandardError, "An error has occurred, #{canceled_msg}all later migrations canceled:\n\n#{e}", e.backtrace
    end
  end
end

- (Object) migrated



347
348
349
# File 'lib/big_record/migration.rb', line 347

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

- (Object) migrations



312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# File 'lib/big_record/migration.rb', line 312

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     = name.camelize
      migration.version  = version
      migration.filename = file
      klasses << migration
    end

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

- (Object) pending_migrations



342
343
344
345
# File 'lib/big_record/migration.rb', line 342

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

- (Object) run



264
265
266
267
268
269
270
271
# File 'lib/big_record/migration.rb', line 264

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