Skip to content

Commit

Permalink
Minor spec improvements + restore db:drop task breaking compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
jarthod committed Sep 12, 2024
1 parent af9ce91 commit ae7103c
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 28 deletions.
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,26 +93,25 @@ Global migrations can still be created with the `--no-shards` option.

* `1.5.x` targets Mongoid >= `5.0` and Rails >= `4.2`
* `1.4.x` targets Mongoid >= `4.0` and Rails >= `4.2`
* `1.3.x` targets Mongoid >= `4.0` and Rails >= `4.2`
* `1.2.x` targets Mongoid >= `4.0` and Rails >= `4.2`
* `1.1.x` targets Mongoid >= `4.0` and Rails >= `4.2`
* `1.0.0` targers Mongoid >= `3.0` and Rails >= `3.2`
* `0.0.14` targets Mongoid >= `2.0` and Rails >= `3.0` (but < `3.2`)

# Changelog

## Unreleased

* Remove unnecessary purge, setup, reset, etc. rake tasks because they are already defined by Mongoid (#60)
* Minor tests improvements

## 1.5.0
_26/03/2021_
[Compare master with 1.4.0](https://github.com/adacosta/mongoid_rails_migrations/compare/v1.4.0...master)
[Compare master with 1.5.0](https://github.com/adacosta/mongoid_rails_migrations/compare/v1.5.0...master)
* Add support of multi databases
* Shards migrations can now be stored in a `shards` subfolder inside the migration folder
* All Rake tasks now support a custom client with the `MONGOID_CLIENT_NAME` environment variable

## 1.4.0
_08/01/2021_
[Compare master with 1.3.0](https://github.com/adacosta/mongoid_rails_migrations/compare/v1.3.0...master)
* The hook `after_migrate` can be use when migration crash (#56)

## 1.3.0
Expand Down
16 changes: 1 addition & 15 deletions lib/mongoid_rails_migrations/active_record_ext/migrations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ def migrate(direction)
# it is safe for the call to proceed.
def singleton_method_added(sym) #:nodoc:
return if defined?(@ignore_new_methods) && @ignore_new_methods

begin
@ignore_new_methods = true

Expand Down Expand Up @@ -154,24 +153,11 @@ def suppress_messages
def connection
# ActiveRecord::Base.connection
if ENV['MONGOID_CLIENT_NAME']
Migrator.with_mongoid_client(ENV['MONGOID_CLIENT_NAME']) do
Mongoid.client(Mongoid::Threaded.client_override)
end
Mongoid.client(ENV['MONGOID_CLIENT_NAME'])
else
Mongoid.default_client
end
end

def method_missing(method, *arguments, &block)
arg_list = arguments.map(&:inspect) * ', '

say_with_time "#{method}(#{arg_list})" do
# unless arguments.empty? || method == :execute
# arguments[0] = Migrator.proper_table_name(arguments.first)
# end
connection.send(method, *arguments, &block)
end
end
end
end

Expand Down
10 changes: 10 additions & 0 deletions lib/mongoid_rails_migrations/mongoid_ext/railties/database.rake
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
namespace :db do
if Rake::Task.task_defined?("db:drop")
Rake::Task["db:drop"].clear
end

desc 'Drops the database for the current Mongoid client'
task :drop => :environment do
# Unlike Mongoid's default, this implementation supports the MONGOID_CLIENT_NAME override
Mongoid::Migration.connection.database.drop
end

desc 'Current database version'
task :version => :environment do
puts Mongoid::Migrator.current_version.to_s
Expand Down
5 changes: 4 additions & 1 deletion test/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@
MIGRATIONS_ROOT = 'test/migrations'

Mongoid.configure.load!("#{__dir__}/mongoid.yml", 'test')
require 'models/survey_schema'
require_relative 'models/survey_schema'

module TestMongoidRailsMigrations
class Application < Rails::Application; end
end

TestMongoidRailsMigrations::Application.load_tasks

# Mongo debug log
# Mongo::Logger.logger = Logger.new(STDOUT)

# Hide task output
class Mongoid::Migration
def self.puts _
Expand Down
5 changes: 1 addition & 4 deletions test/migration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@ module Mongoid
class TestCase < Minitest::Test #:nodoc:

def setup
Mongoid::Migration.verbose = true
Mongo::Logger.logger.level = 1
# same as db:drop command in lib/mongoid_rails_migrations/mongoid_ext/railties/database.rake
invoke("db:drop")
Mongoid::Migrator.with_mongoid_client("shard1") do
with_env("MONGOID_CLIENT_NAME" => "shard1") do
invoke("db:drop")
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/tasks/task_test_base.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require 'helper'
require_relative '../helper'

load 'lib/mongoid_rails_migrations/mongoid_ext/railties/database.rake'

Expand Down
6 changes: 4 additions & 2 deletions test/tasks/version_task_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ def test_multidatabase_version
assert_output("0\n") { invoke("db:version") }
end

def test_multidatabase_version_with_target_client
def test_multidatabase_version_with_target_client_and_rollback
Mongoid::Migrator.migrations_path = [MIGRATIONS_ROOT + "/multi_shards"]
with_env("MONGOID_CLIENT_NAME" => "shard1") do
assert_output("0\n") { invoke("db:version") }
invoke("db:migrate")
assert_output("20210210125532\n") { invoke("db:version") }
invoke("db:drop")
invoke("db:rollback")
assert_output("20210210124656\n") { invoke("db:version") }
invoke("db:rollback")
assert_output("0\n") { invoke("db:version") }
end
end
Expand Down

0 comments on commit ae7103c

Please sign in to comment.