Class: Ronin::Database::Migrations::Graph
- Inherits:
-
Object
- Object
- Ronin::Database::Migrations::Graph
- Includes:
- Enumerable, TSort
- Defined in:
- lib/ronin/database/migrations/graph.rb
Overview
Sorts and runs Migrations based on their dependencies.
Instance Attribute Summary collapse
-
#migrations ⇒ Object
readonly
The migrations in the graph.
-
#positions ⇒ Object
readonly
The mapping of position numbers to migration names.
Instance Method Summary collapse
-
#[](position_or_name) ⇒ Migration
Provides direct access to migrations.
-
#downto(position_or_name = nil) {|migration| ... } ⇒ Enumerator
Enumerates over the migrations down to a specific migration.
-
#empty? ⇒ Boolean
Determines if the migration graph is empty.
-
#initialize ⇒ Graph
constructor
Creates a new Migration Graph.
-
#migration_at(position, name, options = {}, &block) ⇒ Migration
Defines a migration assigned to a given position.
-
#migration_named(name, options = {}, &block) ⇒ Migration
Defines a migration.
-
#name_of(position_or_name) ⇒ Symbol
protected
private
Maps a position or name to a migration name.
- #tsort_each_child(node) ⇒ Object private
- #tsort_each_node(&block) ⇒ Object private
-
#upto(position_or_name = nil) {|migration| ... } ⇒ Enumerator
Enumerates over the migrations up to a specific migration.
Constructor Details
#initialize ⇒ Graph
Creates a new Migration Graph.
49 50 51 52 |
# File 'lib/ronin/database/migrations/graph.rb', line 49 def initialize @migrations = {} @positions = {} end |
Instance Attribute Details
#migrations ⇒ Object (readonly)
The migrations in the graph
39 40 41 |
# File 'lib/ronin/database/migrations/graph.rb', line 39 def migrations @migrations end |
#positions ⇒ Object (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.
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.
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.
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.
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,={},&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 [:needs] = [position - 1] end name = name.to_sym migration = migration_named(name,,&block) # define a mapping from position to migration name @positions[position] = name return migration end |
#migration_named(name, options = {}, &block) ⇒ Migration
Defines a migration.
138 139 140 141 142 143 144 |
# File 'lib/ronin/database/migrations/graph.rb', line 138 def migration_named(name,={},&block) if @migrations.has_key?(name) raise(DuplicateMigration,"there is already a migration with the name #{name}",caller) end @migrations[name] = Migration.new(name,,&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.
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.
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 |