The default migrator, recommended in most cases. Uses a simple incrementing version number starting with 1, where missing or duplicate migration file versions are not allowed. Part of the migration extension.
Constants
| Error | = | Migrator::Error |
Attributes
| current | [R] |
The current version for this migrator |
| direction | [R] |
The direction of the migrator, either :up or :down |
| migrations | [R] |
The migrations used by this migrator |
Public Class methods
new(db, directory, opts=OPTS)
Set up all state for the migrator instance
[show source]
# File lib/sequel/extensions/migration.rb 555 def initialize(db, directory, opts=OPTS) 556 super 557 @current = opts[:current] || current_migration_version 558 559 latest_version = latest_migration_version 560 @target = if opts[:target] 561 opts[:target] 562 elsif opts[:relative] 563 @current + opts[:relative] 564 else 565 latest_version 566 end 567 568 raise(Error, "No target and/or latest version available, probably because no migration files found or filenames don't follow the migration filename convention") unless target && latest_version 569 570 if @target > latest_version 571 @target = latest_version 572 elsif @target < 0 573 @target = 0 574 end 575 576 @direction = current < target ? :up : :down 577 578 if @direction == :down && @current >= @files.length && !@allow_missing_migration_files 579 raise Migrator::Error, "Missing migration version(s) needed to migrate down to target version (current: #{current}, target: #{target})" 580 end 581 582 @migrations = get_migrations 583 end
Public Instance methods
is_current?()
The integer migrator is current if the current version is the same as the target version.
[show source]
# File lib/sequel/extensions/migration.rb 586 def is_current? 587 current_migration_version == target 588 end
run()
Apply all migrations on the database
[show source]
# File lib/sequel/extensions/migration.rb 591 def run 592 migrations.zip(version_numbers).each do |m, v| 593 timer = Sequel.start_timer 594 db.log_info("Begin applying migration version #{v}, direction: #{direction}") 595 checked_transaction(m) do 596 m.apply(db, direction) 597 set_migration_version(up? ? v : v-1) 598 end 599 db.log_info("Finished applying migration version #{v}, direction: #{direction}, took #{sprintf('%0.6f', Sequel.elapsed_seconds_since(timer))} seconds") 600 end 601 602 target 603 end