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



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

def config
  DbVcs.config.mongo_config
end

.connectionMongo::Client

Returns:

  • (Mongo::Client)


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:

  • to_db (String)
  • from_db (String)

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 =
    <<~SH
    #{config.mongodump_path} #{config.mongo_uri} --db="#{from_db}" --archive --quiet \
    | #{config.mongorestore_path} #{config.mongo_uri} --archive --quiet --nsFrom='#{from_db}.*' --nsTo='#{to_db}.*'
  SH
  `#{command}`
end

.create_database(db_name) ⇒ Object

Returns void.

Parameters:

  • db_name (String)

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:

  • db_name (String)

Returns:

  • (Boolean)


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:

  • db_name (String)


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:

  • (Array<String>)


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

def list_databases
  connection.database_names
end