The migrator used if any migration file version appears to be a timestamp. 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.
Set up all state for the migrator instance
# File lib/sequel/extensions/migration.rb, line 619 619: def initialize(db, directory, opts={}) 620: super 621: @target = opts[:target] 622: @applied_migrations = get_applied_migrations 623: @migration_tuples = get_migration_tuples 624: end
The timestamp migrator is current if there are no migrations to apply in either direction.
# File lib/sequel/extensions/migration.rb, line 628 628: def is_current? 629: migration_tuples.empty? 630: end
Apply all migration tuples on the database
# File lib/sequel/extensions/migration.rb, line 633 633: def run 634: migration_tuples.each do |m, f, direction| 635: t = Time.now 636: db.log_info("Begin applying migration #{f}, direction: #{direction}") 637: checked_transaction(m) do 638: m.apply(db, direction) 639: fi = f.downcase 640: direction == :up ? ds.insert(column=>fi) : ds.filter(column=>fi).delete 641: end 642: db.log_info("Finished applying migration #{f}, direction: #{direction}, took #{sprintf('%0.6f', Time.now - t)} seconds") 643: end 644: nil 645: end
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, line 651 651: def convert_from_schema_info 652: v = db[IntegerMigrator::DEFAULT_SCHEMA_TABLE].get(IntegerMigrator::DEFAULT_SCHEMA_COLUMN) 653: ds = db.from(table) 654: files.each do |path| 655: f = File.basename(path) 656: if migration_version_from_file(f) <= v 657: ds.insert(column=>f) 658: end 659: end 660: end
Returns filenames of all applied migrations
# File lib/sequel/extensions/migration.rb, line 663 663: def get_applied_migrations 664: am = ds.select_order_map(column) 665: missing_migration_files = am - files.map{|f| File.basename(f).downcase} 666: raise(Error, "Applied migration files not in file system: #{missing_migration_files.join(', ')}") if missing_migration_files.length > 0 667: am 668: end
Returns any migration files found in the migrator’s directory.
# File lib/sequel/extensions/migration.rb, line 671 671: def get_migration_files 672: files = [] 673: Dir.new(directory).each do |file| 674: next unless MIGRATION_FILE_PATTERN.match(file) 675: files << File.join(directory, file) 676: end 677: files.sort_by{|f| MIGRATION_FILE_PATTERN.match(File.basename(f))[1].to_i} 678: end
Returns tuples of migration, filename, and direction
# File lib/sequel/extensions/migration.rb, line 681 681: def get_migration_tuples 682: remove_migration_classes 683: up_mts = [] 684: down_mts = [] 685: ms = Migration.descendants 686: files.each do |path| 687: f = File.basename(path) 688: fi = f.downcase 689: if target 690: if migration_version_from_file(f) > target 691: if applied_migrations.include?(fi) 692: load(path) 693: down_mts << [ms.last, f, :down] 694: end 695: elsif !applied_migrations.include?(fi) 696: load(path) 697: up_mts << [ms.last, f, :up] 698: end 699: elsif !applied_migrations.include?(fi) 700: load(path) 701: up_mts << [ms.last, f, :up] 702: end 703: end 704: up_mts + down_mts.reverse 705: end
Returns the dataset for the schema_migrations table. If no such table exists, it is automatically created.
# File lib/sequel/extensions/migration.rb, line 709 709: def schema_dataset 710: c = column 711: ds = db.from(table) 712: if !db.table_exists?(table) 713: db.create_table(table){String c, :primary_key=>true} 714: if db.table_exists?(:schema_info) and vha = db[:schema_info].all and vha.length == 1 and 715: vha.first.keys == [:version] and vha.first.values.first.is_a?(Integer) 716: convert_from_schema_info 717: end 718: elsif !ds.columns.include?(c) 719: raise(Error, "Migrator table #{table} does not contain column #{c}") 720: end 721: ds 722: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.