class Sequel::IntegerMigrator
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
Attributes
The current version for this migrator
The direction of the migrator, either :up or :down
The migrations used by this migrator
Public Class Methods
Set up all state for the migrator instance
Sequel::Migrator::new
# File lib/sequel/extensions/migration.rb 522 def initialize(db, directory, opts=OPTS) 523 super 524 @current = opts[:current] || current_migration_version 525 526 latest_version = latest_migration_version 527 @target = if opts[:target] 528 opts[:target] 529 elsif opts[:relative] 530 @current + opts[:relative] 531 else 532 latest_version 533 end 534 535 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 536 537 if @target > latest_version 538 @target = latest_version 539 elsif @target < 0 540 @target = 0 541 end 542 543 @direction = current < target ? :up : :down 544 545 if @direction == :down && @current >= @files.length 546 raise Migrator::Error, "Missing migration version(s) needed to migrate down to target version (current: #{current}, target: #{target})" 547 end 548 549 @migrations = get_migrations 550 end
Public Instance Methods
The integer migrator is current if the current version is the same as the target version.
# File lib/sequel/extensions/migration.rb 553 def is_current? 554 current_migration_version == target 555 end
Apply all migrations on the database
# File lib/sequel/extensions/migration.rb 558 def run 559 migrations.zip(version_numbers).each do |m, v| 560 timer = Sequel.start_timer 561 db.log_info("Begin applying migration version #{v}, direction: #{direction}") 562 checked_transaction(m) do 563 m.apply(db, direction) 564 set_migration_version(up? ? v : v-1) 565 end 566 db.log_info("Finished applying migration version #{v}, direction: #{direction}, took #{sprintf('%0.6f', Sequel.elapsed_seconds_since(timer))} seconds") 567 end 568 569 target 570 end
Private Instance Methods
Gets the current migration version stored in the database. If no version number is stored, 0 is returned.
# File lib/sequel/extensions/migration.rb 576 def current_migration_version 577 ds.get(column) || 0 578 end
The default column storing schema version.
# File lib/sequel/extensions/migration.rb 581 def default_schema_column 582 :version 583 end
The default table storing schema version.
# File lib/sequel/extensions/migration.rb 586 def default_schema_table 587 :schema_info 588 end
Returns any found migration files in the supplied directory.
# File lib/sequel/extensions/migration.rb 591 def get_migration_files 592 files = [] 593 Dir.new(directory).each do |file| 594 next unless MIGRATION_FILE_PATTERN.match(file) 595 version = migration_version_from_file(file) 596 if version >= 20000101 597 raise Migrator::Error, "Migration number too large, must use TimestampMigrator: #{file}" 598 end 599 raise(Error, "Duplicate migration version: #{version}") if files[version] 600 files[version] = File.join(directory, file) 601 end 602 1.upto(files.length - 1){|i| raise(Error, "Missing migration version: #{i}") unless files[i]} unless @allow_missing_migration_files 603 files 604 end
Returns a list of migration classes filtered for the migration range and ordered according to the migration direction.
# File lib/sequel/extensions/migration.rb 608 def get_migrations 609 version_numbers.map{|n| load_migration_file(files[n])} 610 end
Returns the latest version available in the specified directory.
# File lib/sequel/extensions/migration.rb 613 def latest_migration_version 614 l = files.last 615 l ? migration_version_from_file(File.basename(l)) : nil 616 end
Returns the dataset for the schema_info table. If no such table exists, it is automatically created.
# File lib/sequel/extensions/migration.rb 620 def schema_dataset 621 c = column 622 ds = db.from(table) 623 db.create_table?(table){Integer c, :default=>0, :null=>false} 624 unless ds.columns.include?(c) 625 db.alter_table(table){add_column c, Integer, :default=>0, :null=>false} 626 end 627 ds.insert(c=>0) if ds.empty? 628 raise(Error, "More than 1 row in migrator table") if ds.count > 1 629 ds 630 end
Sets the current migration version stored in the database.
# File lib/sequel/extensions/migration.rb 633 def set_migration_version(version) 634 ds.update(column=>version) 635 end
Whether or not this is an up migration
# File lib/sequel/extensions/migration.rb 638 def up? 639 direction == :up 640 end
An array of numbers corresponding to the migrations, so that each number in the array is the migration version that will be in affect after the migration is run.
# File lib/sequel/extensions/migration.rb 645 def version_numbers 646 @version_numbers ||= begin 647 versions = files. 648 compact. 649 map{|f| migration_version_from_file(File.basename(f))}. 650 select{|v| up? ? (v > current && v <= target) : (v <= current && v > target)}. 651 sort 652 versions.reverse! unless up? 653 versions 654 end 655 end