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 549 def initialize(db, directory, opts=OPTS) 550 super 551 @current = opts[:current] || current_migration_version 552 553 latest_version = latest_migration_version 554 @target = if opts[:target] 555 opts[:target] 556 elsif opts[:relative] 557 @current + opts[:relative] 558 else 559 latest_version 560 end 561 562 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 563 564 if @target > latest_version 565 @target = latest_version 566 elsif @target < 0 567 @target = 0 568 end 569 570 @direction = current < target ? :up : :down 571 572 if @direction == :down && @current >= @files.length && !@allow_missing_migration_files 573 raise Migrator::Error, "Missing migration version(s) needed to migrate down to target version (current: #{current}, target: #{target})" 574 end 575 576 @migrations = get_migrations 577 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 580 def is_current? 581 current_migration_version == target 582 end
Apply all migrations on the database
# File lib/sequel/extensions/migration.rb 585 def run 586 migrations.zip(version_numbers).each do |m, v| 587 timer = Sequel.start_timer 588 db.log_info("Begin applying migration version #{v}, direction: #{direction}") 589 checked_transaction(m) do 590 m.apply(db, direction) 591 set_migration_version(up? ? v : v-1) 592 end 593 db.log_info("Finished applying migration version #{v}, direction: #{direction}, took #{sprintf('%0.6f', Sequel.elapsed_seconds_since(timer))} seconds") 594 end 595 596 target 597 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 603 def current_migration_version 604 ds.get(column) || 0 605 end
The default column storing schema version.
# File lib/sequel/extensions/migration.rb 608 def default_schema_column 609 :version 610 end
The default table storing schema version.
# File lib/sequel/extensions/migration.rb 613 def default_schema_table 614 :schema_info 615 end
Returns any found migration files in the supplied directory.
# File lib/sequel/extensions/migration.rb 618 def get_migration_files 619 files = [] 620 Dir.new(directory).each do |file| 621 next unless MIGRATION_FILE_PATTERN.match(file) 622 version = migration_version_from_file(file) 623 if version >= 20000101 624 raise Migrator::Error, "Migration number too large, must use TimestampMigrator: #{file}" 625 end 626 raise(Error, "Duplicate migration version: #{version}") if files[version] 627 files[version] = File.join(directory, file) 628 end 629 1.upto(files.length - 1){|i| raise(Error, "Missing migration version: #{i}") unless files[i]} unless @allow_missing_migration_files 630 files 631 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 635 def get_migrations 636 version_numbers.map{|n| load_migration_file(files[n])} 637 end
Returns the latest version available in the specified directory.
# File lib/sequel/extensions/migration.rb 640 def latest_migration_version 641 l = files.last 642 l ? migration_version_from_file(File.basename(l)) : nil 643 end
Returns the dataset for the schema_info table. If no such table exists, it is automatically created.
# File lib/sequel/extensions/migration.rb 647 def schema_dataset 648 c = column 649 ds = db.from(table) 650 db.create_table?(table){Integer c, :default=>0, :null=>false} 651 unless ds.columns.include?(c) 652 db.alter_table(table){add_column c, Integer, :default=>0, :null=>false} 653 end 654 ds.insert(c=>0) if ds.empty? 655 raise(Error, "More than 1 row in migrator table") if ds.count > 1 656 ds 657 end
Sets the current migration version stored in the database.
# File lib/sequel/extensions/migration.rb 660 def set_migration_version(version) 661 ds.update(column=>version) 662 end
Whether or not this is an up migration
# File lib/sequel/extensions/migration.rb 665 def up? 666 direction == :up 667 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 672 def version_numbers 673 @version_numbers ||= begin 674 versions = files. 675 compact. 676 map{|f| migration_version_from_file(File.basename(f))}. 677 select{|v| up? ? (v > current && v <= target) : (v <= current && v > target)}. 678 sort 679 versions.reverse! unless up? 680 versions 681 end 682 end