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
Array
of strings of applied migration filenames
Get tuples of migrations, filenames, and actions for each migration
Public Class Methods
Set up all state for the migrator instance
Sequel::Migrator::new
# File lib/sequel/extensions/migration.rb 664 def initialize(db, directory, opts=OPTS) 665 super 666 @target = opts[:target] 667 @applied_migrations = get_applied_migrations 668 @migration_tuples = get_migration_tuples 669 end
Public Instance Methods
The timestamp migrator is current if there are no migrations to apply in either direction.
# File lib/sequel/extensions/migration.rb 673 def is_current? 674 migration_tuples.empty? 675 end
Apply all migration tuples on the database
# File lib/sequel/extensions/migration.rb 678 def run 679 migration_tuples.each do |m, f, direction| 680 t = Time.now 681 db.log_info("Begin applying migration #{f}, direction: #{direction}") 682 checked_transaction(m) do 683 m.apply(db, direction) 684 fi = f.downcase 685 direction == :up ? ds.insert(column=>fi) : ds.where(column=>fi).delete 686 end 687 db.log_info("Finished applying migration #{f}, direction: #{direction}, took #{sprintf('%0.6f', Time.now - t)} seconds") 688 end 689 nil 690 end
Private Instance Methods
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 696 def convert_from_schema_info 697 v = db[:schema_info].get(:version) 698 ds = db.from(table) 699 files.each do |path| 700 f = File.basename(path) 701 if migration_version_from_file(f) <= v 702 ds.insert(column=>f) 703 end 704 end 705 end
The default column storing migration filenames.
# File lib/sequel/extensions/migration.rb 708 def default_schema_column 709 :filename 710 end
The default table storing migration filenames.
# File lib/sequel/extensions/migration.rb 713 def default_schema_table 714 :schema_migrations 715 end
Returns filenames of all applied migrations
# File lib/sequel/extensions/migration.rb 718 def get_applied_migrations 719 am = ds.select_order_map(column) 720 missing_migration_files = am - files.map{|f| File.basename(f).downcase} 721 raise(Error, "Applied migration files not in file system: #{missing_migration_files.join(', ')}") if missing_migration_files.length > 0 && !@allow_missing_migration_files 722 am 723 end
Returns any migration files found in the migrator's directory.
# File lib/sequel/extensions/migration.rb 726 def get_migration_files 727 files = [] 728 Dir.new(directory).each do |file| 729 next unless MIGRATION_FILE_PATTERN.match(file) 730 files << File.join(directory, file) 731 end 732 files.sort_by{|f| MIGRATION_FILE_PATTERN.match(File.basename(f))[1].to_i} 733 end
Returns tuples of migration, filename, and direction
# File lib/sequel/extensions/migration.rb 736 def get_migration_tuples 737 up_mts = [] 738 down_mts = [] 739 files.each do |path| 740 f = File.basename(path) 741 fi = f.downcase 742 if target 743 if migration_version_from_file(f) > target 744 if applied_migrations.include?(fi) 745 down_mts << [load_migration_file(path), f, :down] 746 end 747 elsif !applied_migrations.include?(fi) 748 up_mts << [load_migration_file(path), f, :up] 749 end 750 elsif !applied_migrations.include?(fi) 751 up_mts << [load_migration_file(path), f, :up] 752 end 753 end 754 up_mts + down_mts.reverse 755 end
Returns the dataset for the schema_migrations table. If no such table exists, it is automatically created.
# File lib/sequel/extensions/migration.rb 759 def schema_dataset 760 c = column 761 ds = db.from(table) 762 if !db.table_exists?(table) 763 begin 764 db.create_table(table){String c, :primary_key=>true} 765 rescue Sequel::DatabaseError => e 766 if db.database_type == :mysql && e.message =~ /max key length/ 767 # Handle case where MySQL is used with utf8mb4 charset default, which 768 # only allows a maximum length of about 190 characters for string 769 # primary keys due to InnoDB limitations. 770 db.create_table(table){String c, :primary_key=>true, :size=>190} 771 else 772 raise e 773 end 774 end 775 if db.table_exists?(:schema_info) and vha = db[:schema_info].all and vha.length == 1 and 776 vha.first.keys == [:version] and vha.first.values.first.is_a?(Integer) 777 convert_from_schema_info 778 end 779 elsif !ds.columns.include?(c) 780 raise(Error, "Migrator table #{table} does not contain column #{c}") 781 end 782 ds 783 end