Class: Ronin::Database::Migrations::Graph

Inherits:
Object
  • Object
show all
Includes:
Enumerable, TSort
Defined in:
lib/ronin/database/migrations/graph.rb

Overview

Sorts and runs Migrations based on their dependencies.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGraph

Creates a new Migration Graph.

Since:

  • 1.0.1



49
50
51
52
# File 'lib/ronin/database/migrations/graph.rb', line 49

def initialize
  @migrations = {}
  @positions = {}
end

Instance Attribute Details

#migrationsObject (readonly)

The migrations in the graph



39
40
41
# File 'lib/ronin/database/migrations/graph.rb', line 39

def migrations
  @migrations
end

#positionsObject (readonly)

The mapping of position numbers to migration names



42
43
44
# File 'lib/ronin/database/migrations/graph.rb', line 42

def positions
  @positions
end

Instance Method Details

#[](position_or_name) ⇒ Migration

Provides direct access to migrations.

Parameters:

  • position_or_name (Integer, Symbol)

    The position or name of the migration.

Returns:

Raises:

  • (KeyError)

    There was no migration defined with the given position or name.



78
79
80
81
82
83
84
85
86
# File 'lib/ronin/database/migrations/graph.rb', line 78

def [](position_or_name)
  name = name_of(position_or_name)

  unless @migrations.has_key?(name)
    raise(UnknownMigration,"unknown migration #{position_or_name.inspect}",caller)
  end

  @migrations[name]
end

#downto(position_or_name = nil) {|migration| ... } ⇒ Enumerator

Enumerates over the migrations down to a specific migration.

Parameters:

  • position_or_name (Integer, Symbol, nil) (defaults to: nil)

    The migration position or name to stop before.

Yields:

  • (migration)

    The given block will be passed each migration. Once the migration of the given position or name is reached, no further migrations will be yielded.

Yield Parameters:

  • migration (Migration)

    A migration from the graph.

Returns:

  • (Enumerator)

    If no block is given, an enumerator object will be returned.

Raises:

Since:

  • 1.0.1



252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/ronin/database/migrations/graph.rb', line 252

def downto(position_or_name=nil)
  return enum_for(:downto,position_or_name) unless block_given?

  name = name_of(position_or_name)

  # tsort named migrations by their dependencies
  tsort.reverse_each do |key|
    # break before the target migration is reached
    break if (name && name == key)

    yield @migrations[key]
  end
end

#empty?Boolean

Determines if the migration graph is empty.

Returns:

  • (Boolean)

    Specifies whether the migration graph contains no migrations.

Since:

  • 1.0.1



62
63
64
# File 'lib/ronin/database/migrations/graph.rb', line 62

def empty?
  @migrations.empty?
end

#migration_at(position, name, options = {}, &block) ⇒ Migration

Defines a migration assigned to a given position.

Parameters:

  • position (Integer)

    The position of the migration.

  • name (Symbol)

    The name of the migration.

  • options (Hash) (defaults to: {})

    Additional options for the migration.

Returns:

  • (Migration)

    The newly defined migration.

Raises:

  • (DuplicateMigration)

    Another migration was previously defined with the same name or position.

See Also:

Since:

  • 1.0.1



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/ronin/database/migrations/graph.rb', line 171

def migration_at(position,name,options={},&block)
  if @positions.has_key?(position)
    raise(DuplicateMigration,"there is already a migration at position #{position}",caller)
  end

  if position > 1
    # explicit define a dependency on the previous migration position
    options[:needs] = [position - 1]
  end

  name = name.to_sym

  migration = migration_named(name,options,&block)

  # define a mapping from position to migration name
  @positions[position] = name
  return migration
end

#migration_named(name, options = {}, &block) ⇒ Migration

Defines a migration.

Parameters:

  • name (Symbol)

    The name of the migration.

  • options (Hash) (defaults to: {})

    Additional options for the migration.

Options Hash (options):

  • :verbose (Boolean) — default: true

    Enables or disables verbose output.

  • :repository (Symbol) — default: :default

    The DataMapper repository the migration will operate on.

  • :needs (Array, Symbol)

    Other migrations that are dependencies of the migration.

Returns:

  • (Migration)

    The newly defined migration.

Raises:

Since:

  • 1.0.1



138
139
140
141
142
143
144
# File 'lib/ronin/database/migrations/graph.rb', line 138

def migration_named(name,options={},&block)
  if @migrations.has_key?(name)
    raise(DuplicateMigration,"there is already a migration with the name #{name}",caller)
  end

  @migrations[name] = Migration.new(name,options,&block)
end

#name_of(position_or_name) ⇒ Symbol (protected)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Maps a position or name to a migration name.

Parameters:

  • position_or_name (Symbol, Integer)

    The migration position or name.

Returns:

  • (Symbol)

    The migration name.

Raises:

Since:

  • 1.0.1



284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/ronin/database/migrations/graph.rb', line 284

def name_of(position_or_name)
  case position_or_name
  when Integer
    unless @positions.has_key?(position_or_name)
      raise(UnknownMigration,"unknown migration position #{position_or_name}",caller)
    end

    @positions[position_or_name]
  when Symbol
    position_or_name
  end
end

#tsort_each_child(node) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



98
99
100
101
102
103
104
105
106
# File 'lib/ronin/database/migrations/graph.rb', line 98

def tsort_each_child(node)
  unless @migrations.has_key?(node)
    raise(UnknownMigration,"no migration defined for #{node}",caller)
  end

  @migrations[node].needs.each do |dep|
    yield name_of(dep)
  end
end

#tsort_each_node(&block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



91
92
93
# File 'lib/ronin/database/migrations/graph.rb', line 91

def tsort_each_node(&block)
  @migrations.each_key(&block)
end

#upto(position_or_name = nil) {|migration| ... } ⇒ Enumerator

Enumerates over the migrations up to a specific migration.

Parameters:

  • position_or_name (Integer, Symbol, nil) (defaults to: nil)

    The migration position or name to stop after.

Yields:

  • (migration)

    The given block will be passed each migration. Once the migration of the given position or name is yielded, no further migrations will be yielded.

Yield Parameters:

  • migration (Migration)

    A migration from the graph.

Returns:

  • (Enumerator)

    If no block is given, an enumerator object will be returned.

Raises:

Since:

  • 1.0.1



214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/ronin/database/migrations/graph.rb', line 214

def upto(position_or_name=nil)
  return enum_for(:upto,position_or_name) unless block_given?

  name = name_of(position_or_name)

  # tsort named migrations by their dependencies
  tsort.each do |key|
    yield @migrations[key]

    # break after the target migration has been reached
    break if (name && name == key)
  end
end