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.
Set up all state for the migrator instance
# File lib/sequel/extensions/migration.rb, line 492 492: def initialize(db, directory, opts={}) 493: super 494: @target = opts[:target] || latest_migration_version 495: @current = opts[:current] || current_migration_version 496: 497: raise(Error, "No current version available") unless current 498: raise(Error, "No target version available, probably because no migration files found or filenames don't follow the migration filename convention") unless target 499: 500: @direction = current < target ? :up : :down 501: @migrations = get_migrations 502: end
The integer migrator is current if the current version is the same as the target version.
# File lib/sequel/extensions/migration.rb, line 505 505: def is_current? 506: current_migration_version == target 507: end
Apply all migrations on the database
# File lib/sequel/extensions/migration.rb, line 510 510: def run 511: migrations.zip(version_numbers).each do |m, v| 512: t = Time.now 513: lv = up? ? v : v + 1 514: db.log_info("Begin applying migration version #{lv}, direction: #{direction}") 515: checked_transaction(m) do 516: m.apply(db, direction) 517: set_migration_version(v) 518: end 519: db.log_info("Finished applying migration version #{lv}, direction: #{direction}, took #{sprintf('%0.6f', Time.now - t)} seconds") 520: end 521: 522: target 523: end
Gets the current migration version stored in the database. If no version number is stored, 0 is returned.
# File lib/sequel/extensions/migration.rb, line 529 529: def current_migration_version 530: ds.get(column) || 0 531: end
Returns any found migration files in the supplied directory.
# File lib/sequel/extensions/migration.rb, line 534 534: def get_migration_files 535: files = [] 536: Dir.new(directory).each do |file| 537: next unless MIGRATION_FILE_PATTERN.match(file) 538: version = migration_version_from_file(file) 539: if version >= 20000101 540: raise Migrator::Error, "Migration number too large, must use TimestampMigrator: #{file}" 541: end 542: raise(Error, "Duplicate migration version: #{version}") if files[version] 543: files[version] = File.join(directory, file) 544: end 545: 1.upto(files.length - 1){|i| raise(Error, "Missing migration version: #{i}") unless files[i]} 546: files 547: 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, line 551 551: def get_migrations 552: remove_migration_classes 553: 554: # load migration files 555: files[up? ? (current + 1)..target : (target + 1)..current].compact.each{|f| load(f)} 556: 557: # get migration classes 558: classes = Migration.descendants 559: up? ? classes : classes.reverse 560: end
Returns the latest version available in the specified directory.
# File lib/sequel/extensions/migration.rb, line 563 563: def latest_migration_version 564: l = files.last 565: l ? migration_version_from_file(File.basename(l)) : nil 566: end
Returns the dataset for the schema_info table. If no such table exists, it is automatically created.
# File lib/sequel/extensions/migration.rb, line 570 570: def schema_dataset 571: c = column 572: ds = db.from(table) 573: if !db.table_exists?(table) 574: db.create_table(table){Integer c, :default=>0, :null=>false} 575: elsif !ds.columns.include?(c) 576: db.alter_table(table){add_column c, Integer, :default=>0, :null=>false} 577: end 578: ds.insert(c=>0) if ds.empty? 579: raise(Error, "More than 1 row in migrator table") if ds.count > 1 580: ds 581: end
Sets the current migration version stored in the database.
# File lib/sequel/extensions/migration.rb, line 584 584: def set_migration_version(version) 585: ds.update(column=>version) 586: end
Whether or not this is an up migration
# File lib/sequel/extensions/migration.rb, line 589 589: def up? 590: direction == :up 591: 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, line 596 596: def version_numbers 597: up? ? ((current+1)..target).to_a : (target..(current - 1)).to_a.reverse 598: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.