Class: ActiveRecord::MigrationContext

Inherits:
Object
  • Object
show all
Defined in:
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/migration.rb

Overview

MigrationContext sets the context in which a migration is run.

A migration context requires the path to the migrations is set in the migrations_paths parameter. Optionally a schema_migration class can be provided. For most applications, SchemaMigration is sufficient. Multiple database applications need a SchemaMigration per primary database.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(migrations_paths, schema_migration = SchemaMigration) ⇒ MigrationContext

Returns a new instance of MigrationContext.



1071
1072
1073
1074
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/migration.rb', line 1071

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

Instance Attribute Details

#migrations_pathsObject (readonly)

Returns the value of attribute migrations_paths.



1069
1070
1071
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/migration.rb', line 1069

def migrations_paths
  @migrations_paths
end

#schema_migrationObject (readonly)

Returns the value of attribute schema_migration.



1069
1070
1071
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/migration.rb', line 1069

def schema_migration
  @schema_migration
end

Instance Method Details

#current_environmentObject

:nodoc:



1190
1191
1192
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/migration.rb', line 1190

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

#current_versionObject

:nodoc:



1146
1147
1148
1149
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/migration.rb', line 1146

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

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

:nodoc:



1120
1121
1122
1123
1124
1125
1126
1127
1128
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/migration.rb', line 1120

def down(target_version = nil, &block) # :nodoc:
  selected_migrations = if block_given?
    migrations.select(&block)
  else
    migrations
  end

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

#forward(steps = 1) ⇒ Object

:nodoc:



1106
1107
1108
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/migration.rb', line 1106

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

#get_all_versionsObject

:nodoc:



1138
1139
1140
1141
1142
1143
1144
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/migration.rb', line 1138

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

#last_stored_environmentObject

:nodoc:



1198
1199
1200
1201
1202
1203
1204
1205
1206
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/migration.rb', line 1198

def last_stored_environment # :nodoc:
  return nil unless ActiveRecord::InternalMetadata.enabled?
  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

Runs the migrations in the migrations_path.

If target_version is nil, migrate will run up.

If the current_version and target_version are both 0 then an empty array will be returned and no migrations will be run.

If the current_version in the schema is greater than the target_version, then down will be run.

If none of the conditions are met, up will be run with the target_version.



1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/migration.rb', line 1089

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

:nodoc:



1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/migration.rb', line 1159

def migrations # :nodoc:
  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

:nodoc:



1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/migration.rb', line 1172

def migrations_status # :nodoc:
  db_list = schema_migration.normalized_versions

  file_list = migration_files.filter_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

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

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

#needs_migration?Boolean

:nodoc:

Returns:

  • (Boolean)


1151
1152
1153
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/migration.rb', line 1151

def needs_migration? # :nodoc:
  pending_migration_versions.size > 0
end

#openObject

:nodoc:



1134
1135
1136
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/migration.rb', line 1134

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

#pending_migration_versionsObject

:nodoc:



1155
1156
1157
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/migration.rb', line 1155

def pending_migration_versions # :nodoc:
  migrations.collect(&:version) - get_all_versions
end

#protected_environment?Boolean

:nodoc:

Returns:

  • (Boolean)


1194
1195
1196
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/migration.rb', line 1194

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

#rollback(steps = 1) ⇒ Object

:nodoc:



1102
1103
1104
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/migration.rb', line 1102

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

#run(direction, target_version) ⇒ Object

:nodoc:



1130
1131
1132
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/migration.rb', line 1130

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

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

:nodoc:



1110
1111
1112
1113
1114
1115
1116
1117
1118
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/migration.rb', line 1110

def up(target_version = nil, &block) # :nodoc:
  selected_migrations = if block_given?
    migrations.select(&block)
  else
    migrations
  end

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