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.
The current version for this migrator
The direction of the migrator, either :up or :down
The migrations used by this migrator
Set up all state for the migrator instance
# File lib/sequel/extensions/migration.rb, line 505 def initialize(db, directory, opts={}) super @target = opts[:target] || latest_migration_version @current = opts[:current] || current_migration_version raise(Error, "No current version available") unless current raise(Error, "No target version available, probably because no migration files found or filenames don't follow the migration filename convention") unless target @direction = current < target ? :up : :down @migrations = get_migrations end
The integer migrator is current if the current version is the same as the target version.
# File lib/sequel/extensions/migration.rb, line 518 def is_current? current_migration_version == target end
Apply all migrations on the database
# File lib/sequel/extensions/migration.rb, line 523 def run migrations.zip(version_numbers).each do |m, v| t = Time.now lv = up? ? v : v + 1 db.log_info("Begin applying migration version #{lv}, direction: #{direction}") checked_transaction(m) do m.apply(db, direction) set_migration_version(v) end db.log_info("Finished applying migration version #{lv}, direction: #{direction}, took #{sprintf('%0.6f', Time.now - t)} seconds") end target 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 542 def current_migration_version ds.get(column) || 0 end
Returns any found migration files in the supplied directory.
# File lib/sequel/extensions/migration.rb, line 547 def get_migration_files files = [] Dir.new(directory).each do |file| next unless MIGRATION_FILE_PATTERN.match(file) version = migration_version_from_file(file) if version >= 20000101 raise Migrator::Error, "Migration number too large, must use TimestampMigrator: #{file}" end raise(Error, "Duplicate migration version: #{version}") if files[version] files[version] = File.join(directory, file) end 1.upto(files.length - 1){|i| raise(Error, "Missing migration version: #{i}") unless files[i]} unless @allow_missing_migration_files files 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 564 def get_migrations remove_migration_classes # load migration files files[up? ? (current + 1)..target : (target + 1)..current].compact.each{|f| load(f)} # get migration classes classes = Migration.descendants up? ? classes : classes.reverse end
Returns the latest version available in the specified directory.
# File lib/sequel/extensions/migration.rb, line 576 def latest_migration_version l = files.last l ? migration_version_from_file(File.basename(l)) : nil 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 583 def schema_dataset c = column ds = db.from(table) if !db.table_exists?(table) db.create_table(table){Integer c, :default=>0, :null=>false} elsif !ds.columns.include?(c) db.alter_table(table){add_column c, Integer, :default=>0, :null=>false} end ds.insert(c=>0) if ds.empty? raise(Error, "More than 1 row in migrator table") if ds.count > 1 ds end
Sets the current migration version stored in the database.
# File lib/sequel/extensions/migration.rb, line 597 def set_migration_version(version) ds.update(column=>version) end
Whether or not this is an up migration
# File lib/sequel/extensions/migration.rb, line 602 def up? direction == :up 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 609 def version_numbers up? ? ((current+1)..target).to_a : (target..(current - 1)).to_a.reverse end