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

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) click to toggle source

Set up all state for the migrator instance

Calls superclass method Sequel::Migrator::new
    # File lib/sequel/extensions/migration.rb
529 def initialize(db, directory, opts=OPTS)
530   super
531   @current = opts[:current] || current_migration_version
532 
533   latest_version = latest_migration_version
534   @target = if opts[:target]
535     opts[:target]
536   elsif opts[:relative]
537     @current + opts[:relative]
538   else
539     latest_version
540   end
541 
542   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
543 
544   if @target > latest_version
545     @target = latest_version
546   elsif @target < 0
547     @target = 0
548   end
549 
550   @direction = current < target ? :up : :down
551 
552   if @direction == :down && @current >= @files.length && !@allow_missing_migration_files
553     raise Migrator::Error, "Missing migration version(s) needed to migrate down to target version (current: #{current}, target: #{target})"
554   end
555 
556   @migrations = get_migrations
557 end

Public Instance Methods

is_current?() click to toggle source

The integer migrator is current if the current version is the same as the target version.

    # File lib/sequel/extensions/migration.rb
560 def is_current?
561   current_migration_version == target
562 end
run() click to toggle source

Apply all migrations on the database

    # File lib/sequel/extensions/migration.rb
565 def run
566   migrations.zip(version_numbers).each do |m, v|
567     timer = Sequel.start_timer
568     db.log_info("Begin applying migration version #{v}, direction: #{direction}")
569     checked_transaction(m) do
570       m.apply(db, direction)
571       set_migration_version(up? ? v : v-1)
572     end
573     db.log_info("Finished applying migration version #{v}, direction: #{direction}, took #{sprintf('%0.6f', Sequel.elapsed_seconds_since(timer))} seconds")
574   end
575   
576   target
577 end

Private Instance Methods

current_migration_version() click to toggle source

Gets the current migration version stored in the database. If no version number is stored, 0 is returned.

    # File lib/sequel/extensions/migration.rb
583 def current_migration_version
584   ds.get(column) || 0
585 end
default_schema_column() click to toggle source

The default column storing schema version.

    # File lib/sequel/extensions/migration.rb
588 def default_schema_column
589   :version
590 end
default_schema_table() click to toggle source

The default table storing schema version.

    # File lib/sequel/extensions/migration.rb
593 def default_schema_table
594   :schema_info
595 end
get_migration_files() click to toggle source

Returns any found migration files in the supplied directory.

    # File lib/sequel/extensions/migration.rb
598 def get_migration_files
599   files = []
600   Dir.new(directory).each do |file|
601     next unless MIGRATION_FILE_PATTERN.match(file)
602     version = migration_version_from_file(file)
603     if version >= 20000101
604       raise Migrator::Error, "Migration number too large, must use TimestampMigrator: #{file}"
605     end
606     raise(Error, "Duplicate migration version: #{version}") if files[version]
607     files[version] = File.join(directory, file)
608   end
609   1.upto(files.length - 1){|i| raise(Error, "Missing migration version: #{i}") unless files[i]} unless @allow_missing_migration_files
610   files
611 end
get_migrations() click to toggle source

Returns a list of migration classes filtered for the migration range and ordered according to the migration direction.

    # File lib/sequel/extensions/migration.rb
615 def get_migrations
616   version_numbers.map{|n| load_migration_file(files[n])}
617 end
latest_migration_version() click to toggle source

Returns the latest version available in the specified directory.

    # File lib/sequel/extensions/migration.rb
620 def latest_migration_version
621   l = files.last
622   l ? migration_version_from_file(File.basename(l)) : nil
623 end
schema_dataset() click to toggle source

Returns the dataset for the schema_info table. If no such table exists, it is automatically created.

    # File lib/sequel/extensions/migration.rb
627 def schema_dataset
628   c = column
629   ds = db.from(table)
630   db.create_table?(table){Integer c, :default=>0, :null=>false}
631   unless ds.columns.include?(c)
632     db.alter_table(table){add_column c, Integer, :default=>0, :null=>false}
633   end
634   ds.insert(c=>0) if ds.empty?
635   raise(Error, "More than 1 row in migrator table") if ds.count > 1
636   ds
637 end
set_migration_version(version) click to toggle source

Sets the current migration version stored in the database.

    # File lib/sequel/extensions/migration.rb
640 def set_migration_version(version)
641   ds.update(column=>version)
642 end
up?() click to toggle source

Whether or not this is an up migration

    # File lib/sequel/extensions/migration.rb
645 def up?
646   direction == :up
647 end
version_numbers() click to toggle source

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
652 def version_numbers
653   @version_numbers ||= begin
654     versions = files.
655       compact.
656       map{|f| migration_version_from_file(File.basename(f))}.
657       select{|v| up? ? (v > current && v <= target) : (v <= current && v > target)}.
658       sort
659     versions.reverse! unless up?
660     versions
661   end
662 end