Class: ActiveRecord::Migrator

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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



32
33
34
35
# File 'lib/active_record/migration.rb', line 32

def initialize(direction, migrations_path, target_version = nil)
  @direction, @migrations_path, @target_version = direction, migrations_path, target_version
  Base.connection.initialize_schema_information
end

Class Method Details

.current_versionObject



27
28
29
# File 'lib/active_record/migration.rb', line 27

def current_version
  Base.connection.select_one("SELECT version FROM schema_info")["version"].to_i
end

.down(migrations_path, target_version = nil) ⇒ Object



23
24
25
# File 'lib/active_record/migration.rb', line 23

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

.up(migrations_path, target_version = nil) ⇒ Object



19
20
21
# File 'lib/active_record/migration.rb', line 19

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

Instance Method Details

#current_versionObject



37
38
39
# File 'lib/active_record/migration.rb', line 37

def current_version
  self.class.current_version
end

#migrateObject



41
42
43
44
45
46
47
48
49
50
# File 'lib/active_record/migration.rb', line 41

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

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