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
673 def initialize(db, directory, opts=OPTS)
674   super
675   @target = opts[:target]
676   @applied_migrations = get_applied_migrations
677   @migration_tuples = get_migration_tuples
678 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
682 def is_current?
683   migration_tuples.empty?
684 end
run() click to toggle source

Apply all migration tuples on the database

    # File lib/sequel/extensions/migration.rb
687 def run
688   migration_tuples.each do |m, f, direction|
689     t = Time.now
690     db.log_info("Begin applying migration #{f}, direction: #{direction}")
691     checked_transaction(m) do
692       m.apply(db, direction)
693       fi = f.downcase
694       direction == :up ? ds.insert(column=>fi) : ds.where(column=>fi).delete
695     end
696     db.log_info("Finished applying migration #{f}, direction: #{direction}, took #{sprintf('%0.6f', Time.now - t)} seconds")
697   end
698   nil
699 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
705 def convert_from_schema_info
706   v = db[:schema_info].get(:version)
707   ds = db.from(table)
708   files.each do |path|
709     f = File.basename(path)
710     if migration_version_from_file(f) <= v
711       ds.insert(column=>f)
712     end
713   end
714 end
default_schema_column() click to toggle source

The default column storing migration filenames.

    # File lib/sequel/extensions/migration.rb
717 def default_schema_column
718   :filename
719 end
default_schema_table() click to toggle source

The default table storing migration filenames.

    # File lib/sequel/extensions/migration.rb
722 def default_schema_table
723   :schema_migrations
724 end
get_applied_migrations() click to toggle source

Returns filenames of all applied migrations

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

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

    # File lib/sequel/extensions/migration.rb
735 def get_migration_files
736   files = []
737   Dir.new(directory).each do |file|
738     next unless MIGRATION_FILE_PATTERN.match(file)
739     files << File.join(directory, file)
740   end
741   files.sort_by{|f| MIGRATION_FILE_PATTERN.match(File.basename(f))[1].to_i}
742 end
get_migration_tuples() click to toggle source

Returns tuples of migration, filename, and direction

    # File lib/sequel/extensions/migration.rb
745 def get_migration_tuples
746   up_mts = []
747   down_mts = []
748   files.each do |path|
749     f = File.basename(path)
750     fi = f.downcase
751     if target
752       if migration_version_from_file(f) > target
753         if applied_migrations.include?(fi)
754           down_mts << [load_migration_file(path), f, :down]
755         end
756       elsif !applied_migrations.include?(fi)
757         up_mts << [load_migration_file(path), f, :up]
758       end
759     elsif !applied_migrations.include?(fi)
760       up_mts << [load_migration_file(path), f, :up]
761     end
762   end
763   up_mts + down_mts.reverse
764 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
768 def schema_dataset
769   c = column
770   ds = db.from(table)
771   if !db.table_exists?(table)
772     begin
773       db.create_table(table){String c, :primary_key=>true}
774     rescue Sequel::DatabaseError => e
775       if db.database_type == :mysql && e.message =~ /max key length/
776         # Handle case where MySQL is used with utf8mb4 charset default, which
777         # only allows a maximum length of about 190 characters for string
778         # primary keys due to InnoDB limitations.
779         db.create_table(table){String c, :primary_key=>true, :size=>190}
780       else
781         raise e
782       end
783     end
784     if db.table_exists?(:schema_info) and vha = db[:schema_info].all and vha.length == 1 and
785        vha.first.keys == [:version] and vha.first.values.first.is_a?(Integer)
786       convert_from_schema_info
787     end
788   elsif !ds.columns.include?(c)
789     raise(Error, "Migrator table #{table} does not contain column #{c}")
790   end
791   ds
792 end