Class: ActiveRecord::Migrator
- Inherits:
-
Object
- Object
- ActiveRecord::Migrator
- Defined in:
- lib/active_record/migration.rb
Overview
:nodoc:
Class Method Summary collapse
- .current_version ⇒ Object
- .down(migrations_path, target_version = nil) ⇒ Object
- .migrate(migrations_path, target_version = nil) ⇒ Object
- .proper_table_name(name) ⇒ Object
- .schema_info_table_name ⇒ Object
- .up(migrations_path, target_version = nil) ⇒ Object
Instance Method Summary collapse
- #current_version ⇒ Object
-
#initialize(direction, migrations_path, target_version = nil) ⇒ Migrator
constructor
A new instance of Migrator.
- #migrate ⇒ Object
Constructor Details
#initialize(direction, migrations_path, target_version = nil) ⇒ Migrator
Returns a new instance of Migrator.
319 320 321 322 323 |
# File 'lib/active_record/migration.rb', line 319 def initialize(direction, migrations_path, target_version = nil) raise StandardError.new("This database does not yet support migrations") unless Base.connection.supports_migrations? @direction, @migrations_path, @target_version = direction, migrations_path, target_version Base.connection.initialize_schema_information end |
Class Method Details
.current_version ⇒ Object
308 309 310 |
# File 'lib/active_record/migration.rb', line 308 def current_version (Base.connection.select_one("SELECT version FROM #{schema_info_table_name}") || {"version" => 0})["version"].to_i end |
.down(migrations_path, target_version = nil) ⇒ Object
300 301 302 |
# File 'lib/active_record/migration.rb', line 300 def down(migrations_path, target_version = nil) self.new(:down, migrations_path, target_version).migrate end |
.migrate(migrations_path, target_version = nil) ⇒ Object
283 284 285 286 287 288 289 290 291 292 293 294 |
# File 'lib/active_record/migration.rb', line 283 def migrate(migrations_path, target_version = nil) Base.connection.initialize_schema_information case when target_version.nil?, current_version < target_version up(migrations_path, target_version) when current_version > target_version down(migrations_path, target_version) when current_version == target_version return # You're on the right version end end |
.proper_table_name(name) ⇒ Object
312 313 314 315 |
# File 'lib/active_record/migration.rb', line 312 def proper_table_name(name) # Use the ActiveRecord objects own table_name, or pre/suffix from ActiveRecord::Base if name is a symbol/string name.table_name rescue "#{ActiveRecord::Base.table_name_prefix}#{name}#{ActiveRecord::Base.table_name_suffix}" end |
.schema_info_table_name ⇒ Object
304 305 306 |
# File 'lib/active_record/migration.rb', line 304 def schema_info_table_name Base.table_name_prefix + "schema_info" + Base.table_name_suffix end |
.up(migrations_path, target_version = nil) ⇒ Object
296 297 298 |
# File 'lib/active_record/migration.rb', line 296 def up(migrations_path, target_version = nil) self.new(:up, migrations_path, target_version).migrate end |
Instance Method Details
#current_version ⇒ Object
325 326 327 |
# File 'lib/active_record/migration.rb', line 325 def current_version self.class.current_version end |
#migrate ⇒ Object
329 330 331 332 333 334 335 336 337 338 |
# File 'lib/active_record/migration.rb', line 329 def migrate migration_classes.each 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.migrate(@direction) set_schema_version(version) end end |