diff --git a/.gitignore b/.gitignore index f501bdf..8fe1fa1 100644 --- a/.gitignore +++ b/.gitignore @@ -29,3 +29,4 @@ spec/support/database.yml log/deprecations.log +.byebug_history \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index 83356da..a5b4485 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,13 @@ # Mongify ChangeLog +## 1.4.0 / 25 June 2017 +* Added support for ActiveRecord/ActiveSupport 5.0 +* Added support for newer versions of MongoDB (3.2+) via the new ruby Mongo driver (version 2) +* Updated gem requirements to support ActiveRecord/ActiveSupport 5.0 +* Updated gem requirements to use Mongo 2.0 +* Added support for sharded clusters and replica sets for mongodb by setting the config option "host" to a comma-separated list +* Added ssl option to support ssl connections + ## 1.3.1 / 09 Nov 2016 * Updated gem requirements to exlucde ActiveRecord/ActiveSupport 5.0 * Locked down gem versions to prevent issues on newer gems (Like ActiveRecord 5 and Mongo) @@ -17,34 +25,43 @@ * Ensured updated_at doesn't get parsed multiple times in `sync` command (thanks @hammady) * Improved error message during `sync` command (thanks @hammady) * Minor bug fixes + ## 1.2.4 / 29 July 2014 * Fixed bug with ActiveSupport::Autoload (thanks [altieres](https://github.com/anlek/mongify/pull/46)) * Moved the order of reference updating to be second (after copying tables), this way embed tables can use new references * Small tweak on table.name (making sure rename_to value gets returns first) - It wasn't commited in version 1.2.3 + ## 1.2.3 / 17 July 2014 * Fixed MS Sql batching issue * Fixed row.delete requiring you to send in a string (vs symbol) * ~~Small tweak on table.name (making sure rename_to value gets returns first)~~ + ## 1.2.2 / 9 July 2014 * Locked in BSON and rspec gem to versions that will work with Mongify * Updated gems + ## 1.2.1 / 16 June 2014 * Fixed polymorphic associations not getting set properly + ## 1.2 / 16 June 2014 * Added batching to embedded and polymorphic tables. * Tweaked progress bar display. * Fixed issue with embedding * Updated mongo drivers to version 1.10.2 + ## 1.1 / 24 March 2014 * Sync feature add (thanks to {hammady}[https://github.com/hammady]) * Parent unset in before_save (tanks to {nessche}[https://github.com/nessche]) * Added batch_size (recommended by {mosheka}[https://github.com/mosheka]) * bugs and performance improvments + ## 1.0.1 / 24 December 2013 * Updated and locked down gems to prevent Mongify from breaking when rails gets updated. + ## 1.0.0 / 28 May 2013 * Fixed issue with MySql when specifying port (thanks to sebastianmarr) * Added information Mongify-Mongoid + ## 0.9 / 08 Feb 2013 * Changed Mongify Syntax (thanks to tanema) * Improved error handling @@ -53,10 +70,12 @@ * Updated Gem Requirements * Removed depricated calls (from Mongo insert statement) * Removed MySql2 dependency (ActiveRecord takes care of that) + ## 0.3.1 / 21 Jan 2013 * README examples updated * README typos fixed. * Added ability to fetch IDs of fields that have an array of ids + ## 0.3 / 12 Sep 2012 * Added ability to work with :key columns that were not :integer values * You can now specify :as value for a type :key column @@ -64,33 +83,41 @@ * Updated README * Improved tests * Small refactoring + ## 0.2.2 / 11 Sep 2012 * Fix broken specs (Pranas Kiziela) * Fix translation process when embedding multiple objects (Pranas Kiziela) * Fix typos (Pranas Kiziela) * Rake file fixed to work in Ruby 1.8 and Ruby 1.9 + ## 0.2.1 / 11 Sep 2012 * This version was skipped (internal issues) + ## 0.2.0 / 01 Jun 2011 * Added the ability to modify parent table when working with embedded tables + ## 0.1.7 / 03 May 2011 * Changed logic to NOT translate columns that are not in the translation file. If column is not listed, it's not moved over. * Fixed issue with case sensitivity of column names * Made sure that default value returned is a string (when translating) * Ensured to_s/to_print would output correct column name (even if it's renamed_to) + ## 0.1.6 / 13 Apr 2011 * Removed no longer needed dependencies * Small tweak to the cucumber tests to ensure correct feedback * Small changes to the README + ## 0.1.5 / 08 Apr 2011 * Removed Gem::Specification#default_executable= from gemspec because it's deprecated with no replacement * Updated code to work with Ruby 1.9 (thanks to eimermusic for pointing out a ruby 1.9 issue to me) * Added Rake as a development dependency * Added new rake tasks: test, test:rspec, test:cucumber * Updated to newer gems for ActiveSupport and Mongo Drivers. + ## 0.1.4 / 04 Mar 2011 * Changed gemspec to point to new homepage at 'http://mongify.com' * Small change to attempt to fix issue in Ruby 1.9 (Mongify still doesn't work in 1.9, but making a step towards it) + ## 0.1.3 / 21 Feb 2011 * Made all exception stem from MongifyError and system always raises a child of MongifyError. * Brought in the progress bar into source @@ -100,17 +127,20 @@ * Added an index on pre_mongified_id to speedup lookup times (Making it 42 times faster on import of embedded tables) * Fixed bug in importing of polymorphic tables where associations are nil * Fixed bug where pre_mongified_id would be a string and not an integer (causing no updates of ref_ids) + ## 0.1.2 / 14 Feb 2011 * More Improvements to README and RDOC * Added ability to :force => true on a mongodb_connection, forcing it to drop database before processing. * Improved UI output * Added activesupport notifications to keep track of what is happening with the system during import. + ## 0.1.1 / 28 Jan 2011 * Renamed GenerateDatabase to DatabaseGenerator * Renamed Table#embed? to Table#embedded? * Renamed Table#embed_as_object? to Table#embedded_as_object? * Added support for polymorphic tables * Did some file cleanup for unused files + ## 0.1.0 / 21 Jan 2011 * Moved from Alpha to Beta! :) * Improved internal RDocs @@ -119,17 +149,20 @@ * Renamed 'translate' command to 'translation' * Added :auto_detect option to columns * Added ability to do a before_save on the table. + ## 0.0.9 / 19 Jan 2011 * Added ability to rename tables * Fixed bug 'pre_mogified_id' not being removed in all records * Added ability to ignore tables * Added ability to ignore columns * Added ability to rename columns + ## 0.0.8 / 18 Jan 2011 * Added ability to embed as object on an embedded table * Some bugs fixed * Updated README with new table options * Improved data conversion on processing + ## 0.0.7 / 17 Jan 2011 * Added mysql2 as a dependency * Improved error message when configuration file not provided @@ -137,11 +170,14 @@ * Removed :default flag for column (there was no need for this) * Removed 'pre_mongified_id' from all collections (including embedded) * Removed the parent_id from an embedded row + ## 0.0.6 / 16 Jan 2011 * Added the ability to embed tables into collections (currently only one to many relationship) * Few tweaks and improvements in code + ## 0.0.5 / 15 Jan 2011 * Improved description for mongify. + ## 0.0.4 / 15 Jan 2011 * Switched the way connection files are written out. * Fixed issue with Configuration File reader @@ -153,14 +189,17 @@ * Added the first version of processing command (very basic) * Indexed columns in table class to speedup lookup time * Added ability to update references once database is populated + ## 0.0.3 / 28 Dec 2010 * Switched gemspec generation to raw gemspec and added Bundler for easier development * Added LICENSE * Cleaned up the require statements through out the app + ## 0.0.2 / 09 Jun 2010 * Rewrote the whole CLI interface * Removed datamapper and added active_record * Improved tests to cover most of the code * Changed overall commands for Mongify + ## 0.0.1 / 22 Mar 2010 * Initial Setup \ No newline at end of file diff --git a/Gemfile.lock b/Gemfile.lock index 6d7bb75..640169d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,38 +1,36 @@ PATH remote: . specs: - mongify (1.3.1) - activerecord (>= 4.2, < 5.0) - activesupport (>= 4.2, < 5.0) - bson (= 1.12.5) - bson_ext (= 1.12.5) + mongify (1.4.0) + activerecord (>= 4.2, < 6.0) + activesupport (>= 4.2, < 6.0) + bson (~> 4.0) + bson_ext (~> 1.5) highline (= 1.7.8) - mongo (= 1.12.5) + mongo (> 2.0) GEM remote: http://rubygems.org/ specs: - activemodel (4.2.7.1) - activesupport (= 4.2.7.1) - builder (~> 3.1) - activerecord (4.2.7.1) - activemodel (= 4.2.7.1) - activesupport (= 4.2.7.1) - arel (~> 6.0) - activesupport (4.2.7.1) + activemodel (5.1.1) + activesupport (= 5.1.1) + activerecord (5.1.1) + activemodel (= 5.1.1) + activesupport (= 5.1.1) + arel (~> 8.0) + activesupport (5.1.1) + concurrent-ruby (~> 1.0, >= 1.0.2) i18n (~> 0.7) - json (~> 1.7, >= 1.7.7) minitest (~> 5.1) - thread_safe (~> 0.3, >= 0.3.4) tzinfo (~> 1.1) - arel (6.0.3) - awesome_print (1.7.0) - bson (1.12.5) - bson_ext (1.12.5) - bson (~> 1.12.5) - builder (3.2.2) + arel (8.0.0) + awesome_print (1.8.0) + bson (4.2.1) + bson_ext (1.5.1) + builder (3.2.3) byebug (9.0.6) coderay (1.1.1) + concurrent-ruby (1.0.5) coolline (0.5.0) unicode_utils (~> 1.4) cucumber (2.4.0) @@ -46,11 +44,11 @@ GEM cucumber-core (1.5.0) gherkin (~> 4.0) cucumber-wire (0.0.1) - diff-lcs (1.2.5) - gherkin (4.0.0) + diff-lcs (1.3) + gherkin (4.1.3) highline (1.7.8) hirb (0.7.3) - i18n (0.7.0) + i18n (0.8.4) jazz_fingers (4.0.1) awesome_print (~> 1.6) hirb (~> 0.7) @@ -58,49 +56,48 @@ GEM pry-byebug (~> 3.4) pry-coolline (~> 0.2) pry-doc (~> 0.6) - json (1.8.3) metaclass (0.0.4) method_source (0.8.2) - minitest (5.9.1) + minitest (5.10.2) mocha (1.2.1) metaclass (~> 0.0.1) - mongo (1.12.5) - bson (= 1.12.5) + mongo (2.4.2) + bson (>= 4.2.1, < 5.0.0) multi_json (1.12.1) multi_test (0.1.2) - mysql2 (0.4.5) - pg (0.19.0) + mysql2 (0.4.6) + pg (0.21.0) pry (0.10.4) coderay (~> 1.1.0) method_source (~> 0.8.1) slop (~> 3.4) - pry-byebug (3.4.0) + pry-byebug (3.4.2) byebug (~> 9.0) pry (~> 0.10) pry-coolline (0.2.5) coolline (~> 0.5) - pry-doc (0.9.0) + pry-doc (0.10.0) pry (~> 0.9) - yard (~> 0.8) - rake (11.3.0) + yard (~> 0.9) + rake (12.0.0) rspec (2.99.0) rspec-core (~> 2.99.0) rspec-expectations (~> 2.99.0) rspec-mocks (~> 2.99.0) - rspec-collection_matchers (1.1.2) + rspec-collection_matchers (1.1.3) rspec-expectations (>= 2.99.0.beta1) rspec-core (2.99.2) rspec-expectations (2.99.2) diff-lcs (>= 1.1.3, < 2.0) rspec-mocks (2.99.4) slop (3.6.0) - sqlite3 (1.3.12) - thread_safe (0.3.5) - tzinfo (1.2.2) + sqlite3 (1.3.13) + thread_safe (0.3.6) + tzinfo (1.2.3) thread_safe (~> 0.1) unicode_utils (1.4.0) watchr (0.7) - yard (0.9.5) + yard (0.9.9) PLATFORMS ruby @@ -120,4 +117,4 @@ DEPENDENCIES yard (>= 0.8) BUNDLED WITH - 1.13.5 + 1.14.6 diff --git a/README.rdoc b/README.rdoc index 88ae0e4..818173e 100644 --- a/README.rdoc +++ b/README.rdoc @@ -19,7 +19,10 @@ Learn more about MongoDB at: http://www.mongodb.org === NOTICE - You might have to install active record database gems (you'll know if you get an error message while running mongify command) + 1. You might have to install active record database gems (you'll know if you get an error message while running mongify command) + + 2. Mongify 1.4.0 and up uses the new Mongo 2.0 ruby driver, which adds support for MongoDB 3.4+ + If you run into issues with older versions of MongoDB, downgrade to Mongify 1.3.1 == Usage @@ -56,6 +59,34 @@ Currently the only supported option is for the mongodb_connection. You can omit the mongodb connection until you're ready to process your translation +==== Additional connection configuration: + + mongodb_connection do + # connect to a single host: + host "localhost" + + # or: connect to multiple hosts (comma-separated) + host "host1.mongodb.net,host2.mongodb.net,host3.mongodb.net" + + # define a port (defaults to 27017) + # if you pass in multiple hosts, this port is shared across all of them + port 27017 + + # connect using ssl (defaults to false) + ssl true + + # authentication credentials (optional for localhost if you didn't secure it) + username "root" + password "passw0rd" + + # the database in which your users are stored (defaults to admin) + auth_source "admin" + + # which database to connect to (will be created if does not yet exist) + database "my_database" + + end + === Generating or creating a translation diff --git a/lib/mongify/database/base_connection.rb b/lib/mongify/database/base_connection.rb index 181cdb7..74f2b49 100644 --- a/lib/mongify/database/base_connection.rb +++ b/lib/mongify/database/base_connection.rb @@ -8,7 +8,7 @@ class BaseConnection # List of required fields to make a valid base connection REQUIRED_FIELDS = %w{host} # List of all the available fields to make up a connection - AVAILABLE_FIELDS = %w{adapter host username password database socket port encoding batch_size} + AVAILABLE_FIELDS = %w{adapter host username password database socket port encoding batch_size ssl auth_source} # List of all fields that should be forced to a string STRING_FIELDS = %w{adapter} diff --git a/lib/mongify/database/column.rb b/lib/mongify/database/column.rb index c448145..d132960 100644 --- a/lib/mongify/database/column.rb +++ b/lib/mongify/database/column.rb @@ -230,6 +230,8 @@ def scale=(value) # Casts the value to a given type def type_cast(value) return nil if value.nil? + # maintain rails 4 compatibility ('cast' is the new method in rails5) + cast_method = ActiveRecord::Type::DateTime.new.respond_to?('type_cast_from_database') ? 'type_cast_from_database' : 'cast' case type when :key then options['as'] == :string ? value.to_s : value.to_i #If :as is provided, check if it's string, otherwise integer when :string then value.to_s @@ -237,18 +239,18 @@ def type_cast(value) when :integer then value.to_i when :float then value.to_f when :decimal - value = ActiveRecord::Type::Decimal.new.type_cast_from_database(value) + value = ActiveRecord::Type::Decimal.new.send(cast_method, value) if as_integer? (value * (10 ** self.scale)).round.to_i else value.to_s end - when :datetime then ActiveRecord::Type::DateTime.new.type_cast_from_database(value) - when :timestamp then ActiveRecord::Type::DateTime.new.type_cast_from_database(value) - when :time then ActiveRecord::Type::Time.new.type_cast_from_database(value) - when :date then ActiveRecord::Type::DateTime.new.type_cast_from_database(value) - when :binary then ActiveRecord::Type::Binary.new.type_cast_from_database(value) - when :boolean then ActiveRecord::Type::Boolean.new.type_cast_from_database(value) + when :datetime then ActiveRecord::Type::DateTime.new.send(cast_method, value) + when :timestamp then ActiveRecord::Type::DateTime.new.send(cast_method, value) + when :time then ActiveRecord::Type::Time.new.send(cast_method, value) + when :date then ActiveRecord::Type::DateTime.new.send(cast_method, value) + when :binary then ActiveRecord::Type::Binary.new.send(cast_method, value) + when :boolean then ActiveRecord::Type::Boolean.new.send(cast_method, value) else value.to_s end end diff --git a/lib/mongify/database/no_sql_connection.rb b/lib/mongify/database/no_sql_connection.rb index ec7aa28..1222289 100644 --- a/lib/mongify/database/no_sql_connection.rb +++ b/lib/mongify/database/no_sql_connection.rb @@ -19,6 +19,8 @@ module Database # username # password # port + # ssl + # auth_source # # Options: # :force => true # This will force a database drop before processing @@ -26,6 +28,7 @@ module Database # class NoSqlConnection < Mongify::Database::BaseConnection include Mongo + Mongo::Logger.logger.level = ::Logger::INFO #Required fields for a no sql connection @@ -61,11 +64,30 @@ def forced? !!@options['force'] end + def ssl? + !!ssl + end + # Sets up a connection to the database def setup_connection_adapter - connection = Connection.new(host, port) - connection.add_auth(database, username, password) if username && password - connection + options = { database: database, ssl: ssl? } + options = add_auth(options, username, password, auth_source) + if host.nil? + addresses = ["#{host}:#{port}"] + else + @port ||= 27017 + addresses = host.split(",").map { |h| "#{h}:#{@port}" } + end + Mongo::Client.new(addresses, options) + end + + def add_auth(options, username, password, auth_source) + if username && password + options[:user] = username + options[:password] = password + options[:auth_source] = auth_source || 'admin' + end + options end # Returns a mongo connection @@ -79,12 +101,23 @@ def connection # Returns true or false depending if we have a connection to a mongo server def has_connection? - connection.connected? + # this is a hack - mongo v1 exposed a connected? method + # this is no longer true for mongo v2 + # however, mongo.list_databases will throw an exception if not connected + # known issue with this -- if user has no permission to list databases, + # this will throw an error even if connected + begin + connection.list_databases + rescue + return false + end + + return true end # Returns the database from the connection def db - @db ||= connection[database] + @db ||= connection.database end # Returns a hash of all the rows from the database of a given collection @@ -98,12 +131,13 @@ def select_by_query(collection, query) # Inserts into the collection a given row def insert_into(colleciton_name, row) - db[colleciton_name].insert(row) + method_name = row.is_a?(Array) ? "insert_many" : "insert_one" + db[colleciton_name].send(method_name, row) end # Updates a collection item with a given ID with the given attributes def update(colleciton_name, id, attributes) - db[colleciton_name].update({"_id" => id}, attributes) + db[colleciton_name].update_one({"_id" => id}, attributes) end # Upserts into the collection a given row @@ -131,18 +165,18 @@ def upsert(collection_name, row) # Finds one item from a collection with the given query def find_one(collection_name, query) - db[collection_name].find_one(query) + db[collection_name].find(query).try(:first) end # Returns a row of a item from a given collection with a given pre_mongified_id def get_id_using_pre_mongified_id(colleciton_name, pre_mongified_id) - db[colleciton_name].find_one('pre_mongified_id' => pre_mongified_id).try(:[], '_id') + db[colleciton_name].find('pre_mongified_id' => pre_mongified_id).try(:first).try(:[], '_id') end # Removes pre_mongified_id from all records in a given collection def remove_pre_mongified_ids(collection_name) drop_mongified_index(collection_name) - db[collection_name].update({}, { '$unset' => { 'pre_mongified_id' => 1} }, :multi => true) + db[collection_name].update_many({}, { '$unset' => { 'pre_mongified_id' => 1} }) end # Removes pre_mongified_id from collection @@ -150,13 +184,13 @@ def remove_pre_mongified_ids(collection_name) # @return True if successful # @raise MongoDBError if index isn't found def drop_mongified_index(collection_name) - db[collection_name].drop_index('pre_mongified_id_1') if db[collection_name].index_information.keys.include?("pre_mongified_id_1") + db[collection_name].indexes.drop_one('pre_mongified_id_1') unless db[collection_name].indexes.get('pre_mongified_id_1').nil? end # Creates a pre_mongified_id index to ensure # speedy lookup for collections via the pre_mongified_id def create_pre_mongified_id_index(collection_name) - db[collection_name].create_index([['pre_mongified_id', Mongo::ASCENDING]]) + db[collection_name].indexes.create_many([ { key: { pre_mongified_id: 1 } } ]) end # Asks user permission to drop the database @@ -173,7 +207,7 @@ def ask_to_drop_database # Drops the mongodb database def drop_database - connection.drop_database(database) + connection.database.drop end diff --git a/lib/mongify/translation/process.rb b/lib/mongify/translation/process.rb index d0663cc..32a50f3 100644 --- a/lib/mongify/translation/process.rb +++ b/lib/mongify/translation/process.rb @@ -47,7 +47,7 @@ def update_reference_ids rows.each do |row| id = row["_id"] attributes = fetch_reference_ids(t, row) - no_sql_connection.update(t.name, id, {"$set" => attributes}) unless attributes.blank? + no_sql_connection.connection[t.name].update_one( { "_id" => id } , { "$set" => attributes}) unless attributes.blank? Mongify::Status.publish('update_references') end Mongify::Status.publish('update_references', :action => 'finish') diff --git a/lib/mongify/translation/processor_common.rb b/lib/mongify/translation/processor_common.rb index 6969292..dff03d3 100644 --- a/lib/mongify/translation/processor_common.rb +++ b/lib/mongify/translation/processor_common.rb @@ -48,7 +48,7 @@ def copy_embedded_tables row.merge!(fetch_reference_ids(t, row)) row.delete('pre_mongified_id') save_function_call = t.embedded_as_object? ? '$set' : '$addToSet' - no_sql_connection.update(t.embed_in, target_row['_id'], append_parent_object({save_function_call => {t.name => row}}, parent_row, unset_keys)) + no_sql_connection.connection[t.embed_in].update_one( { "_id" => target_row['_id'] }, append_parent_object({save_function_call => {t.name => row}}, parent_row, unset_keys)) Mongify::Status.publish('copy_embedded') end Mongify::Status.publish('copy_embedded', :action => 'finish') diff --git a/lib/mongify/translation/sync.rb b/lib/mongify/translation/sync.rb index 86a4687..135a420 100644 --- a/lib/mongify/translation/sync.rb +++ b/lib/mongify/translation/sync.rb @@ -10,7 +10,7 @@ module Sync DRAFT_KEY = "__mongify_sync_draft__" SYNC_HELPER_TABLE = "__mongify_sync_helper__" - class SyncHelperMigrator < ActiveRecord::Migration + class SyncHelperMigrator < ActiveRecord::Migration[4.2] def up create_table SYNC_HELPER_TABLE, :id => false do |t| t.string :table_name diff --git a/lib/mongify/version.rb b/lib/mongify/version.rb index 7add008..831f6fb 100644 --- a/lib/mongify/version.rb +++ b/lib/mongify/version.rb @@ -1,4 +1,4 @@ module Mongify # Mongify's Current Version Number - VERSION = "1.3.1" + VERSION = "1.4.0" end \ No newline at end of file diff --git a/mongify.gemspec b/mongify.gemspec index df02529..e3313f3 100644 --- a/mongify.gemspec +++ b/mongify.gemspec @@ -12,11 +12,11 @@ Gem::Specification.new do |s| s.description = %q{Mongify allows you to map your sql data into a mongodb document database with a simple DSL.} s.required_ruby_version = ">= 1.8.7" - s.add_runtime_dependency('activerecord', ">= 4.2", "< 5.0") - s.add_runtime_dependency('activesupport', ">= 4.2", "< 5.0") - s.add_runtime_dependency('mongo', "= 1.12.5") - s.add_runtime_dependency('bson', "= 1.12.5") - s.add_runtime_dependency('bson_ext', "= 1.12.5") unless RUBY_PLATFORM == 'java' + s.add_runtime_dependency('activerecord', ">= 4.2", "< 6.0") + s.add_runtime_dependency('activesupport', ">= 4.2", "< 6.0") + s.add_runtime_dependency('bson', "~> 4.0") + s.add_runtime_dependency('mongo', "> 2.0") + s.add_runtime_dependency('bson_ext', '~> 1.5') unless RUBY_PLATFORM == 'java' s.add_runtime_dependency('highline', '= 1.7.8') diff --git a/spec/mongify/database/no_sql_connection_spec.rb b/spec/mongify/database/no_sql_connection_spec.rb index 2234ee5..ca18361 100644 --- a/spec/mongify/database/no_sql_connection_spec.rb +++ b/spec/mongify/database/no_sql_connection_spec.rb @@ -49,17 +49,17 @@ context "connection" do before(:each) do @mock_connection = double(:connected? => true) - Mongo::Connection.stub(:new).and_return(@mock_connection) + Mongo::Client.stub(:new).and_return(@mock_connection) end it "should only create a connection once" do - Mongo::Connection.should_receive(:new).once + Mongo::Client.should_receive(:new).once @mongodb_connection.connection @mongodb_connection.connection end it "should add_auth if username && password is present" do - @mock_connection.should_receive(:add_auth) + @mongodb_connection.should_receive(:add_auth) @mongodb_connection.username "bob" @mongodb_connection.password "secret" @mongodb_connection.connection @@ -72,23 +72,26 @@ before(:each) do @collection = double @db = double + @indexes = double @db.stub(:[]).with('users').and_return(@collection) + @collection.stub(:indexes).and_return(@indexes) @mongodb_connection.stub(:db).and_return(@db) end + context "insert_into" do it "should insert into a table using the mongo driver" do - @collection.should_receive(:insert).with({'first_name' => 'bob'}) + @collection.should_receive(:insert_one).with({'first_name' => 'bob'}) @mongodb_connection.insert_into('users', {'first_name' => 'bob'}) end end context "get_id_using_pre_mongified_id" do it "should return new id" do - @collection.should_receive(:find_one).with({"pre_mongified_id"=>1}).and_return({'_id' => '123'}) + @collection.should_receive(:find).with({"pre_mongified_id"=>1}).and_return([{'_id' => '123'}]) @mongodb_connection.get_id_using_pre_mongified_id('users', 1).should == '123' end it "should return nil if nothing is found" do - @collection.should_receive(:find_one).with({"pre_mongified_id"=>1}).and_return(nil) + @collection.should_receive(:find).with({"pre_mongified_id"=>1}).and_return(nil) @mongodb_connection.get_id_using_pre_mongified_id('users', 1).should == nil end end @@ -111,7 +114,7 @@ context "update" do it "should update the record" do attributes = {'post_id' => 123} - @collection.should_receive(:update).with({"_id" => 1}, attributes) + @collection.should_receive(:update_one).with({"_id" => 1}, attributes) @mongodb_connection.update('users', 1, attributes) end end @@ -145,28 +148,31 @@ context "find_one" do it "should call find_one on collection" do query= {'pre_mongified_id' => 1} - @collection.should_receive(:find_one).with(query) + @collection.should_receive(:find).with(query) @mongodb_connection.find_one('users', query) end end it "should create index for pre_mongified_id" do - @collection.should_receive(:create_index).with([["pre_mongified_id", Mongo::ASCENDING]]).and_return(true) + @collection.indexes + @collection.indexes.should_receive(:create_many).with([ { key: { pre_mongified_id: 1 } } ]).and_return(true) @mongodb_connection.create_pre_mongified_id_index('users') end context "remove_pre_mongified_ids" do before(:each) do - @collection.stub(:index_information).and_return('pre_mongified_id_1' => 'something') + @indexes = double + @indexes.stub(:drop_one) + @collection.stub(:indexes).and_return(@indexes) + @collection.stub(:update_many) + @indexes.stub(:get).and_return('pre_mongified_id_1' => 'something') end it "should call update with unset" do - @collection.should_receive(:update).with({},{'$unset' => {'pre_mongified_id' => 1}}, {:multi=>true}) - @collection.stub(:drop_index) + @collection.should_receive(:update_many).with({},{'$unset' => {'pre_mongified_id' => 1}}) @mongodb_connection.remove_pre_mongified_ids('users') end it "should drop the index" do - @collection.should_receive(:drop_index).with('pre_mongified_id_1') - @collection.stub(:update) + @indexes.should_receive(:drop_one).with('pre_mongified_id_1') @mongodb_connection.remove_pre_mongified_ids('users') end end @@ -175,7 +181,10 @@ context "force" do before(:each) do @mock_connection = double(:connected? => true, :drop_database => true) - Mongo::Connection.stub(:new).and_return(@mock_connection) + @database = double + @database.stub(:drop).and_return(true) + @mock_connection.stub(:database).and_return(@database) + Mongo::Client.stub(:new).and_return(@mock_connection) @mongodb_connection = Mongify::Database::NoSqlConnection.new(:host => 'localhost', :database => 'blue', :force => true) Mongify::UI.stub(:ask).and_return(true) end @@ -187,7 +196,7 @@ end it "should drop database" do - @mongodb_connection.connection.should_receive(:drop_database).with('blue').and_return(true) + @database.should_receive(:drop).and_return(true) @mongodb_connection.send(:drop_database) end @@ -198,12 +207,12 @@ end it "should not drop database if permission is declined" do Mongify::UI.should_receive(:ask).and_return(false) - @mongodb_connection.should_receive(:drop_database).never + @mongodb_connection.should_receive(:drop).never @mongodb_connection.send(:ask_to_drop_database) end it "should drop database if permission is granted" do Mongify::UI.should_receive(:ask).and_return(true) - @mongodb_connection.should_receive(:drop_database).once + @database.should_receive(:drop).once @mongodb_connection.send(:ask_to_drop_database) end end @@ -221,7 +230,7 @@ end it "should return a db" do - @mongodb_connection.db.should be_a Mongify::Database::NoSqlConnection::DB + @mongodb_connection.db.should be_a Mongify::Database::NoSqlConnection::Database end end diff --git a/spec/support/database_generator.rb b/spec/support/database_generator.rb index 0b545e1..d627643 100644 --- a/spec/support/database_generator.rb +++ b/spec/support/database_generator.rb @@ -162,7 +162,7 @@ def self.sqlite(include_data=true) # Drops the database in mongo def self.clear_mongodb - mongo_connection.connection.drop_database mongo_connection.database + mongo_connection.connection.database.drop end # Returns a mongo connection (based on the database.yml)