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