Class: ActiveRecord::MigrationContext

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



1028
1029
1030
1031
# File 'lib/active_record/migration.rb', line 1028

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.



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

def migrations_paths
  @migrations_paths
end

#schema_migrationObject (readonly)

Returns the value of attribute schema_migration.



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

def schema_migration
  @schema_migration
end

Instance Method Details

#any_migrations?Boolean

Returns:

  • (Boolean)


1099
1100
1101
# File 'lib/active_record/migration.rb', line 1099

def any_migrations?
  migrations.any?
end

#current_environmentObject



1138
1139
1140
# File 'lib/active_record/migration.rb', line 1138

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

#current_versionObject



1090
1091
1092
1093
# File 'lib/active_record/migration.rb', line 1090

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

#down(target_version = nil) ⇒ Object



1064
1065
1066
1067
1068
1069
1070
1071
1072
# File 'lib/active_record/migration.rb', line 1064

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



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

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

#get_all_versionsObject



1082
1083
1084
1085
1086
1087
1088
# File 'lib/active_record/migration.rb', line 1082

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

#last_migrationObject

:nodoc:



1103
1104
1105
# File 'lib/active_record/migration.rb', line 1103

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

#last_stored_environmentObject



1146
1147
1148
1149
1150
1151
1152
1153
# File 'lib/active_record/migration.rb', line 1146

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



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

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



1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
# File 'lib/active_record/migration.rb', line 1107

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



1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
# File 'lib/active_record/migration.rb', line 1120

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)


1095
1096
1097
# File 'lib/active_record/migration.rb', line 1095

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

#openObject



1078
1079
1080
# File 'lib/active_record/migration.rb', line 1078

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

#protected_environment?Boolean

Returns:

  • (Boolean)


1142
1143
1144
# File 'lib/active_record/migration.rb', line 1142

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

#rollback(steps = 1) ⇒ Object



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

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

#run(direction, target_version) ⇒ Object



1074
1075
1076
# File 'lib/active_record/migration.rb', line 1074

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

#up(target_version = nil) ⇒ Object



1054
1055
1056
1057
1058
1059
1060
1061
1062
# File 'lib/active_record/migration.rb', line 1054

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