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 516 def initialize(db, directory, opts=OPTS) super @current = opts[:current] || current_migration_version raise(Error, "No current version available") unless current latest_version = latest_migration_version @target = if opts[:target] opts[:target] elsif opts[:relative] @current + opts[:relative] else latest_version end raise(Error, "No target version available, probably because no migration files found or filenames don't follow the migration filename convention") unless target if @target > latest_version @target = latest_version elsif @target < 0 @target = 0 end @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 544 def is_current? current_migration_version == target end
Apply all migrations on the database
# File lib/sequel/extensions/migration.rb, line 549 def run migrations.zip(version_numbers).each do |m, v| timer = Sequel.start_timer db.log_info("Begin applying migration version #{v}, direction: #{direction}") checked_transaction(m) do m.apply(db, direction) set_migration_version(up? ? v : v-1) end db.log_info("Finished applying migration version #{v}, direction: #{direction}, took #{sprintf('%0.6f', Sequel.elapsed_seconds_since(timer))} 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 567 def current_migration_version ds.get(column) || 0 end
The default column storing schema version.
# File lib/sequel/extensions/migration.rb, line 572 def default_schema_column :version end
The default table storing schema version.
# File lib/sequel/extensions/migration.rb, line 577 def default_schema_table :schema_info end
Returns any found migration files in the supplied directory.
# File lib/sequel/extensions/migration.rb, line 582 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 599 def get_migrations version_numbers.map{|n| load_migration_file(files[n])} end
Returns the latest version available in the specified directory.
# File lib/sequel/extensions/migration.rb, line 604 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 611 def schema_dataset c = column ds = db.from(table) db.create_table?(table){Integer c, :default=>0, :null=>false} unless 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 624 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 629 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 636 def version_numbers @version_numbers ||= begin versions = files. compact. map{|f| migration_version_from_file(File.basename(f))}. select{|v| up? ? (v > current && v <= target) : (v <= current && v > target)}. sort versions.reverse! unless up? versions end end