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
553 def initialize(db, directory, opts=OPTS)
554   super
555   @current = opts[:current] || current_migration_version
556 
557   latest_version = latest_migration_version
558   @target = if opts[:target]
559     opts[:target]
560   elsif opts[:relative]
561     @current + opts[:relative]
562   else
563     latest_version
564   end
565 
566   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
567 
568   if @target > latest_version
569     @target = latest_version
570   elsif @target < 0
571     @target = 0
572   end
573 
574   @direction = current < target ? :up : :down
575 
576   if @direction == :down && @current >= @files.length && !@allow_missing_migration_files
577     raise Migrator::Error, "Missing migration version(s) needed to migrate down to target version (current: #{current}, target: #{target})"
578   end
579 
580   @migrations = get_migrations
581 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
584 def is_current?
585   current_migration_version == target
586 end
run() click to toggle source

Apply all migrations on the database

    # File lib/sequel/extensions/migration.rb
589 def run
590   migrations.zip(version_numbers).each do |m, v|
591     timer = Sequel.start_timer
592     db.log_info("Begin applying migration version #{v}, direction: #{direction}")
593     checked_transaction(m) do
594       m.apply(db, direction)
595       set_migration_version(up? ? v : v-1)
596     end
597     db.log_info("Finished applying migration version #{v}, direction: #{direction}, took #{sprintf('%0.6f', Sequel.elapsed_seconds_since(timer))} seconds")
598   end
599   
600   target
601 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
607 def current_migration_version
608   ds.get(column) || 0
609 end
default_schema_column() click to toggle source

The default column storing schema version.

    # File lib/sequel/extensions/migration.rb
612 def default_schema_column
613   :version
614 end
default_schema_table() click to toggle source

The default table storing schema version.

    # File lib/sequel/extensions/migration.rb
617 def default_schema_table
618   :schema_info
619 end
get_migration_files() click to toggle source

Returns any found migration files in the supplied directory.

    # File lib/sequel/extensions/migration.rb
622 def get_migration_files
623   files = []
624   Dir.new(directory).each do |file|
625     next unless MIGRATION_FILE_PATTERN.match(file)
626     version = migration_version_from_file(file)
627     if version >= 20000101
628       raise Migrator::Error, "Migration number too large, must use TimestampMigrator: #{file}"
629     end
630     raise(Error, "Duplicate migration version: #{version}") if files[version]
631     files[version] = File.join(directory, file)
632   end
633   1.upto(files.length - 1){|i| raise(Error, "Missing migration version: #{i}") unless files[i]} unless @allow_missing_migration_files
634   files
635 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
639 def get_migrations
640   version_numbers.map{|n| load_migration_file(files[n])}
641 end
latest_migration_version() click to toggle source

Returns the latest version available in the specified directory.

    # File lib/sequel/extensions/migration.rb
644 def latest_migration_version
645   l = files.last
646   l ? migration_version_from_file(File.basename(l)) : nil
647 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
651 def schema_dataset
652   c = column
653   ds = db.from(table)
654   db.create_table?(table){Integer c, :default=>0, :null=>false}
655   unless ds.columns.include?(c)
656     db.alter_table(table){add_column c, Integer, :default=>0, :null=>false}
657   end
658   ds.insert(c=>0) if ds.empty?
659   raise(Error, "More than 1 row in migrator table") if ds.count > 1
660   ds
661 end
set_migration_version(version) click to toggle source

Sets the current migration version stored in the database.

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

Whether or not this is an up migration

    # File lib/sequel/extensions/migration.rb
669 def up?
670   direction == :up
671 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
676 def version_numbers
677   @version_numbers ||= begin
678     versions = files.
679       compact.
680       map{|f| migration_version_from_file(File.basename(f))}.
681       select{|v| up? ? (v > current && v <= target) : (v <= current && v > target)}.
682       sort
683     versions.reverse! unless up?
684     versions
685   end
686 end