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
520 def initialize(db, directory, opts=OPTS)
521   super
522   @current = opts[:current] || current_migration_version
523 
524   latest_version = latest_migration_version
525   @target = if opts[:target]
526     opts[:target]
527   elsif opts[:relative]
528     @current + opts[:relative]
529   else
530     latest_version
531   end
532 
533   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
534 
535   if @target > latest_version
536     @target = latest_version
537   elsif @target < 0
538     @target = 0
539   end
540 
541   @direction = current < target ? :up : :down
542 
543   if @direction == :down && @current >= @files.length
544     raise Migrator::Error, "Missing migration version(s) needed to migrate down to target version (current: #{current}, target: #{target})"
545   end
546 
547   @migrations = get_migrations
548 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
551 def is_current?
552   current_migration_version == target
553 end
run() click to toggle source

Apply all migrations on the database

    # File lib/sequel/extensions/migration.rb
556 def run
557   migrations.zip(version_numbers).each do |m, v|
558     timer = Sequel.start_timer
559     db.log_info("Begin applying migration version #{v}, direction: #{direction}")
560     checked_transaction(m) do
561       m.apply(db, direction)
562       set_migration_version(up? ? v : v-1)
563     end
564     db.log_info("Finished applying migration version #{v}, direction: #{direction}, took #{sprintf('%0.6f', Sequel.elapsed_seconds_since(timer))} seconds")
565   end
566   
567   target
568 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
574 def current_migration_version
575   ds.get(column) || 0
576 end
default_schema_column() click to toggle source

The default column storing schema version.

    # File lib/sequel/extensions/migration.rb
579 def default_schema_column
580   :version
581 end
default_schema_table() click to toggle source

The default table storing schema version.

    # File lib/sequel/extensions/migration.rb
584 def default_schema_table
585   :schema_info
586 end
get_migration_files() click to toggle source

Returns any found migration files in the supplied directory.

    # File lib/sequel/extensions/migration.rb
589 def get_migration_files
590   files = []
591   Dir.new(directory).each do |file|
592     next unless MIGRATION_FILE_PATTERN.match(file)
593     version = migration_version_from_file(file)
594     if version >= 20000101
595       raise Migrator::Error, "Migration number too large, must use TimestampMigrator: #{file}"
596     end
597     raise(Error, "Duplicate migration version: #{version}") if files[version]
598     files[version] = File.join(directory, file)
599   end
600   1.upto(files.length - 1){|i| raise(Error, "Missing migration version: #{i}") unless files[i]} unless @allow_missing_migration_files
601   files
602 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
606 def get_migrations
607   version_numbers.map{|n| load_migration_file(files[n])}
608 end
latest_migration_version() click to toggle source

Returns the latest version available in the specified directory.

    # File lib/sequel/extensions/migration.rb
611 def latest_migration_version
612   l = files.last
613   l ? migration_version_from_file(File.basename(l)) : nil
614 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
618 def schema_dataset
619   c = column
620   ds = db.from(table)
621   db.create_table?(table){Integer c, :default=>0, :null=>false}
622   unless ds.columns.include?(c)
623     db.alter_table(table){add_column c, Integer, :default=>0, :null=>false}
624   end
625   ds.insert(c=>0) if ds.empty?
626   raise(Error, "More than 1 row in migrator table") if ds.count > 1
627   ds
628 end
set_migration_version(version) click to toggle source

Sets the current migration version stored in the database.

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

Whether or not this is an up migration

    # File lib/sequel/extensions/migration.rb
636 def up?
637   direction == :up
638 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
643 def version_numbers
644   @version_numbers ||= begin
645     versions = files.
646       compact.
647       map{|f| migration_version_from_file(File.basename(f))}.
648       select{|v| up? ? (v > current && v <= target) : (v <= current && v > target)}.
649       sort
650     versions.reverse! unless up?
651     versions
652   end
653 end