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 680 def initialize(db, directory, opts=OPTS) 681 super 682 @target = opts[:target] 683 @applied_migrations = get_applied_migrations 684 @migration_tuples = get_migration_tuples 685 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 689 def is_current? 690 migration_tuples.empty? 691 end
Apply all migration tuples on the database
# File lib/sequel/extensions/migration.rb 694 def run 695 migration_tuples.each do |m, f, direction| 696 t = Time.now 697 db.log_info("Begin applying migration #{f}, direction: #{direction}") 698 checked_transaction(m) do 699 m.apply(db, direction) 700 fi = f.downcase 701 direction == :up ? ds.insert(column=>fi) : ds.where(column=>fi).delete 702 end 703 db.log_info("Finished applying migration #{f}, direction: #{direction}, took #{sprintf('%0.6f', Time.now - t)} seconds") 704 end 705 nil 706 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 712 def convert_from_schema_info 713 v = db[:schema_info].get(:version) 714 ds = db.from(table) 715 files.each do |path| 716 f = File.basename(path) 717 if migration_version_from_file(f) <= v 718 ds.insert(column=>f) 719 end 720 end 721 end
The default column storing migration filenames.
# File lib/sequel/extensions/migration.rb 724 def default_schema_column 725 :filename 726 end
The default table storing migration filenames.
# File lib/sequel/extensions/migration.rb 729 def default_schema_table 730 :schema_migrations 731 end
Returns filenames of all applied migrations
# File lib/sequel/extensions/migration.rb 734 def get_applied_migrations 735 am = ds.select_order_map(column) 736 missing_migration_files = am - files.map{|f| File.basename(f).downcase} 737 raise(Error, "Applied migration files not in file system: #{missing_migration_files.join(', ')}") if missing_migration_files.length > 0 && !@allow_missing_migration_files 738 am 739 end
Returns any migration files found in the migrator's directory.
# File lib/sequel/extensions/migration.rb 742 def get_migration_files 743 files = [] 744 Dir.new(directory).each do |file| 745 next unless MIGRATION_FILE_PATTERN.match(file) 746 files << File.join(directory, file) 747 end 748 files.sort_by{|f| MIGRATION_FILE_PATTERN.match(File.basename(f))[1].to_i} 749 end
Returns tuples of migration, filename, and direction
# File lib/sequel/extensions/migration.rb 752 def get_migration_tuples 753 up_mts = [] 754 down_mts = [] 755 files.each do |path| 756 f = File.basename(path) 757 fi = f.downcase 758 if target 759 if migration_version_from_file(f) > target 760 if applied_migrations.include?(fi) 761 down_mts << [load_migration_file(path), f, :down] 762 end 763 elsif !applied_migrations.include?(fi) 764 up_mts << [load_migration_file(path), f, :up] 765 end 766 elsif !applied_migrations.include?(fi) 767 up_mts << [load_migration_file(path), f, :up] 768 end 769 end 770 up_mts + down_mts.reverse 771 end
Returns the dataset for the schema_migrations table. If no such table exists, it is automatically created.
# File lib/sequel/extensions/migration.rb 775 def schema_dataset 776 c = column 777 ds = db.from(table) 778 if !db.table_exists?(table) 779 begin 780 db.create_table(table){String c, :primary_key=>true} 781 rescue Sequel::DatabaseError => e 782 if db.database_type == :mysql && e.message =~ /max key length/ 783 # Handle case where MySQL is used with utf8mb4 charset default, which 784 # only allows a maximum length of about 190 characters for string 785 # primary keys due to InnoDB limitations. 786 db.create_table(table){String c, :primary_key=>true, :size=>190} 787 else 788 raise e 789 end 790 end 791 if db.table_exists?(:schema_info) and vha = db[:schema_info].all and vha.length == 1 and 792 vha.first.keys == [:version] and vha.first.values.first.is_a?(Integer) 793 convert_from_schema_info 794 end 795 elsif !ds.columns.include?(c) 796 raise(Error, "Migrator table #{table} does not contain column #{c}") 797 end 798 ds 799 end