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