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 525 def initialize(db, directory, opts=OPTS) 526 super 527 @current = opts[:current] || current_migration_version 528 529 latest_version = latest_migration_version 530 @target = if opts[:target] 531 opts[:target] 532 elsif opts[:relative] 533 @current + opts[:relative] 534 else 535 latest_version 536 end 537 538 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 539 540 if @target > latest_version 541 @target = latest_version 542 elsif @target < 0 543 @target = 0 544 end 545 546 @direction = current < target ? :up : :down 547 548 if @direction == :down && @current >= @files.length && !@allow_missing_migration_files 549 raise Migrator::Error, "Missing migration version(s) needed to migrate down to target version (current: #{current}, target: #{target})" 550 end 551 552 @migrations = get_migrations 553 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 556 def is_current? 557 current_migration_version == target 558 end
Apply all migrations on the database
# File lib/sequel/extensions/migration.rb 561 def run 562 migrations.zip(version_numbers).each do |m, v| 563 timer = Sequel.start_timer 564 db.log_info("Begin applying migration version #{v}, direction: #{direction}") 565 checked_transaction(m) do 566 m.apply(db, direction) 567 set_migration_version(up? ? v : v-1) 568 end 569 db.log_info("Finished applying migration version #{v}, direction: #{direction}, took #{sprintf('%0.6f', Sequel.elapsed_seconds_since(timer))} seconds") 570 end 571 572 target 573 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 579 def current_migration_version 580 ds.get(column) || 0 581 end
The default column storing schema version.
# File lib/sequel/extensions/migration.rb 584 def default_schema_column 585 :version 586 end
The default table storing schema version.
# File lib/sequel/extensions/migration.rb 589 def default_schema_table 590 :schema_info 591 end
Returns any found migration files in the supplied directory.
# File lib/sequel/extensions/migration.rb 594 def get_migration_files 595 files = [] 596 Dir.new(directory).each do |file| 597 next unless MIGRATION_FILE_PATTERN.match(file) 598 version = migration_version_from_file(file) 599 if version >= 20000101 600 raise Migrator::Error, "Migration number too large, must use TimestampMigrator: #{file}" 601 end 602 raise(Error, "Duplicate migration version: #{version}") if files[version] 603 files[version] = File.join(directory, file) 604 end 605 1.upto(files.length - 1){|i| raise(Error, "Missing migration version: #{i}") unless files[i]} unless @allow_missing_migration_files 606 files 607 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 611 def get_migrations 612 version_numbers.map{|n| load_migration_file(files[n])} 613 end
Returns the latest version available in the specified directory.
# File lib/sequel/extensions/migration.rb 616 def latest_migration_version 617 l = files.last 618 l ? migration_version_from_file(File.basename(l)) : nil 619 end
Returns the dataset for the schema_info table. If no such table exists, it is automatically created.
# File lib/sequel/extensions/migration.rb 623 def schema_dataset 624 c = column 625 ds = db.from(table) 626 db.create_table?(table){Integer c, :default=>0, :null=>false} 627 unless ds.columns.include?(c) 628 db.alter_table(table){add_column c, Integer, :default=>0, :null=>false} 629 end 630 ds.insert(c=>0) if ds.empty? 631 raise(Error, "More than 1 row in migrator table") if ds.count > 1 632 ds 633 end
Sets the current migration version stored in the database.
# File lib/sequel/extensions/migration.rb 636 def set_migration_version(version) 637 ds.update(column=>version) 638 end
Whether or not this is an up migration
# File lib/sequel/extensions/migration.rb 641 def up? 642 direction == :up 643 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 648 def version_numbers 649 @version_numbers ||= begin 650 versions = files. 651 compact. 652 map{|f| migration_version_from_file(File.basename(f))}. 653 select{|v| up? ? (v > current && v <= target) : (v <= current && v > target)}. 654 sort 655 versions.reverse! unless up? 656 versions 657 end 658 end