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
Set up all state for the migrator instance
Sequel::Migrator::new
# File lib/sequel/extensions/migration.rb 538 def initialize(db, directory, opts=OPTS) 539 super 540 @current = opts[:current] || current_migration_version 541 542 latest_version = latest_migration_version 543 @target = if opts[:target] 544 opts[:target] 545 elsif opts[:relative] 546 @current + opts[:relative] 547 else 548 latest_version 549 end 550 551 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 552 553 if @target > latest_version 554 @target = latest_version 555 elsif @target < 0 556 @target = 0 557 end 558 559 @direction = current < target ? :up : :down 560 561 if @direction == :down && @current >= @files.length && !@allow_missing_migration_files 562 raise Migrator::Error, "Missing migration version(s) needed to migrate down to target version (current: #{current}, target: #{target})" 563 end 564 565 @migrations = get_migrations 566 end
Public Instance Methods
The integer migrator is current if the current version is the same as the target version.
# File lib/sequel/extensions/migration.rb 569 def is_current? 570 current_migration_version == target 571 end
Apply all migrations on the database
# File lib/sequel/extensions/migration.rb 574 def run 575 migrations.zip(version_numbers).each do |m, v| 576 timer = Sequel.start_timer 577 db.log_info("Begin applying migration version #{v}, direction: #{direction}") 578 checked_transaction(m) do 579 m.apply(db, direction) 580 set_migration_version(up? ? v : v-1) 581 end 582 db.log_info("Finished applying migration version #{v}, direction: #{direction}, took #{sprintf('%0.6f', Sequel.elapsed_seconds_since(timer))} seconds") 583 end 584 585 target 586 end
Private Instance Methods
Gets the current migration version stored in the database. If no version number is stored, 0 is returned.
# File lib/sequel/extensions/migration.rb 592 def current_migration_version 593 ds.get(column) || 0 594 end
The default column storing schema version.
# File lib/sequel/extensions/migration.rb 597 def default_schema_column 598 :version 599 end
The default table storing schema version.
# File lib/sequel/extensions/migration.rb 602 def default_schema_table 603 :schema_info 604 end
Returns any found migration files in the supplied directory.
# File lib/sequel/extensions/migration.rb 607 def get_migration_files 608 files = [] 609 Dir.new(directory).each do |file| 610 next unless MIGRATION_FILE_PATTERN.match(file) 611 version = migration_version_from_file(file) 612 if version >= 20000101 613 raise Migrator::Error, "Migration number too large, must use TimestampMigrator: #{file}" 614 end 615 raise(Error, "Duplicate migration version: #{version}") if files[version] 616 files[version] = File.join(directory, file) 617 end 618 1.upto(files.length - 1){|i| raise(Error, "Missing migration version: #{i}") unless files[i]} unless @allow_missing_migration_files 619 files 620 end
Returns a list of migration classes filtered for the migration range and ordered according to the migration direction.
# File lib/sequel/extensions/migration.rb 624 def get_migrations 625 version_numbers.map{|n| load_migration_file(files[n])} 626 end
Returns the latest version available in the specified directory.
# File lib/sequel/extensions/migration.rb 629 def latest_migration_version 630 l = files.last 631 l ? migration_version_from_file(File.basename(l)) : nil 632 end
Returns the dataset for the schema_info table. If no such table exists, it is automatically created.
# File lib/sequel/extensions/migration.rb 636 def schema_dataset 637 c = column 638 ds = db.from(table) 639 db.create_table?(table){Integer c, :default=>0, :null=>false} 640 unless ds.columns.include?(c) 641 db.alter_table(table){add_column c, Integer, :default=>0, :null=>false} 642 end 643 ds.insert(c=>0) if ds.empty? 644 raise(Error, "More than 1 row in migrator table") if ds.count > 1 645 ds 646 end
Sets the current migration version stored in the database.
# File lib/sequel/extensions/migration.rb 649 def set_migration_version(version) 650 ds.update(column=>version) 651 end
Whether or not this is an up migration
# File lib/sequel/extensions/migration.rb 654 def up? 655 direction == :up 656 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.
# File lib/sequel/extensions/migration.rb 661 def version_numbers 662 @version_numbers ||= begin 663 versions = files. 664 compact. 665 map{|f| migration_version_from_file(File.basename(f))}. 666 select{|v| up? ? (v > current && v <= target) : (v <= current && v > target)}. 667 sort 668 versions.reverse! unless up? 669 versions 670 end 671 end