Class: ActiveRecord::Migrator
- Inherits:
-
Object
- Object
- ActiveRecord::Migrator
- Defined in:
- activerecord/lib/active_record/migration.rb
Overview
:nodoc:
Class Method Summary (collapse)
- + (Object) current_version
- + (Object) down(migrations_path, target_version = nil)
- + (Object) forward(migrations_path, steps = 1)
- + (Object) get_all_versions
- + (Object) migrate(migrations_path, target_version = nil)
- + (Object) migrations_path
- + (Object) proper_table_name(name)
- + (Object) rollback(migrations_path, steps = 1)
- + (Object) run(direction, migrations_path, target_version)
- + (Object) schema_migrations_table_name
- + (Object) up(migrations_path, target_version = nil)
Instance Method Summary (collapse)
- - (Object) current_migration
- - (Object) current_version
-
- (Migrator) initialize(direction, migrations_path, target_version = nil)
constructor
A new instance of Migrator.
- - (Object) migrate
- - (Object) migrated
- - (Object) migrations
- - (Object) pending_migrations
- - (Object) run
Constructor Details
- (Migrator) initialize(direction, migrations_path, target_version = nil)
A new instance of Migrator
485 486 487 488 489 |
# File 'activerecord/lib/active_record/migration.rb', line 485 def initialize(direction, migrations_path, target_version = nil) raise StandardError.new("This database does not yet support migrations") unless Base.connection.supports_migrations? Base.connection.initialize_schema_migrations_table @direction, @migrations_path, @target_version = direction, migrations_path, target_version end |
Class Method Details
+ (Object) current_version
457 458 459 460 461 462 463 464 |
# File 'activerecord/lib/active_record/migration.rb', line 457 def current_version sm_table = schema_migrations_table_name if Base.connection.table_exists?(sm_table) get_all_versions.max || 0 else 0 end end |
+ (Object) down(migrations_path, target_version = nil)
436 437 438 |
# File 'activerecord/lib/active_record/migration.rb', line 436 def down(migrations_path, target_version = nil) self.new(:down, migrations_path, target_version).migrate end |
+ (Object) forward(migrations_path, steps = 1)
428 429 430 |
# File 'activerecord/lib/active_record/migration.rb', line 428 def forward(migrations_path, steps=1) move(:up, migrations_path, steps) end |
+ (Object) get_all_versions
452 453 454 455 |
# File 'activerecord/lib/active_record/migration.rb', line 452 def get_all_versions table = Arel::Table.new(schema_migrations_table_name) Base.connection.select_values(table.project(table['version']).to_sql).map{ |v| v.to_i }.sort end |
+ (Object) migrate(migrations_path, target_version = nil)
412 413 414 415 416 417 418 419 420 421 422 |
# File 'activerecord/lib/active_record/migration.rb', line 412 def migrate(migrations_path, target_version = nil) case when target_version.nil? up(migrations_path, target_version) when current_version == 0 && target_version == 0 when current_version > target_version down(migrations_path, target_version) else up(migrations_path, target_version) end end |
+ (Object) migrations_path
444 445 446 |
# File 'activerecord/lib/active_record/migration.rb', line 444 def migrations_path 'db/migrate' end |
+ (Object) proper_table_name(name)
466 467 468 469 |
# File 'activerecord/lib/active_record/migration.rb', line 466 def proper_table_name(name) # Use the Active Record 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 |
+ (Object) rollback(migrations_path, steps = 1)
424 425 426 |
# File 'activerecord/lib/active_record/migration.rb', line 424 def rollback(migrations_path, steps=1) move(:down, migrations_path, steps) end |
+ (Object) run(direction, migrations_path, target_version)
440 441 442 |
# File 'activerecord/lib/active_record/migration.rb', line 440 def run(direction, migrations_path, target_version) self.new(direction, migrations_path, target_version).run end |
+ (Object) schema_migrations_table_name
448 449 450 |
# File 'activerecord/lib/active_record/migration.rb', line 448 def schema_migrations_table_name Base.table_name_prefix + 'schema_migrations' + Base.table_name_suffix end |
+ (Object) up(migrations_path, target_version = nil)
432 433 434 |
# File 'activerecord/lib/active_record/migration.rb', line 432 def up(migrations_path, target_version = nil) self.new(:up, migrations_path, target_version).migrate end |
Instance Method Details
- (Object) current_migration
495 496 497 |
# File 'activerecord/lib/active_record/migration.rb', line 495 def current_migration migrations.detect { |m| m.version == current_version } end |
- (Object) current_version
491 492 493 |
# File 'activerecord/lib/active_record/migration.rb', line 491 def current_version migrated.last || 0 end |
- (Object) migrate
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 |
# File 'activerecord/lib/active_record/migration.rb', line 508 def migrate current = migrations.detect { |m| m.version == current_version } target = migrations.detect { |m| m.version == @target_version } if target.nil? && !@target_version.nil? && @target_version > 0 raise UnknownMigrationVersionError.new(@target_version) end start = up? ? 0 : (migrations.index(current) || 0) finish = migrations.index(target) || migrations.size - 1 runnable = migrations[start..finish] # skip the last migration if we're headed down, but not ALL the way down runnable.pop if down? && !target.nil? runnable.each do |migration| Base.logger.info "Migrating to #{migration.name} (#{migration.version})" if Base.logger # On our way up, we skip migrating the ones we've already migrated next if up? && migrated.include?(migration.version.to_i) # On our way down, we skip reverting the ones we've never migrated if down? && !migrated.include?(migration.version.to_i) migration.announce 'never migrated, skipping'; migration.write next end begin ddl_transaction do migration.migrate(@direction) (migration.version) end rescue => e canceled_msg = Base.connection.supports_ddl_transactions? ? "this and " : "" raise StandardError, "An error has occurred, #{canceled_msg}all later migrations canceled:\n\n#{e}", e.backtrace end end end |
- (Object) migrated
582 583 584 |
# File 'activerecord/lib/active_record/migration.rb', line 582 def migrated @migrated_versions ||= self.class.get_all_versions end |
- (Object) migrations
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 |
# File 'activerecord/lib/active_record/migration.rb', line 547 def migrations @migrations ||= begin files = Dir["#{@migrations_path}/[0-9]*_*.rb"] migrations = files.inject([]) do |klasses, file| version, name = file.scan(/([0-9]+)_([_a-z0-9]*).rb/).first raise IllegalMigrationNameError.new(file) unless version version = version.to_i if klasses.detect { |m| m.version == version } raise DuplicateMigrationVersionError.new(version) end if klasses.detect { |m| m.name == name.camelize } raise DuplicateMigrationNameError.new(name.camelize) end migration = MigrationProxy.new migration.name = name.camelize migration.version = version migration.filename = file klasses << migration end migrations = migrations.sort_by { |m| m.version } down? ? migrations.reverse : migrations end end |
- (Object) pending_migrations
577 578 579 580 |
# File 'activerecord/lib/active_record/migration.rb', line 577 def pending_migrations already_migrated = migrated migrations.reject { |m| already_migrated.include?(m.version.to_i) } end |
- (Object) run
499 500 501 502 503 504 505 506 |
# File 'activerecord/lib/active_record/migration.rb', line 499 def run target = migrations.detect { |m| m.version == @target_version } raise UnknownMigrationVersionError.new(@target_version) if target.nil? unless (up? && migrated.include?(target.version.to_i)) || (down? && !migrated.include?(target.version.to_i)) target.migrate(@direction) (target.version) end end |