Class: Mongify::Database::NoSqlConnection

Inherits:
BaseConnection show all
Includes:
Mongo
Defined in:
lib/mongify/database/no_sql_connection.rb

Overview

No Sql Connection configuration

Basic format should look something like this:

no_sql_connection {options} do
  adapter   "mongodb"
  host      "localhost"
  database  "my_database"
end

Possible attributes:

adapter
host
database
username
password
port

Options:

:force => true       # This will force a database drop before processing

You’re also able to set attributes via the options

Constant Summary collapse

REQUIRED_FIELDS =

Required fields for a no sql connection

%w{host database}

Constants inherited from BaseConnection

BaseConnection::AVAILABLE_FIELDS, BaseConnection::STRING_FIELDS

Instance Method Summary collapse

Methods inherited from BaseConnection

#method_missing, #respond_to?, #to_hash

Constructor Details

#initialize(options = {}) ⇒ NoSqlConnection

Returns a new instance of NoSqlConnection.



34
35
36
37
38
# File 'lib/mongify/database/no_sql_connection.rb', line 34

def initialize(options={})
  super options
  @options = options
  adapter 'mongodb' if adapter.nil? || adapter.downcase == "mongo"
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Mongify::Database::BaseConnection

Instance Method Details

#adapter(name = nil) ⇒ Object

Sets and/or returns a adapter It takes care of renaming adapter(‘mongo’) to ‘mongodb’



42
43
44
# File 'lib/mongify/database/no_sql_connection.rb', line 42

def adapter(name=nil)
  super(name)
end

#ask_to_drop_databaseObject

Asks user permission to drop the database

Returns:

  • true or false depending on user’s response



164
165
166
167
168
# File 'lib/mongify/database/no_sql_connection.rb', line 164

def ask_to_drop_database
  if UI.ask("Are you sure you want to drop #{database} database?")
    drop_database
  end
end

#connectionObject

Returns a mongo connection NOTE: If forced? is true, the first time a connection is made, it will ask to drop the database before continuing



74
75
76
77
78
# File 'lib/mongify/database/no_sql_connection.rb', line 74

def connection
  return @connection if @connection
  @connection = setup_connection_adapter
  @connection
end

#connection_stringObject

Returns a connection string that can be used to build a Mongo Connection (Currently this isn’t used due to some issue early on in development)



48
49
50
# File 'lib/mongify/database/no_sql_connection.rb', line 48

def connection_string
  "#{@adapter}://#{@host}#{":#{@port}" if @port}"
end

#create_pre_mongified_id_index(collection_name) ⇒ Object

Creates a pre_mongified_id index to ensure speedy lookup for collections via the pre_mongified_id



158
159
160
# File 'lib/mongify/database/no_sql_connection.rb', line 158

def create_pre_mongified_id_index(collection_name)
  db[collection_name].create_index([['pre_mongified_id', Mongo::ASCENDING]])
end

#dbObject

Returns the database from the connection



86
87
88
# File 'lib/mongify/database/no_sql_connection.rb', line 86

def db
  @db ||= connection[database]
end

#drop_mongified_index(collection_name) ⇒ Object

Removes pre_mongified_id from collection

Parameters:

  • collection_name (String)

    name of collection to remove the index from

Returns:

  • True if successful

Raises:

  • MongoDBError if index isn’t found



152
153
154
# File 'lib/mongify/database/no_sql_connection.rb', line 152

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")
end

#find_one(collection_name, query) ⇒ Object

Finds one item from a collection with the given query



133
134
135
# File 'lib/mongify/database/no_sql_connection.rb', line 133

def find_one(collection_name, query)
  db[collection_name].find_one(query)
end

#forced?Boolean

Returns true if :force was set to true This will force a drop of the database upon connection

Returns:

  • (Boolean)


60
61
62
# File 'lib/mongify/database/no_sql_connection.rb', line 60

def forced?
  !!@options['force']
end

#get_id_using_pre_mongified_id(colleciton_name, pre_mongified_id) ⇒ Object

Returns a row of a item from a given collection with a given pre_mongified_id



138
139
140
# File 'lib/mongify/database/no_sql_connection.rb', line 138

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')
end

#has_connection?Boolean

Returns true or false depending if we have a connection to a mongo server

Returns:

  • (Boolean)


81
82
83
# File 'lib/mongify/database/no_sql_connection.rb', line 81

def has_connection?
  connection.connected?
end

#insert_into(colleciton_name, row) ⇒ Object

Inserts into the collection a given row



100
101
102
# File 'lib/mongify/database/no_sql_connection.rb', line 100

def insert_into(colleciton_name, row)
  db[colleciton_name].insert(row)
end

#remove_pre_mongified_ids(collection_name) ⇒ Object

Removes pre_mongified_id from all records in a given collection



143
144
145
146
# File 'lib/mongify/database/no_sql_connection.rb', line 143

def remove_pre_mongified_ids(collection_name)
  drop_mongified_index(collection_name)
  db[collection_name].update({}, { '$unset' => { 'pre_mongified_id' => 1} }, :multi => true)
end

#select_by_query(collection, query) ⇒ Object



95
96
97
# File 'lib/mongify/database/no_sql_connection.rb', line 95

def select_by_query(collection, query)
  db[collection].find(query)
end

#select_rows(collection) ⇒ Object

Returns a hash of all the rows from the database of a given collection



91
92
93
# File 'lib/mongify/database/no_sql_connection.rb', line 91

def select_rows(collection)
  db[collection].find
end

#setup_connection_adapterObject

Sets up a connection to the database



65
66
67
68
69
# File 'lib/mongify/database/no_sql_connection.rb', line 65

def setup_connection_adapter
  connection = Connection.new(host, port)
  connection.add_auth(database, username, password) if username && password
  connection
end

#update(colleciton_name, id, attributes) ⇒ Object

Updates a collection item with a given ID with the given attributes



105
106
107
# File 'lib/mongify/database/no_sql_connection.rb', line 105

def update(colleciton_name, id, attributes)
  db[colleciton_name].update({"_id" => id}, attributes)
end

#upsert(collection_name, row) ⇒ Object

Upserts into the collection a given row



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/mongify/database/no_sql_connection.rb', line 110

def upsert(collection_name, row)
  # We can't use the save method of the Mongo collection
  # The reason is that it detects duplicates using _id
  # but we should detect it using pre_mongified_id instead
  # because in the case of sync, same rows are identified by their original sql ids
  #
  # db[collection_name].save(row)

  if row.has_key?(:pre_mongified_id) || row.has_key?('pre_mongified_id')
    id = row[:pre_mongified_id] || row['pre_mongified_id']
    duplicate = find_one(collection_name, {"pre_mongified_id" => id})
    if duplicate
      update(collection_name, duplicate[:_id] || duplicate["_id"], row)
    else
      insert_into(collection_name, row)
    end
  else
    # no pre_mongified_id, fallback to the upsert method of Mongo
    db[collection_name].save(row)
  end
end

#valid?Boolean

Returns true or false depending if the given attributes are present and valid to make up a connection to a mongo server

Returns:

  • (Boolean)


54
55
56
# File 'lib/mongify/database/no_sql_connection.rb', line 54

def valid?
  super && @database.present?
end