Class: ActiveRecord::MigrationContext

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

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(migrations_paths, schema_migration) ⇒ MigrationContext

Returns a new instance of MigrationContext.


1026
1027
1028
1029
# File 'activerecord/lib/active_record/migration.rb', line 1026

def initialize(migrations_paths, schema_migration)
  @migrations_paths = migrations_paths
  @schema_migration = schema_migration
end

Instance Attribute Details

#migrations_pathsObject (readonly)

Returns the value of attribute migrations_paths


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

def migrations_paths
  @migrations_paths
end

#schema_migrationObject (readonly)

Returns the value of attribute schema_migration


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

def schema_migration
  @schema_migration
end

Instance Method Details

#any_migrations?Boolean

Returns:

  • (Boolean)

1097
1098
1099
# File 'activerecord/lib/active_record/migration.rb', line 1097

def any_migrations?
  migrations.any?
end

#current_environmentObject


1132
1133
1134
# File 'activerecord/lib/active_record/migration.rb', line 1132

def current_environment
  ActiveRecord::ConnectionHandling::DEFAULT_ENV.call
end

#current_versionObject


1088
1089
1090
1091
# File 'activerecord/lib/active_record/migration.rb', line 1088

def current_version
  get_all_versions.max || 0
rescue ActiveRecord::NoDatabaseError
end

#down(target_version = nil) ⇒ Object


1062
1063
1064
1065
1066
1067
1068
1069
1070
# File 'activerecord/lib/active_record/migration.rb', line 1062

def down(target_version = nil)
  selected_migrations = if block_given?
    migrations.select { |m| yield m }
  else
    migrations
  end

  Migrator.new(:down, selected_migrations, schema_migration, target_version).migrate
end

#forward(steps = 1) ⇒ Object


1048
1049
1050
# File 'activerecord/lib/active_record/migration.rb', line 1048

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

#get_all_versionsObject


1080
1081
1082
1083
1084
1085
1086
# File 'activerecord/lib/active_record/migration.rb', line 1080

def get_all_versions
  if schema_migration.table_exists?
    schema_migration.all_versions.map(&:to_i)
  else
    []
  end
end

#last_stored_environmentObject


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

def last_stored_environment
  return nil if current_version == 0
  raise NoEnvironmentInSchemaError unless ActiveRecord::InternalMetadata.table_exists?

  environment = ActiveRecord::InternalMetadata[:environment]
  raise NoEnvironmentInSchemaError unless environment
  environment
end

#migrate(target_version = nil, &block) ⇒ Object


1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
# File 'activerecord/lib/active_record/migration.rb', line 1031

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

#migrationsObject


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

def migrations
  migrations = migration_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

#migrations_statusObject


1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
# File 'activerecord/lib/active_record/migration.rb', line 1114

def migrations_status
  db_list = schema_migration.normalized_versions

  file_list = migration_files.map do |file|
    version, name, scope = parse_migration_filename(file)
    raise IllegalMigrationNameError.new(file) unless version
    version = schema_migration.normalize_migration_number(version)
    status = db_list.delete(version) ? "up" : "down"
    [status, version, (name + scope).humanize]
  end.compact

  db_list.map! do |version|
    ["up", version, "********** NO FILE **********"]
  end

  (db_list + file_list).sort_by { |_, version, _| version }
end

#needs_migration?Boolean

Returns:

  • (Boolean)

1093
1094
1095
# File 'activerecord/lib/active_record/migration.rb', line 1093

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

#openObject


1076
1077
1078
# File 'activerecord/lib/active_record/migration.rb', line 1076

def open
  Migrator.new(:up, migrations, schema_migration)
end

#protected_environment?Boolean

Returns:

  • (Boolean)

1136
1137
1138
# File 'activerecord/lib/active_record/migration.rb', line 1136

def protected_environment?
  ActiveRecord::Base.protected_environments.include?(last_stored_environment) if last_stored_environment
end

#rollback(steps = 1) ⇒ Object


1044
1045
1046
# File 'activerecord/lib/active_record/migration.rb', line 1044

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

#run(direction, target_version) ⇒ Object


1072
1073
1074
# File 'activerecord/lib/active_record/migration.rb', line 1072

def run(direction, target_version)
  Migrator.new(direction, migrations, schema_migration, target_version).run
end

#up(target_version = nil) ⇒ Object


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

def up(target_version = nil)
  selected_migrations = if block_given?
    migrations.select { |m| yield m }
  else
    migrations
  end

  Migrator.new(:up, selected_migrations, schema_migration, target_version).migrate
end