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
700 def initialize(db, directory, opts=OPTS)
701   super
702   @target = opts[:target]
703   @applied_migrations = get_applied_migrations
704   @migration_tuples = get_migration_tuples
705 end
run_single(db, path, opts=OPTS) click to toggle source

Apply the migration in the given file path. See Migrator.run for the available options. Additionally, this method supports the :direction option for whether to run the migration up (default) or down.

    # File lib/sequel/extensions/migration.rb
710 def self.run_single(db, path, opts=OPTS)
711   new(db, File.dirname(path), opts).run_single(path, opts[:direction] || :up)
712 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
716 def is_current?
717   migration_tuples.empty?
718 end
run() click to toggle source

Apply all migration tuples on the database

    # File lib/sequel/extensions/migration.rb
721 def run
722   migration_tuples.each do |m, f, direction|
723     apply_migration(m, f, direction)
724   end
725   nil
726 end
run_single(path, direction) click to toggle source

Apply single migration tuple at the given path with the given direction on the database.

    # File lib/sequel/extensions/migration.rb
730 def run_single(path, direction)
731   migration = load_migration_file(path)
732   file_name = File.basename(path)
733   already_applied = applied_migrations.include?(file_name.downcase)
734 
735   return if direction == :up ? already_applied : !already_applied
736 
737   apply_migration(migration, file_name, direction)
738   nil
739 end

Private Instance Methods

apply_migration(migration, file_name, direction) click to toggle source

Apply a single migration with the given filename in the given direction.

    # File lib/sequel/extensions/migration.rb
744 def apply_migration(migration, file_name, direction)
745   fi = file_name.downcase
746   t = Time.now
747 
748   db.log_info("Begin applying migration #{file_name}, direction: #{direction}")
749   checked_transaction(migration) do
750     migration.apply(db, direction)
751     direction == :up ? ds.insert(column=>fi) : ds.where(column=>fi).delete
752   end
753   db.log_info("Finished applying migration #{file_name}, direction: #{direction}, took #{sprintf('%0.6f', Time.now - t)} seconds")
754 end
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
758 def convert_from_schema_info
759   v = db[:schema_info].get(:version)
760   ds = db.from(table)
761   files.each do |path|
762     f = File.basename(path)
763     if migration_version_from_file(f) <= v
764       ds.insert(column=>f)
765     end
766   end
767 end
default_schema_column() click to toggle source

The default column storing migration filenames.

    # File lib/sequel/extensions/migration.rb
770 def default_schema_column
771   :filename
772 end
default_schema_table() click to toggle source

The default table storing migration filenames.

    # File lib/sequel/extensions/migration.rb
775 def default_schema_table
776   :schema_migrations
777 end
get_applied_migrations() click to toggle source

Returns filenames of all applied migrations

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

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

    # File lib/sequel/extensions/migration.rb
788 def get_migration_files
789   files = []
790   Dir.new(directory).each do |file|
791     next unless MIGRATION_FILE_PATTERN.match(file)
792     files << File.join(directory, file)
793   end
794   files.sort_by{|f| MIGRATION_FILE_PATTERN.match(File.basename(f))[1].to_i}
795 end
get_migration_tuples() click to toggle source

Returns tuples of migration, filename, and direction

    # File lib/sequel/extensions/migration.rb
798 def get_migration_tuples
799   up_mts = []
800   down_mts = []
801   files.each do |path|
802     f = File.basename(path)
803     fi = f.downcase
804     if target
805       if migration_version_from_file(f) > target
806         if applied_migrations.include?(fi)
807           down_mts << [load_migration_file(path), f, :down]
808         end
809       elsif !applied_migrations.include?(fi)
810         up_mts << [load_migration_file(path), f, :up]
811       end
812     elsif !applied_migrations.include?(fi)
813       up_mts << [load_migration_file(path), f, :up]
814     end
815   end
816   up_mts + down_mts.reverse
817 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
821 def schema_dataset
822   c = column
823   ds = db.from(table)
824   if !db.table_exists?(table)
825     begin
826       db.create_table(table){String c, :primary_key=>true}
827     rescue Sequel::DatabaseError => e
828       if db.database_type == :mysql && e.message =~ /max key length/
829         # Handle case where MySQL is used with utf8mb4 charset default, which
830         # only allows a maximum length of about 190 characters for string
831         # primary keys due to InnoDB limitations.
832         db.create_table(table){String c, :primary_key=>true, :size=>190}
833       else
834         raise e
835       end
836     end
837     if db.table_exists?(:schema_info) and vha = db[:schema_info].all and vha.length == 1 and
838        vha.first.keys == [:version] and vha.first.values.first.is_a?(Integer)
839       convert_from_schema_info
840     end
841   elsif !ds.columns.include?(c)
842     raise(Error, "Migrator table #{table} does not contain column #{c}")
843   end
844   ds
845 end