Module: Mongration

Extended by:
Mongration
Included in:
Mongration
Defined in:
lib/mongration.rb,
lib/mongration/file.rb,
lib/mongration/errors.rb,
lib/mongration/status.rb,
lib/mongration/migrate.rb,
lib/mongration/version.rb,
lib/mongration/rollback.rb,
lib/mongration/migration.rb,
lib/mongration/migrate/up.rb,
lib/mongration/null_output.rb,
lib/mongration/migrate/down.rb,
lib/mongration/migrate/move.rb,
lib/mongration/configuration.rb,
lib/mongration/create_migration.rb,
lib/mongration/migrate/direction.rb,
lib/mongration/create_migration/migration_file_writer.rb

Defined Under Namespace

Modules: Migrate, Status Classes: Configuration, CreateMigration, Errors, File, Migration, NullOutput, Rollback

Constant Summary collapse

VERSION =
'0.0.7'

Instance Method Summary collapse

Instance Method Details

#configurationObject



102
103
104
# File 'lib/mongration.rb', line 102

def configuration
  @configuration ||= Configuration.new
end

#configure {|configuration| ... } ⇒ Object

Yields:



98
99
100
# File 'lib/mongration.rb', line 98

def configure
  yield configuration if block_given?
end

#create_migration(name, options = {}) ⇒ String

Creates a migration with the given name

Returns:

  • (String)

    name of the file created



63
64
65
66
67
68
# File 'lib/mongration.rb', line 63

def create_migration(name, options = {})
  CreateMigration.perform(
    name,
    options
  )
end

#migrate(version = nil) ⇒ Boolean

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).

Parameters:

  • version (String, nil) (defaults to: nil)

Returns:

  • (Boolean)

    true if migration was successful, false otherwise



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# 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

#outObject



90
91
92
93
94
95
96
# File 'lib/mongration.rb', line 90

def out
  if configuration.silent?
    @null_output ||= NullOutput.new
  else
    $stdout
  end
end

#rollback(step = 1) ⇒ void

This method returns an undefined value.

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

Parameters:

  • number (Integer)

    of migrations to rollback



54
55
56
57
# File 'lib/mongration.rb', line 54

def rollback(step = 1)
  files = File.migrated.reverse.first(step)
  Migrate::Down.new(files).perform
end

#statusArray

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

Returns:

  • (Array)

    migration statuses



86
87
88
# File 'lib/mongration.rb', line 86

def status
  Status.migrations
end

#versionString

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

Returns:

  • (String)

    version



74
75
76
77
78
79
80
# File 'lib/mongration.rb', line 74

def version
  if File.migrated.any?
    File.migrated.last.version
  else
    '0'
  end
end