module Mongration

Constants

VERSION

Public Instance Methods

configuration() click to toggle source
# File lib/mongration.rb, line 102
def configuration
  @configuration ||= Configuration.new
end
configure() { |configuration| ... } click to toggle source
# File lib/mongration.rb, line 98
def configure
  yield configuration if block_given?
end
create_migration(name, options = {}) click to toggle source

Creates a migration with the given name

@return [String] name of the file created

# File lib/mongration.rb, line 63
def create_migration(name, options = {})
  CreateMigration.perform(
    name,
    options
  )
end
migrate(version = nil) click to toggle source

Performs the migrations. If no version is provided, all pending migrations will be run. If a version is provided, migrations will be run to that version (either up or down).

@param [String, nil] version

@return [Boolean] true if migration was successful, false otherwise

# File lib/mongration.rb, line 26
def migrate(version = nil)
  pending = ->(_) { File.pending.map(&:version).include?(_) }
  migrated = ->(_) { File.migrated.map(&:version).include?(_) }

  case version
  when nil
    files = File.pending
    Migrate::Up.new(files).perform

  when pending
    files = File.pending.select { |f| f.version <= version }
    Migrate::Up.new(files).perform

  when migrated
    files = File.migrated.select { |f| f.version > version }.reverse
    Migrate::Down.new(files).perform

  else
    out.puts("Invalid Version: #{version} does not exist.")
  end
end
out() click to toggle source
# File lib/mongration.rb, line 90
def out
  if configuration.silent?
    @null_output ||= NullOutput.new
  else
    $stdout
  end
end
rollback(step = 1) click to toggle source

Rolls back (calls ‘down`) on the most recent migration(s).

@param [Integer] number of migrations to rollback

@return [void]

# File lib/mongration.rb, line 54
def rollback(step = 1)
  files = File.migrated.reverse.first(step)
  Migrate::Down.new(files).perform
end
status() click to toggle source

Returns the direction (up if it has been run, down otherwise), migration ID (version), and description of the migration.

@return [Array] migration statuses

# File lib/mongration.rb, line 86
def status
  Status.migrations
end
version() click to toggle source

Returns the version of most recently run migration. If there are no migrations that have run (all migrations are pending), it returns ‘0’.

@return [String] version

# File lib/mongration.rb, line 74
def version
  if File.migrated.any?
    File.migrated.last.version
  else
    '0'
  end
end