Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/evolver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ def find(migration, session)
# @return [ true ] True if all migrations succeeded.
#
# @since 0.0.0
def migrate
def migrate(options = {})
load_migrations
Migrator.new(sessions).execute and true
Migrator.new(sessions, options).execute and true
end

# Load all the migrations in the application.
Expand Down
17 changes: 13 additions & 4 deletions lib/evolver/migrator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ class Migrator

TEMPLATE = File.join(File.dirname(__FILE__), "stats.txt.erb")

attr_reader :sessions
DEFAULT_OPTIONS = {
dry: false
}

attr_reader :sessions, :dry, :options

# Execute the migrations. This grabs all pending migrations in order and
# calls execute on them for each session that it has specified it runs on.
Expand All @@ -22,11 +26,13 @@ class Migrator
#
# @since 0.0.0
def execute
log(:info, "DRY RUN: Migrations will only be logged and *NOT* executed.") if dry

sessions.each_pair do |name, session|
pending(name, session.with(safe: true, consistency: :strong)).each do |migration|
migration.execute
migration.execute unless dry
log_execution(migration, name)
migration.mark_as_executed
migration.mark_as_executed unless dry
end
end
end
Expand All @@ -52,10 +58,13 @@ def executed(session)
# Migrator.new([ session ])
#
# @param [ Array<Moped::Session> ] sessions The sessions to migrate on.
# @param [ Hash ] options Migration options.
#
# @since 0.0.0
def initialize(sessions)
def initialize(sessions, options = {})
@options = DEFAULT_OPTIONS.merge(options)
@sessions = sessions
@dry = @options[:dry] || false
end

# Get the list of pending migrations - those that need to be executed
Expand Down
7 changes: 7 additions & 0 deletions lib/evolver/tasks/db.rake
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ namespace :evolver do
Evolver.migrate
end

namespace :migrate do
desc "Dry run of any pending MongoDB data migrations on all sessions."
task dry: :environment do
Evolver.migrate(dry: true)
end
end

desc "Revert the last run MongoDB data migration."
task revert: :environment do
Evolver.revert
Expand Down
26 changes: 21 additions & 5 deletions spec/evolver/migrator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,35 @@
Mongoid.default_session
end

before(:all) do
before do
session[:evolver_migrations].find.remove_all
end

describe "#execute" do

let(:migrator) do
described_class.new({ default: session })
context "dry run" do

let(:migrator) do
described_class.new({ default: session }, { dry: true })
end

before { migrator.execute }

let(:migrations) do
session[:evolver_migrations].find.sort(generated: 1).to_a
end

it "does not execute the migrations" do
migrations.count.should eq(0)
end
end

context "when no migrations have been run" do

let(:migrator) do
described_class.new({ default: session })
end

before do
session[:labels].insert({
bands: [ "Depeche Mode", "Erasure" ],
Expand All @@ -28,11 +45,10 @@
end

after do
session[:evolver_migrations].find.remove_all
session[:labels].find.remove_all
end

let!(:migrations) do
let(:migrations) do
session[:evolver_migrations].find.sort(generated: 1).to_a
end

Expand Down