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