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
706 def initialize(db, directory, opts=OPTS)
707   @allow_migration_number = opts[:allow_migration_number]
708   super
709   @target = opts[:target]
710   @applied_migrations = get_applied_migrations
711   @migration_tuples = get_migration_tuples
712 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
717 def self.run_single(db, path, opts=OPTS)
718   new(db, File.dirname(path), opts).run_single(path, opts[:direction] || :up)
719 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
723 def is_current?
724   migration_tuples.empty?
725 end
run() click to toggle source

Apply all migration tuples on the database

    # File lib/sequel/extensions/migration.rb
728 def run
729   migration_tuples.each do |m, f, direction|
730     apply_migration(m, f, direction)
731   end
732   nil
733 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
737 def run_single(path, direction)
738   migration = load_migration_file(path)
739   file_name = File.basename(path)
740   already_applied = applied_migrations.include?(file_name.downcase)
741 
742   return if direction == :up ? already_applied : !already_applied
743 
744   apply_migration(migration, file_name, direction)
745   nil
746 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
751 def apply_migration(migration, file_name, direction)
752   fi = file_name.downcase
753   t = Time.now
754 
755   db.log_info("Begin applying migration #{file_name}, direction: #{direction}")
756   checked_transaction(migration) do
757     migration.apply(db, direction)
758     direction == :up ? ds.insert(column=>fi) : ds.where(column=>fi).delete
759   end
760   db.log_info("Finished applying migration #{file_name}, direction: #{direction}, took #{sprintf('%0.6f', Time.now - t)} seconds")
761 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
765 def convert_from_schema_info
766   v = db[:schema_info].get(:version)
767   ds = db.from(table)
768   files.each do |path|
769     f = File.basename(path)
770     if migration_version_from_file(f) <= v
771       ds.insert(column=>f)
772     end
773   end
774 end
default_schema_column() click to toggle source

The default column storing migration filenames.

    # File lib/sequel/extensions/migration.rb
777 def default_schema_column
778   :filename
779 end
default_schema_table() click to toggle source

The default table storing migration filenames.

    # File lib/sequel/extensions/migration.rb
782 def default_schema_table
783   :schema_migrations
784 end
get_applied_migrations() click to toggle source

Returns filenames of all applied migrations

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

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

    # File lib/sequel/extensions/migration.rb
795 def get_migration_files
796   files = []
797   allow_migration_number = @allow_migration_number
798 
799   Dir.new(directory).each do |file|
800     next unless MIGRATION_FILE_PATTERN.match(file)
801     files << File.join(directory, file)
802   end
803 
804   files.map! do |f|
805     ver, = split = split_migration_filename(f)
806     if allow_migration_number && !(allow_migration_number === ver)
807       raise Error, "Migration filename uses version number that is not allowed: #{f}"
808     end
809     [f, *split]
810   end.sort! do |a, b|
811     _, a_ver, a_name = a
812     _, b_ver, b_name = b
813     x = a_ver <=> b_ver
814     if x.zero?
815       x = a_name <=> b_name
816     end
817     x
818   end.map!(&:first)
819 end
get_migration_tuples() click to toggle source

Returns tuples of migration, filename, and direction

    # File lib/sequel/extensions/migration.rb
829 def get_migration_tuples
830   up_mts = []
831   down_mts = []
832   files.each do |path|
833     f = File.basename(path)
834     fi = f.downcase
835     if target
836       if migration_version_from_file(f) > target
837         if applied_migrations.include?(fi)
838           down_mts << [load_migration_file(path), f, :down]
839         end
840       elsif !applied_migrations.include?(fi)
841         up_mts << [load_migration_file(path), f, :up]
842       end
843     elsif !applied_migrations.include?(fi)
844       up_mts << [load_migration_file(path), f, :up]
845     end
846   end
847   up_mts + down_mts.reverse
848 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
852 def schema_dataset
853   c = column
854   ds = db.from(table)
855   if !db.table_exists?(table)
856     begin
857       db.create_table(table){String c, :primary_key=>true}
858     rescue Sequel::DatabaseError => e
859       if db.database_type == :mysql && e.message.include?('max key length')
860         # Handle case where MySQL is used with utf8mb4 charset default, which
861         # only allows a maximum length of about 190 characters for string
862         # primary keys due to InnoDB limitations.
863         db.create_table(table){String c, :primary_key=>true, :size=>190}
864       else
865         raise e
866       end
867     end
868     if db.table_exists?(:schema_info) and vha = db[:schema_info].all and vha.length == 1 and
869        vha.first.keys == [:version] and vha.first.values.first.is_a?(Integer)
870       convert_from_schema_info
871     end
872   elsif !ds.columns.include?(c)
873     raise(Error, "Migrator table #{table} does not contain column #{c}")
874   end
875   ds
876 end
split_migration_filename(path) click to toggle source

Return an integer and name (without extension) for the given path.

    # File lib/sequel/extensions/migration.rb
822 def split_migration_filename(path)
823   version, name = MIGRATION_FILE_PATTERN.match(File.basename(path)).captures
824   version = version.to_i
825   [version, name]
826 end