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