class Sequel::IntegerMigrator
The default migrator, recommended in most cases. Uses a simple incrementing version number starting with 1, where missing or duplicate migration file versions are not allowed. Part of the migration extension.
Constants
- Error
Attributes
The current version for this migrator
The direction of the migrator, either :up or :down
The migrations used by this migrator
Public Class Methods
Source
# File lib/sequel/extensions/migration.rb 555 def initialize(db, directory, opts=OPTS) 556 super 557 @current = opts[:current] || current_migration_version 558 559 latest_version = latest_migration_version 560 @target = if opts[:target] 561 opts[:target] 562 elsif opts[:relative] 563 @current + opts[:relative] 564 else 565 latest_version 566 end 567 568 raise(Error, "No target and/or latest version available, probably because no migration files found or filenames don't follow the migration filename convention") unless target && latest_version 569 570 if @target > latest_version 571 @target = latest_version 572 elsif @target < 0 573 @target = 0 574 end 575 576 @direction = current < target ? :up : :down 577 578 if @direction == :down && @current >= @files.length && !@allow_missing_migration_files 579 raise Migrator::Error, "Missing migration version(s) needed to migrate down to target version (current: #{current}, target: #{target})" 580 end 581 582 @migrations = get_migrations 583 end
Set up all state for the migrator instance
Sequel::Migrator::new
Public Instance Methods
Source
# File lib/sequel/extensions/migration.rb 586 def is_current? 587 current_migration_version == target 588 end
The integer migrator is current if the current version is the same as the target version.
Source
# File lib/sequel/extensions/migration.rb 591 def run 592 migrations.zip(version_numbers).each do |m, v| 593 timer = Sequel.start_timer 594 db.log_info("Begin applying migration version #{v}, direction: #{direction}") 595 checked_transaction(m) do 596 m.apply(db, direction) 597 set_migration_version(up? ? v : v-1) 598 end 599 db.log_info("Finished applying migration version #{v}, direction: #{direction}, took #{sprintf('%0.6f', Sequel.elapsed_seconds_since(timer))} seconds") 600 end 601 602 target 603 end
Apply all migrations on the database
Private Instance Methods
Source
# File lib/sequel/extensions/migration.rb 609 def current_migration_version 610 ds.get(column) || 0 611 end
Gets the current migration version stored in the database. If no version number is stored, 0 is returned.
Source
# File lib/sequel/extensions/migration.rb 614 def default_schema_column 615 :version 616 end
The default column storing schema version.
Source
# File lib/sequel/extensions/migration.rb 619 def default_schema_table 620 :schema_info 621 end
The default table storing schema version.
Source
# File lib/sequel/extensions/migration.rb 624 def get_migration_files 625 files = [] 626 Dir.new(directory).each do |file| 627 next unless MIGRATION_FILE_PATTERN.match(file) 628 version = migration_version_from_file(file) 629 if version >= 20000101 630 raise Migrator::Error, "Migration number too large, must use TimestampMigrator: #{file}" 631 end 632 raise(Error, "Duplicate migration version: #{version}") if files[version] 633 files[version] = File.join(directory, file) 634 end 635 1.upto(files.length - 1){|i| raise(Error, "Missing migration version: #{i}") unless files[i]} unless @allow_missing_migration_files 636 files 637 end
Returns any found migration files in the supplied directory.
Source
# File lib/sequel/extensions/migration.rb 641 def get_migrations 642 version_numbers.map{|n| load_migration_file(files[n])} 643 end
Returns a list of migration classes filtered for the migration range and ordered according to the migration direction.
Source
# File lib/sequel/extensions/migration.rb 646 def latest_migration_version 647 l = files.last 648 l ? migration_version_from_file(File.basename(l)) : nil 649 end
Returns the latest version available in the specified directory.
Source
# File lib/sequel/extensions/migration.rb 653 def schema_dataset 654 c = column 655 ds = db.from(table) 656 db.create_table?(table){Integer c, :default=>0, :null=>false} 657 unless ds.columns.include?(c) 658 db.alter_table(table){add_column c, Integer, :default=>0, :null=>false} 659 end 660 ds.insert(c=>0) if ds.empty? 661 raise(Error, "More than 1 row in migrator table") if ds.count > 1 662 ds 663 end
Returns the dataset for the schema_info table. If no such table exists, it is automatically created.
Source
# File lib/sequel/extensions/migration.rb 666 def set_migration_version(version) 667 ds.update(column=>version) 668 end
Sets the current migration version stored in the database.
Source
# File lib/sequel/extensions/migration.rb 671 def up? 672 direction == :up 673 end
Whether or not this is an up migration
Source
# File lib/sequel/extensions/migration.rb 678 def version_numbers 679 @version_numbers ||= begin 680 versions = files. 681 compact. 682 map{|f| migration_version_from_file(File.basename(f))}. 683 select{|v| up? ? (v > current && v <= target) : (v <= current && v > target)}. 684 sort 685 versions.reverse! unless up? 686 versions 687 end 688 end
An array of numbers corresponding to the migrations, so that each number in the array is the migration version that will be in affect after the migration is run.