Class: DbVcs::Adapters::Mongo

Inherits:
Object
  • Object
show all
Extended by:
DbVcs::AdapterInterface
Defined in:
lib/db_vcs/adapters/mongo.rb

Defined Under Namespace

Classes: Config

Class Method Summary collapse

Methods included from DbVcs::AdapterInterface

config, connection, copy_database, create_database, db_exists?, drop_by_dbname, list_databases

Class Method Details

.configDbVcs::Adapters::Mongo::Config

Returns:



28
29
30
# File 'lib/db_vcs/adapters/mongo.rb', line 28

def config
  DbVcs.config.mongo_config
end

.connectionMongo::Client

Returns:



33
34
35
36
37
38
39
# File 'lib/db_vcs/adapters/mongo.rb', line 33

def connection
  @connection ||=
    begin
      require "mongo"
      ::Mongo::Client.new(config.mongo_uri)
    end
end

.copy_database(to_db, from_db) ⇒ Object

Returns void.

Parameters:

Returns:

  • void



50
51
52
53
54
55
56
57
# File 'lib/db_vcs/adapters/mongo.rb', line 50

def copy_database(to_db, from_db)
  command =
    "    \#{config.mongodump_path} \#{config.mongo_uri} --db=\"\#{from_db}\" --archive --quiet \\\n    | \#{config.mongorestore_path} \#{config.mongo_uri} --archive --quiet --nsFrom='\#{from_db}.*' --nsTo='\#{to_db}.*'\n  SH\n  `\#{command}`\nend\n"

.create_database(db_name) ⇒ Object

Returns void.

Parameters:

Returns:

  • void



61
62
63
64
# File 'lib/db_vcs/adapters/mongo.rb', line 61

def create_database(db_name)
  # Mongodb databases should contain at least one collection
  connection.use(db_name).database.collection("_db_vcs").insert_one(_db_vcs: 1)
end

.db_exists?(db_name) ⇒ Boolean

Parameters:

Returns:



43
44
45
# File 'lib/db_vcs/adapters/mongo.rb', line 43

def db_exists?(db_name)
  list_databases.include?(db_name)
end

.drop_by_dbname(db_name) ⇒ void

This method returns an undefined value.

Parameters:



73
74
75
# File 'lib/db_vcs/adapters/mongo.rb', line 73

def drop_by_dbname(db_name)
  connection.use(db_name).database.drop
end

.list_databasesArray<String>

Returns:



67
68
69
# File 'lib/db_vcs/adapters/mongo.rb', line 67

def list_databases
  connection.database_names
end