Class: ActiveRecord::Migrator

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record/migration.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.

Raises:

  • (StandardError)


328
329
330
331
332
# File 'lib/active_record/migration.rb', line 328

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

Class Method Details

.current_versionObject



318
319
320
# File 'lib/active_record/migration.rb', line 318

def current_version
  Base.connection.select_value("SELECT version FROM #{schema_info_table_name}").to_i
end

.down(migrations_path, target_version = nil) ⇒ Object



310
311
312
# File 'lib/active_record/migration.rb', line 310

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

.migrate(migrations_path, target_version = nil) ⇒ Object



293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/active_record/migration.rb', line 293

def migrate(migrations_path, target_version = nil)
  Base.connection.initialize_schema_information

  case
    when target_version.nil?, current_version < target_version
      up(migrations_path, target_version)
    when current_version > target_version
      down(migrations_path, target_version)
    when current_version == target_version
      return # You're on the right version
  end
end

.proper_table_name(name) ⇒ Object



322
323
324
325
# File 'lib/active_record/migration.rb', line 322

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

.schema_info_table_nameObject



314
315
316
# File 'lib/active_record/migration.rb', line 314

def schema_info_table_name
  Base.table_name_prefix + "schema_info" + Base.table_name_suffix
end

.up(migrations_path, target_version = nil) ⇒ Object



306
307
308
# File 'lib/active_record/migration.rb', line 306

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

Instance Method Details

#current_versionObject



334
335
336
# File 'lib/active_record/migration.rb', line 334

def current_version
  self.class.current_version
end

#migrateObject



338
339
340
341
342
343
344
345
346
347
348
349
350
351
# File 'lib/active_record/migration.rb', line 338

def migrate
  migration_classes.each do |migration_class|
    if reached_target_version?(migration_class.version)
      Base.logger.info("Reached target version: #{@target_version}")
      break
    end

    next if irrelevant_migration?(migration_class.version)

    Base.logger.info "Migrating to #{migration_class} (#{migration_class.version})"
    migration_class.migrate(@direction)
    set_schema_version(migration_class.version)
  end
end

#pending_migrationsObject



353
354
355
# File 'lib/active_record/migration.rb', line 353

def pending_migrations
  migration_classes.select { |m| m.version > current_version }
end