Class: Migr8::RepositoryOperation
- Inherits:
-
Object
- Object
- Migr8::RepositoryOperation
- Defined in:
- lib/migr8.rb
Instance Method Summary collapse
- #apply(versions) ⇒ Object
- #delete(version) ⇒ Object
- #downgrade(n) ⇒ Object
- #history ⇒ Object
-
#initialize(repo) ⇒ RepositoryOperation
constructor
A new instance of RepositoryOperation.
- #inspect(n = 5) ⇒ Object
- #new(version, author, desc, opts = {}) ⇒ Object
- #show(version = nil, load_from_db = False) ⇒ Object
- #status ⇒ Object
- #unapply(versions) ⇒ Object
- #unapply_only_in_database(versions) ⇒ Object
- #upgrade(n) ⇒ Object
Constructor Details
#initialize(repo) ⇒ RepositoryOperation
Returns a new instance of RepositoryOperation.
352 353 354 |
# File 'lib/migr8.rb', line 352 def initialize(repo) @repo = repo end |
Instance Method Details
#apply(versions) ⇒ Object
512 513 514 515 |
# File 'lib/migr8.rb', line 512 def apply(versions) migs = _get_migrations_in_history_file(versions, false) @repo.apply_migrations(migs) end |
#delete(version) ⇒ Object
460 461 462 |
# File 'lib/migr8.rb', line 460 def delete(version) @repo.delete_migration(version) end |
#downgrade(n) ⇒ Object
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 |
# File 'lib/migr8.rb', line 488 def downgrade(n) migs_hist, migs_dict = _get_migrations_hist_and_applied() ## index of current version curr = migs_hist.rindex {|mig| mig.applied? } ## error when unapplied older version exists in target migrations migs_applied = curr ? migs_hist[0..curr] : [] if curr j = migs_applied.index {|mig| ! mig.applied? } raise MigrationError.new("apply #{migs_applied[j].version} at first.") if j && j < curr end ## unapply n migrations migs_to_unapply = n && n < migs_applied.length ? migs_applied[-n..-1] \ : migs_applied if migs_to_unapply.empty? puts "## (nothing to unapply)" else #migs_to_unapply.reverse_each do |mig| # puts "## unapplying #{mig.version} \# [#{mig.author}] #{mig.desc}" # @repo.unapply_migration(mig) #end @repo.unapply_migrations(migs_to_unapply.reverse()) end end |
#history ⇒ Object
356 357 358 359 360 361 362 363 364 365 366 367 368 369 |
# File 'lib/migr8.rb', line 356 def history mig_hist, mig_dict = _get_migrations_hist_and_applied() s = "" mig_hist.each do |mig| s << _to_line(mig) end if ! mig_dict.empty? s << "##\n" s << "## Applied to DB but not exist in history file:\n" s << "##\n" mig_dict.each {|ver, mig| s << _to_line(mig) } end return s end |
#inspect(n = 5) ⇒ Object
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 |
# File 'lib/migr8.rb', line 380 def inspect(n=5) mig_hist, mig_dict = _get_migrations_hist_and_applied() pos = mig_hist.length - n - 1 i = mig_hist.index {|mig| ! mig.applied? } # index of oldest unapplied j = mig_hist.rindex {|mig| mig.applied? } # index of newest applied start = i.nil? ? pos : [i - 1, pos].min start = 0 if start < 0 if mig_hist.empty? status = "no migrations" recent = nil elsif i.nil? status = "all applied" recent = mig_hist[start..-1] elsif j.nil? status = "nothing applied" recent = mig_hist[0..-1] elsif i < j status = "YOU MUST APPLY #{mig_hist[i].version} AT FIRST!" recent = mig_hist[start..-1] else count = mig_hist.length - i status = "there are #{count} migrations to apply" status = "there is a migration to apply" if count == 1 recent = mig_hist[start..-1] end missing = mig_dict.empty? ? nil : mig_dict.values return {:status=>status, :recent=>recent, :missing=>missing} end |
#new(version, author, desc, opts = {}) ⇒ Object
371 372 373 374 375 376 377 378 |
# File 'lib/migr8.rb', line 371 def new(version, , desc, opts={}) if version && @repo.migration_file_exist?(version) raise MigrationError.new("#{version}: failed to create migration file because file already exists. Please run 'File.basename($0) edit #{version}' to see existing file.") end mig = @repo.create_migration(version, , desc, opts) return mig end |
#show(version = nil, load_from_db = False) ⇒ Object
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 |
# File 'lib/migr8.rb', line 425 def show(version=nil, load_from_db=False) migs = load_from_db ? @repo.migrations_in_history_table() \ : @repo.migrations_in_history_file() if version mig = migs.find {|mig| mig.version == version } or raise MigrationError.new("#{version}: no such migration.") else mig = migs.last or raise MigrationError.new("no migrations to show.") end if load_from_db @repo.fetch_details_from_history_table(mig) #assert mig.instance_variable_get('@up_script') != nil #assert mig.instance_variable_get('@down_script') != nil end # buf = "" buf << "version: #{mig.version}\n" buf << "desc: #{mig.desc}\n" buf << "author: #{mig.}\n" buf << "vars:\n" unless load_from_db mig.vars.each do |k, v| buf << " - %-10s " % ["#{k}:"] << v.inspect << "\n" end unless load_from_db buf << "applied_at: #{mig.applied_at}\n" if load_from_db buf << "\n" buf << "up: |\n" buf << mig.up_script.gsub(/^/, ' ') buf << "\n" buf << "down: |\n" buf << mig.down_script.gsub(/^/, ' ') buf << "\n" return buf end |
#status ⇒ Object
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 |
# File 'lib/migr8.rb', line 409 def status ret = inspect() s = "" s << "## Status: #{ret[:status]}\n" if ret[:recent] s << "## Recent history:\n" ret[:recent].each {|mig| s << _to_line(mig) } end if ret[:missing] s << "## !!! The following migrations are applied to DB, but files are not found.\n" s << "## !!! (Try `#{File.basename($0)} unapply -x abcd1234` to unapply them.)\n" ret[:missing].each {|mig| s << _to_line(mig) } end return s end |
#unapply(versions) ⇒ Object
517 518 519 520 |
# File 'lib/migr8.rb', line 517 def unapply(versions) migs = _get_migrations_in_history_file(versions, true) @repo.unapply_migrations(migs) end |
#unapply_only_in_database(versions) ⇒ Object
522 523 524 525 |
# File 'lib/migr8.rb', line 522 def unapply_only_in_database(versions) migs = _get_migrations_only_in_database(versions) @repo.unapply_migrations(migs, true) end |
#upgrade(n) ⇒ Object
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 |
# File 'lib/migr8.rb', line 464 def upgrade(n) migs_hist, migs_dict = _get_migrations_hist_and_applied() ## index of current version curr = migs_hist.rindex {|mig| mig.applied? } ## error when unapplied older version exists if curr j = migs_hist.index {|mig| ! mig.applied? } raise MigrationError.new("apply #{migs_hist[j].version} at first.") if j && j < curr end ## unapplied migrations migs_unapplied = curr ? migs_hist[(curr+1)..-1] : migs_hist ## apply n migrations migs_to_apply = n.nil? ? migs_unapplied : migs_unapplied[0...n] if migs_to_apply.empty? puts "## (nothing to apply)" else #migs_to_apply.each do |mig| # puts "## applying #{mig.version} \# [#{mig.author}] #{mig.desc}" # @repo.apply_migration(mig) #end @repo.apply_migrations(migs_to_apply) end end |