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