Class: ActiveRecord::Migrator

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

Overview

:nodoc:

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Migrator.

Raises:

  • (StandardError)

1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
# File 'activerecord/lib/active_record/migration.rb', line 1099

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

  @direction         = direction
  @target_version    = target_version
  @migrated_versions = nil
  @migrations        = migrations

  validate(@migrations)

  Base.connection.initialize_schema_migrations_table
  Base.connection.
end

Class Attribute Details

.migrations_pathsObject


1054
1055
1056
1057
1058
# File 'activerecord/lib/active_record/migration.rb', line 1054

def migrations_paths
  @migrations_paths ||= ['db/migrate']  # just to not break things if someone uses: migrations_path = some_string

  Array(@migrations_paths)
end

Class Method Details

.any_migrations?Boolean

Returns:

  • (Boolean)

1046
1047
1048
# File 'activerecord/lib/active_record/migration.rb', line 1046

def any_migrations?
  migrations(migrations_paths).any?
end

.current_version(connection = Base.connection) ⇒ Object


1038
1039
1040
# File 'activerecord/lib/active_record/migration.rb', line 1038

def current_version(connection = Base.connection)
  get_all_versions(connection).max || 0
end

.down(migrations_paths, target_version = nil) ⇒ Object


1009
1010
1011
1012
1013
1014
# File 'activerecord/lib/active_record/migration.rb', line 1009

def down(migrations_paths, target_version = nil)
  migrations = migrations(migrations_paths)
  migrations.select! { |m| yield m } if block_given?

  new(:down, migrations, target_version).migrate
end

.forward(migrations_paths, steps = 1) ⇒ Object


998
999
1000
# File 'activerecord/lib/active_record/migration.rb', line 998

def forward(migrations_paths, steps=1)
  move(:up, migrations_paths, steps)
end

.get_all_versions(connection = Base.connection) ⇒ Object


1028
1029
1030
1031
1032
1033
1034
1035
1036
# File 'activerecord/lib/active_record/migration.rb', line 1028

def get_all_versions(connection = Base.connection)
  ActiveSupport::Deprecation.silence do
    if connection.table_exists?(schema_migrations_table_name)
      SchemaMigration.all.map { |x| x.version.to_i }.sort
    else
      []
    end
  end
end

.last_migrationObject

:nodoc:


1050
1051
1052
# File 'activerecord/lib/active_record/migration.rb', line 1050

def last_migration #:nodoc:
  migrations(migrations_paths).last || NullMigration.new
end

.match_to_migration_filename?(filename) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)

1060
1061
1062
# File 'activerecord/lib/active_record/migration.rb', line 1060

def match_to_migration_filename?(filename) # :nodoc:
  File.basename(filename) =~ Migration::MigrationFilenameRegexp
end

.migrate(migrations_paths, target_version = nil, &block) ⇒ Object


981
982
983
984
985
986
987
988
989
990
991
992
# File 'activerecord/lib/active_record/migration.rb', line 981

def migrate(migrations_paths, target_version = nil, &block)
  case
  when target_version.nil?
    up(migrations_paths, target_version, &block)
  when current_version == 0 && target_version == 0
    []
  when current_version > target_version
    down(migrations_paths, target_version, &block)
  else
    up(migrations_paths, target_version, &block)
  end
end

.migrations(paths) ⇒ Object


1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
# File 'activerecord/lib/active_record/migration.rb', line 1068

def migrations(paths)
  paths = Array(paths)

  files = Dir[*paths.map { |p| "#{p}/**/[0-9]*_*.rb" }]

  migrations = files.map do |file|
    version, name, scope = parse_migration_filename(file)
    raise IllegalMigrationNameError.new(file) unless version
    version = version.to_i
    name = name.camelize

    MigrationProxy.new(name, version, file, scope)
  end

  migrations.sort_by(&:version)
end

.needs_migration?(connection = Base.connection) ⇒ Boolean

Returns:

  • (Boolean)

1042
1043
1044
# File 'activerecord/lib/active_record/migration.rb', line 1042

def needs_migration?(connection = Base.connection)
  (migrations(migrations_paths).collect(&:version) - get_all_versions(connection)).size > 0
end

.open(migrations_paths) ⇒ Object


1020
1021
1022
# File 'activerecord/lib/active_record/migration.rb', line 1020

def open(migrations_paths)
  new(:up, migrations(migrations_paths), nil)
end

.parse_migration_filename(filename) ⇒ Object

:nodoc:


1064
1065
1066
# File 'activerecord/lib/active_record/migration.rb', line 1064

def parse_migration_filename(filename) # :nodoc:
  File.basename(filename).scan(Migration::MigrationFilenameRegexp).first
end

.rollback(migrations_paths, steps = 1) ⇒ Object


994
995
996
# File 'activerecord/lib/active_record/migration.rb', line 994

def rollback(migrations_paths, steps=1)
  move(:down, migrations_paths, steps)
end

.run(direction, migrations_paths, target_version) ⇒ Object


1016
1017
1018
# File 'activerecord/lib/active_record/migration.rb', line 1016

def run(direction, migrations_paths, target_version)
  new(direction, migrations(migrations_paths), target_version).run
end

.schema_migrations_table_nameObject


1024
1025
1026
# File 'activerecord/lib/active_record/migration.rb', line 1024

def schema_migrations_table_name
  SchemaMigration.table_name
end

.up(migrations_paths, target_version = nil) ⇒ Object


1002
1003
1004
1005
1006
1007
# File 'activerecord/lib/active_record/migration.rb', line 1002

def up(migrations_paths, target_version = nil)
  migrations = migrations(migrations_paths)
  migrations.select! { |m| yield m } if block_given?

  new(:up, migrations, target_version).migrate
end

Instance Method Details

#current_migrationObject Also known as: current


1117
1118
1119
# File 'activerecord/lib/active_record/migration.rb', line 1117

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

#current_versionObject


1113
1114
1115
# File 'activerecord/lib/active_record/migration.rb', line 1113

def current_version
  migrated.max || 0
end

#load_migratedObject


1162
1163
1164
# File 'activerecord/lib/active_record/migration.rb', line 1162

def load_migrated
  @migrated_versions = Set.new(self.class.get_all_versions)
end

#migrateObject


1130
1131
1132
1133
1134
1135
1136
# File 'activerecord/lib/active_record/migration.rb', line 1130

def migrate
  if use_advisory_lock?
    with_advisory_lock { migrate_without_lock }
  else
    migrate_without_lock
  end
end

#migratedObject


1158
1159
1160
# File 'activerecord/lib/active_record/migration.rb', line 1158

def migrated
  @migrated_versions || load_migrated
end

#migrationsObject


1149
1150
1151
# File 'activerecord/lib/active_record/migration.rb', line 1149

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

#pending_migrationsObject


1153
1154
1155
1156
# File 'activerecord/lib/active_record/migration.rb', line 1153

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

#runObject


1122
1123
1124
1125
1126
1127
1128
# File 'activerecord/lib/active_record/migration.rb', line 1122

def run
  if use_advisory_lock?
    with_advisory_lock { run_without_lock }
  else
    run_without_lock
  end
end

#runnableObject


1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
# File 'activerecord/lib/active_record/migration.rb', line 1138

def runnable
  runnable = migrations[start..finish]
  if up?
    runnable.reject { |m| ran?(m) }
  else
    # skip the last migration if we're headed down, but not ALL the way down
    runnable.pop if target
    runnable.find_all { |m| ran?(m) }
  end
end