Class: DbVcs::Adapters::Mongo
- Inherits:
-
Object
- Object
- DbVcs::Adapters::Mongo
- Extended by:
- DbVcs::AdapterInterface
- Defined in:
- lib/db_vcs/adapters/mongo.rb
Defined Under Namespace
Classes: Config
Class Method Summary collapse
- .config ⇒ DbVcs::Adapters::Mongo::Config
- .connection ⇒ Mongo::Client
-
.copy_database(to_db, from_db) ⇒ Object
Void.
-
.create_database(db_name) ⇒ Object
Void.
- .db_exists?(db_name) ⇒ Boolean
- .drop_by_dbname(db_name) ⇒ void
- .list_databases ⇒ Array<String>
Methods included from DbVcs::AdapterInterface
config, connection, copy_database, create_database, db_exists?, drop_by_dbname, list_databases
Class Method Details
.config ⇒ DbVcs::Adapters::Mongo::Config
28 29 30 |
# File 'lib/db_vcs/adapters/mongo.rb', line 28 def config DbVcs.config.mongo_config end |
.connection ⇒ 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.
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.
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
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.
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_databases ⇒ Array<String>
67 68 69 |
# File 'lib/db_vcs/adapters/mongo.rb', line 67 def list_databases connection.database_names end |