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 550 def initialize(db, directory, opts=OPTS) 551 super 552 @current = opts[:current] || current_migration_version 553 554 latest_version = latest_migration_version 555 @target = if opts[:target] 556 opts[:target] 557 elsif opts[:relative] 558 @current + opts[:relative] 559 else 560 latest_version 561 end 562 563 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 564 565 if @target > latest_version 566 @target = latest_version 567 elsif @target < 0 568 @target = 0 569 end 570 571 @direction = current < target ? :up : :down 572 573 if @direction == :down && @current >= @files.length && !@allow_missing_migration_files 574 raise Migrator::Error, "Missing migration version(s) needed to migrate down to target version (current: #{current}, target: #{target})" 575 end 576 577 @migrations = get_migrations 578 end
Set up all state for the migrator instance
Sequel::Migrator::new
Public Instance Methods
Source
# File lib/sequel/extensions/migration.rb 581 def is_current? 582 current_migration_version == target 583 end
The integer migrator is current if the current version is the same as the target version.
Source
# File lib/sequel/extensions/migration.rb 586 def run 587 migrations.zip(version_numbers).each do |m, v| 588 timer = Sequel.start_timer 589 db.log_info("Begin applying migration version #{v}, direction: #{direction}") 590 checked_transaction(m) do 591 m.apply(db, direction) 592 set_migration_version(up? ? v : v-1) 593 end 594 db.log_info("Finished applying migration version #{v}, direction: #{direction}, took #{sprintf('%0.6f', Sequel.elapsed_seconds_since(timer))} seconds") 595 end 596 597 target 598 end
Apply all migrations on the database
Private Instance Methods
Source
# File lib/sequel/extensions/migration.rb 604 def current_migration_version 605 ds.get(column) || 0 606 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 609 def default_schema_column 610 :version 611 end
The default column storing schema version.
Source
# File lib/sequel/extensions/migration.rb 614 def default_schema_table 615 :schema_info 616 end
The default table storing schema version.
Source
# File lib/sequel/extensions/migration.rb 619 def get_migration_files 620 files = [] 621 Dir.new(directory).each do |file| 622 next unless MIGRATION_FILE_PATTERN.match(file) 623 version = migration_version_from_file(file) 624 if version >= 20000101 625 raise Migrator::Error, "Migration number too large, must use TimestampMigrator: #{file}" 626 end 627 raise(Error, "Duplicate migration version: #{version}") if files[version] 628 files[version] = File.join(directory, file) 629 end 630 1.upto(files.length - 1){|i| raise(Error, "Missing migration version: #{i}") unless files[i]} unless @allow_missing_migration_files 631 files 632 end
Returns any found migration files in the supplied directory.
Source
# File lib/sequel/extensions/migration.rb 636 def get_migrations 637 version_numbers.map{|n| load_migration_file(files[n])} 638 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 641 def latest_migration_version 642 l = files.last 643 l ? migration_version_from_file(File.basename(l)) : nil 644 end
Returns the latest version available in the specified directory.
Source
# File lib/sequel/extensions/migration.rb 648 def schema_dataset 649 c = column 650 ds = db.from(table) 651 db.create_table?(table){Integer c, :default=>0, :null=>false} 652 unless ds.columns.include?(c) 653 db.alter_table(table){add_column c, Integer, :default=>0, :null=>false} 654 end 655 ds.insert(c=>0) if ds.empty? 656 raise(Error, "More than 1 row in migrator table") if ds.count > 1 657 ds 658 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 661 def set_migration_version(version) 662 ds.update(column=>version) 663 end
Sets the current migration version stored in the database.
Source
# File lib/sequel/extensions/migration.rb 666 def up? 667 direction == :up 668 end
Whether or not this is an up migration
Source
# File lib/sequel/extensions/migration.rb 673 def version_numbers 674 @version_numbers ||= begin 675 versions = files. 676 compact. 677 map{|f| migration_version_from_file(File.basename(f))}. 678 select{|v| up? ? (v > current && v <= target) : (v <= current && v > target)}. 679 sort 680 versions.reverse! unless up? 681 versions 682 end 683 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.