Class: Mongoid::Migrator
- Inherits:
-
Object
- Object
- Mongoid::Migrator
- Defined in:
- lib/mongoid_rails_migrations/active_record_ext/migrations.rb
Overview
:nodoc:
Class Attribute Summary collapse
Class Method Summary collapse
- .current_version ⇒ Object
- .down(migrations_path, target_version = nil) ⇒ Object
- .forward(migrations_path, steps = 1) ⇒ Object
- .get_all_versions ⇒ Object
- .migrate(migrations_path, target_version = nil) ⇒ Object
- .rollback(migrations_path, steps = 1) ⇒ Object
- .rollback_to(migrations_path, target_version) ⇒ Object
- .run(direction, migrations_path, target_version) ⇒ Object
- .status(migrations_path) ⇒ Object
- .up(migrations_path, target_version = nil) ⇒ Object
- .with_mongoid_client(mongoid_client_name, &block) ⇒ Object
Instance Method Summary collapse
- #current_migration ⇒ Object
- #current_version ⇒ Object
-
#initialize(direction, migrations_path, target_version = nil) ⇒ Migrator
constructor
A new instance of Migrator.
- #migrate ⇒ Object
- #migrated ⇒ Object
- #migrations ⇒ Object
- #pending_migrations ⇒ Object
- #run ⇒ Object
- #runnable_migrations ⇒ Object
- #status ⇒ Object
Constructor Details
#initialize(direction, migrations_path, target_version = nil) ⇒ Migrator
Returns a new instance of Migrator.
266 267 268 269 270 271 272 273 |
# File 'lib/mongoid_rails_migrations/active_record_ext/migrations.rb', line 266 def initialize(direction, migrations_path, target_version = nil) @direction, @migrations_path, @target_version = direction, migrations_path, target_version @mongoid_client_name = ENV["MONGOID_CLIENT_NAME"] if @mongoid_client_name && !Mongoid.clients.has_key?(@mongoid_client_name) raise Mongoid::Errors::NoClientConfig.new(@mongoid_client_name) end end |
Class Attribute Details
.migrations_path ⇒ Object
230 231 232 |
# File 'lib/mongoid_rails_migrations/active_record_ext/migrations.rb', line 230 def migrations_path @migrations_path ||= ['db/migrate'] end |
Class Method Details
.current_version ⇒ Object
240 241 242 |
# File 'lib/mongoid_rails_migrations/active_record_ext/migrations.rb', line 240 def current_version get_all_versions.max || 0 end |
.down(migrations_path, target_version = nil) ⇒ Object
222 223 224 |
# File 'lib/mongoid_rails_migrations/active_record_ext/migrations.rb', line 222 def down(migrations_path, target_version = nil) self.new(:down, migrations_path, target_version).migrate end |
.forward(migrations_path, steps = 1) ⇒ Object
214 215 216 |
# File 'lib/mongoid_rails_migrations/active_record_ext/migrations.rb', line 214 def forward(migrations_path, steps=1) move(:up, migrations_path, steps) end |
.get_all_versions ⇒ Object
234 235 236 237 238 |
# File 'lib/mongoid_rails_migrations/active_record_ext/migrations.rb', line 234 def get_all_versions with_mongoid_client(ENV['MONGOID_CLIENT_NAME']) do DataMigration.all.map { |datamigration| datamigration.version.to_i }.sort end end |
.migrate(migrations_path, target_version = nil) ⇒ Object
188 189 190 191 192 193 194 |
# File 'lib/mongoid_rails_migrations/active_record_ext/migrations.rb', line 188 def migrate(migrations_path, target_version = nil) case when target_version.nil? then up(migrations_path, target_version) when current_version > target_version then down(migrations_path, target_version) else up(migrations_path, target_version) end end |
.rollback(migrations_path, steps = 1) ⇒ Object
200 201 202 |
# File 'lib/mongoid_rails_migrations/active_record_ext/migrations.rb', line 200 def rollback(migrations_path, steps=1) move(:down, migrations_path, steps) end |
.rollback_to(migrations_path, target_version) ⇒ Object
204 205 206 207 208 209 210 211 212 |
# File 'lib/mongoid_rails_migrations/active_record_ext/migrations.rb', line 204 def rollback_to(migrations_path, target_version) all_versions = get_all_versions target_version_index = all_versions.index(target_version.to_i) raise UnknownMigrationVersionError.new(target_version) if target_version_index.nil? rollback_to = target_version_index + 1 rollback_steps = all_versions.size - rollback_to rollback migrations_path, rollback_steps end |
.run(direction, migrations_path, target_version) ⇒ Object
226 227 228 |
# File 'lib/mongoid_rails_migrations/active_record_ext/migrations.rb', line 226 def run(direction, migrations_path, target_version) self.new(direction, migrations_path, target_version).run end |
.status(migrations_path) ⇒ Object
196 197 198 |
# File 'lib/mongoid_rails_migrations/active_record_ext/migrations.rb', line 196 def status(migrations_path) new(:up, migrations_path).status end |
.up(migrations_path, target_version = nil) ⇒ Object
218 219 220 |
# File 'lib/mongoid_rails_migrations/active_record_ext/migrations.rb', line 218 def up(migrations_path, target_version = nil) self.new(:up, migrations_path, target_version).migrate end |
.with_mongoid_client(mongoid_client_name, &block) ⇒ Object
244 245 246 247 248 249 250 |
# File 'lib/mongoid_rails_migrations/active_record_ext/migrations.rb', line 244 def with_mongoid_client(mongoid_client_name, &block) previous_mongoid_client_name = Mongoid::Threaded.client_override Mongoid.override_client(mongoid_client_name) block.call ensure Mongoid.override_client(previous_mongoid_client_name) end |
Instance Method Details
#current_migration ⇒ Object
279 280 281 |
# File 'lib/mongoid_rails_migrations/active_record_ext/migrations.rb', line 279 def current_migration migrations.detect { |m| m.version == current_version } end |
#current_version ⇒ Object
275 276 277 |
# File 'lib/mongoid_rails_migrations/active_record_ext/migrations.rb', line 275 def current_version migrated.last || 0 end |
#migrate ⇒ Object
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 |
# File 'lib/mongoid_rails_migrations/active_record_ext/migrations.rb', line 295 def migrate runnable = runnable_migrations runnable.each do |migration| Rails.logger.info "Migrating to #{migration.name} (#{migration.version})" if Rails.logger # On our way up, we skip migrating the ones we've already migrated next if up? && migrated.include?(migration.version.to_i) # On our way down, we skip reverting the ones we've never migrated if down? && !migrated.include?(migration.version.to_i) migration.announce 'never migrated, skipping'; migration.write next end begin migration.migrate(@direction) with_mongoid_client(@mongoid_client_name) do (migration.version) end rescue => e output = Migration.buffer_output + "An error has occurred, #{migration.version} and all later migrations canceled:\n\n#{e}\n#{e.backtrace.join("\n")}" begin Migration.after_migrate.call(output, migration.name, @direction, true) if Migration.after_migrate Migration.buffer_output = nil rescue => error puts("Error in after_migrate hook: #{error}") end raise StandardError, "An error has occurred, #{migration.version} and all later migrations canceled:\n\n#{e}", e.backtrace end end end |
#migrated ⇒ Object
400 401 402 |
# File 'lib/mongoid_rails_migrations/active_record_ext/migrations.rb', line 400 def migrated @migrated_versions ||= self.class.get_all_versions end |
#migrations ⇒ Object
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 |
# File 'lib/mongoid_rails_migrations/active_record_ext/migrations.rb', line 340 def migrations @migrations ||= begin files = Array(@migrations_path).inject([]) do |files, path| files += Dir["#{path}/**/[0-9]*_*.rb"] end migrations = files.inject([]) do |klasses, file| version, name = file.scan(/([0-9]+)_([_a-z0-9]*).rb/).first raise IllegalMigrationNameError.new(file) unless version version = version.to_i if klasses.detect { |m| m.version == version } raise DuplicateMigrationVersionError.new(version) end if klasses.detect { |m| m.name == name.camelize } raise DuplicateMigrationNameError.new(name.camelize) end migration = MigrationProxy.new migration.sharded = file.match?(/\/shards\/#{version}_#{name}\.rb/) migration.name = name.camelize migration.version = version migration.filename = file if (@mongoid_client_name && migration.sharded) || (!@mongoid_client_name && !migration.sharded) klasses << migration end klasses end migrations = migrations.sort_by(&:version) down? ? migrations.reverse : migrations end end |
#pending_migrations ⇒ Object
377 378 379 380 |
# File 'lib/mongoid_rails_migrations/active_record_ext/migrations.rb', line 377 def pending_migrations already_migrated = migrated migrations.reject { |m| already_migrated.include?(m.version.to_i) } end |
#run ⇒ Object
283 284 285 286 287 288 289 290 291 292 293 |
# File 'lib/mongoid_rails_migrations/active_record_ext/migrations.rb', line 283 def run target = migrations.detect { |m| m.version == @target_version } raise UnknownMigrationVersionError.new(@target_version) if target.nil? unless (up? && migrated.include?(target.version.to_i)) || (down? && !migrated.include?(target.version.to_i)) target.migrate(@direction) with_mongoid_client(@mongoid_client_name) do (target.version) end end end |
#runnable_migrations ⇒ Object
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 |
# File 'lib/mongoid_rails_migrations/active_record_ext/migrations.rb', line 382 def runnable_migrations current = migrations.detect { |m| m.version == current_version } target = migrations.detect { |m| m.version == @target_version } if target.nil? && !@target_version.nil? && @target_version > 0 raise UnknownMigrationVersionError.new(@target_version) end start = up? ? 0 : (migrations.index(current) || 0) finish = migrations.index(target) || migrations.size - 1 runnable = migrations[start..finish] # skip the last migration if we're headed down, but not ALL the way down runnable.pop if down? && !target.nil? runnable end |
#status ⇒ Object
328 329 330 331 332 333 334 335 336 337 338 |
# File 'lib/mongoid_rails_migrations/active_record_ext/migrations.rb', line 328 def status database_name = Migration.connection.[:database] puts "\ndatabase: #{database_name}\n\n" puts "#{'Status'.center(8)} #{'Migration ID'.ljust(14)} Migration Name" puts "-" * 50 up_migrations = migrated.to_set migrations.each do |migration| status = up_migrations.include?(migration.version.to_i) ? 'up' : 'down' puts "#{status.center(8)} #{migration.version.to_s.ljust(14)} #{migration.name}" end end |