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 516 def initialize(db, directory, opts=OPTS) 517 super 518 @current = opts[:current] || current_migration_version 519 raise(Error, "No current version available") unless current 520 521 latest_version = latest_migration_version 522 523 @target = if opts[:target] 524 opts[:target] 525 elsif opts[:relative] 526 @current + opts[:relative] 527 else 528 latest_version 529 end 530 531 raise(Error, "No target version available, probably because no migration files found or filenames don't follow the migration filename convention") unless target 532 533 if @target > latest_version 534 @target = latest_version 535 elsif @target < 0 536 @target = 0 537 end 538 539 @direction = current < target ? :up : :down 540 @migrations = get_migrations 541 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 544 def is_current? 545 current_migration_version == target 546 end
Apply all migrations on the database
# File lib/sequel/extensions/migration.rb 549 def run 550 migrations.zip(version_numbers).each do |m, v| 551 timer = Sequel.start_timer 552 db.log_info("Begin applying migration version #{v}, direction: #{direction}") 553 checked_transaction(m) do 554 m.apply(db, direction) 555 set_migration_version(up? ? v : v-1) 556 end 557 db.log_info("Finished applying migration version #{v}, direction: #{direction}, took #{sprintf('%0.6f', Sequel.elapsed_seconds_since(timer))} seconds") 558 end 559 560 target 561 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 567 def current_migration_version 568 ds.get(column) || 0 569 end
The default column storing schema version.
# File lib/sequel/extensions/migration.rb 572 def default_schema_column 573 :version 574 end
The default table storing schema version.
# File lib/sequel/extensions/migration.rb 577 def default_schema_table 578 :schema_info 579 end
Returns any found migration files in the supplied directory.
# File lib/sequel/extensions/migration.rb 582 def get_migration_files 583 files = [] 584 Dir.new(directory).each do |file| 585 next unless MIGRATION_FILE_PATTERN.match(file) 586 version = migration_version_from_file(file) 587 if version >= 20000101 588 raise Migrator::Error, "Migration number too large, must use TimestampMigrator: #{file}" 589 end 590 raise(Error, "Duplicate migration version: #{version}") if files[version] 591 files[version] = File.join(directory, file) 592 end 593 1.upto(files.length - 1){|i| raise(Error, "Missing migration version: #{i}") unless files[i]} unless @allow_missing_migration_files 594 files 595 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 599 def get_migrations 600 version_numbers.map{|n| load_migration_file(files[n])} 601 end
Returns the latest version available in the specified directory.
# File lib/sequel/extensions/migration.rb 604 def latest_migration_version 605 l = files.last 606 l ? migration_version_from_file(File.basename(l)) : nil 607 end
Returns the dataset for the schema_info table. If no such table exists, it is automatically created.
# File lib/sequel/extensions/migration.rb 611 def schema_dataset 612 c = column 613 ds = db.from(table) 614 db.create_table?(table){Integer c, :default=>0, :null=>false} 615 unless ds.columns.include?(c) 616 db.alter_table(table){add_column c, Integer, :default=>0, :null=>false} 617 end 618 ds.insert(c=>0) if ds.empty? 619 raise(Error, "More than 1 row in migrator table") if ds.count > 1 620 ds 621 end
Sets the current migration version stored in the database.
# File lib/sequel/extensions/migration.rb 624 def set_migration_version(version) 625 ds.update(column=>version) 626 end
Whether or not this is an up migration
# File lib/sequel/extensions/migration.rb 629 def up? 630 direction == :up 631 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 636 def version_numbers 637 @version_numbers ||= begin 638 versions = files. 639 compact. 640 map{|f| migration_version_from_file(File.basename(f))}. 641 select{|v| up? ? (v > current && v <= target) : (v <= current && v > target)}. 642 sort 643 versions.reverse! unless up? 644 versions 645 end 646 end