class Sequel::TimestampMigrator

The migrator used if any migration file version is greater than 20000101. Stores filenames of migration files, and can figure out which migrations have not been applied and apply them, even if earlier migrations are added after later migrations. If you plan to do that, the responsibility is on you to make sure the migrations don't conflict. Part of the migration extension.

Constants

Error

Attributes

applied_migrations[R]

Array of strings of applied migration filenames

migration_tuples[R]

Get tuples of migrations, filenames, and actions for each migration

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
665 def initialize(db, directory, opts=OPTS)
666   super
667   @target = opts[:target]
668   @applied_migrations = get_applied_migrations
669   @migration_tuples = get_migration_tuples
670 end

Public Instance Methods

is_current?() click to toggle source

The timestamp migrator is current if there are no migrations to apply in either direction.

    # File lib/sequel/extensions/migration.rb
674 def is_current?
675   migration_tuples.empty?
676 end
run() click to toggle source

Apply all migration tuples on the database

    # File lib/sequel/extensions/migration.rb
679 def run
680   migration_tuples.each do |m, f, direction|
681     t = Time.now
682     db.log_info("Begin applying migration #{f}, direction: #{direction}")
683     checked_transaction(m) do
684       m.apply(db, direction)
685       fi = f.downcase
686       direction == :up ? ds.insert(column=>fi) : ds.where(column=>fi).delete
687     end
688     db.log_info("Finished applying migration #{f}, direction: #{direction}, took #{sprintf('%0.6f', Time.now - t)} seconds")
689   end
690   nil
691 end

Private Instance Methods

convert_from_schema_info() click to toggle source

Convert the schema_info table to the new schema_migrations table format, using the version of the schema_info table and the current migration files.

    # File lib/sequel/extensions/migration.rb
697 def convert_from_schema_info
698   v = db[:schema_info].get(:version)
699   ds = db.from(table)
700   files.each do |path|
701     f = File.basename(path)
702     if migration_version_from_file(f) <= v
703       ds.insert(column=>f)
704     end
705   end
706 end
default_schema_column() click to toggle source

The default column storing migration filenames.

    # File lib/sequel/extensions/migration.rb
709 def default_schema_column
710   :filename
711 end
default_schema_table() click to toggle source

The default table storing migration filenames.

    # File lib/sequel/extensions/migration.rb
714 def default_schema_table
715   :schema_migrations
716 end
get_applied_migrations() click to toggle source

Returns filenames of all applied migrations

    # File lib/sequel/extensions/migration.rb
719 def get_applied_migrations
720   am = ds.select_order_map(column)
721   missing_migration_files = am - files.map{|f| File.basename(f).downcase}
722   raise(Error, "Applied migration files not in file system: #{missing_migration_files.join(', ')}") if missing_migration_files.length > 0 && !@allow_missing_migration_files
723   am
724 end
get_migration_files() click to toggle source

Returns any migration files found in the migrator's directory.

    # File lib/sequel/extensions/migration.rb
727 def get_migration_files
728   files = []
729   Dir.new(directory).each do |file|
730     next unless MIGRATION_FILE_PATTERN.match(file)
731     files << File.join(directory, file)
732   end
733   files.sort_by{|f| MIGRATION_FILE_PATTERN.match(File.basename(f))[1].to_i}
734 end
get_migration_tuples() click to toggle source

Returns tuples of migration, filename, and direction

    # File lib/sequel/extensions/migration.rb
737 def get_migration_tuples
738   up_mts = []
739   down_mts = []
740   files.each do |path|
741     f = File.basename(path)
742     fi = f.downcase
743     if target
744       if migration_version_from_file(f) > target
745         if applied_migrations.include?(fi)
746           down_mts << [load_migration_file(path), f, :down]
747         end
748       elsif !applied_migrations.include?(fi)
749         up_mts << [load_migration_file(path), f, :up]
750       end
751     elsif !applied_migrations.include?(fi)
752       up_mts << [load_migration_file(path), f, :up]
753     end
754   end
755   up_mts + down_mts.reverse
756 end
schema_dataset() click to toggle source

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

    # File lib/sequel/extensions/migration.rb
760 def schema_dataset
761   c = column
762   ds = db.from(table)
763   if !db.table_exists?(table)
764     begin
765       db.create_table(table){String c, :primary_key=>true}
766     rescue Sequel::DatabaseError => e
767       if db.database_type == :mysql && e.message =~ /max key length/
768         # Handle case where MySQL is used with utf8mb4 charset default, which
769         # only allows a maximum length of about 190 characters for string
770         # primary keys due to InnoDB limitations.
771         db.create_table(table){String c, :primary_key=>true, :size=>190}
772       else
773         raise e
774       end
775     end
776     if db.table_exists?(:schema_info) and vha = db[:schema_info].all and vha.length == 1 and
777        vha.first.keys == [:version] and vha.first.values.first.is_a?(Integer)
778       convert_from_schema_info
779     end
780   elsif !ds.columns.include?(c)
781     raise(Error, "Migrator table #{table} does not contain column #{c}")
782   end
783   ds
784 end