mongoid-shell 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md CHANGED
@@ -1,3 +1,19 @@
1
+ Next Release
2
+ ============
3
+
4
+ * Your contribution here.
5
+
6
+ 0.2.0 (1/29/2013)
7
+ =================
8
+
9
+ * Added `Mongoid::Shell::Commands::Mongo` that generates a command line which connects to the session's primary node - [@dblock](https://github.com/dblock).
10
+ * Added `Mongoid::Shell::Commands::Mongostat` that generates a command line for `mongostat` - [@dblock](https://github.com/dblock).
11
+ * Added `Mongoid::Shell::Commands::Mongorestore` that generates a command line for `mongorestore` - [@dblock](https://github.com/dblock).
12
+ * A `Mongoid::Shell::Errors::SessionNotConnectedError` error will be raised when a session is not connected - [@dblock](https://github.com/dblock).
13
+ * Added support for `--out`, `--dbpath`, `--directoryperdb`, `--journal`, `--oplog`, `--repair`, `--forceTableScan`, `--dbpath`, `--ipv6` to `Mongoid::Shell::Commands::Mongodump` - [@dblock](https://github.com/dblock).
14
+ * Added support for `--nodb`, `--norc`, `--quiet` and `--ipv6` to `Mongoid::Shell::Commands::Mongo` - [@dblock](https://github.com/dblock).
15
+ * It's now possible to override built-in `db`, `username`, `password`, `host` and `primary` - [@dblock](https://github.com/dblock).
16
+
1
17
  0.1.0 (1/27/2013)
2
18
  ==================
3
19
 
data/README.md CHANGED
@@ -1,7 +1,20 @@
1
1
  Mongoid::Shell [![Build Status](https://travis-ci.org/dblock/mongoid-shell.png?branch=master)](https://travis-ci.org/dblock/mongoid-shell)
2
2
  ==============
3
3
 
4
- Create mongo command-lines from Mongoid configuration.
4
+ Create mongo command-lines from Mongoid configuration.
5
+
6
+ For example, connect to your production database without having to remember the entire command line using a `db:production:shell` Rake task.
7
+
8
+ ``` ruby
9
+ namespace :db
10
+ namespace :production
11
+ task :shell
12
+ Mongoid.load! File.join(Rails.root, "config/mongoid.yml"), :production
13
+ system Mongoid::Shell::Commands::Mongo.new.to_s
14
+ end
15
+ end
16
+ end
17
+ ```
5
18
 
6
19
  Commands can be created for the current default session or you can pass a session as an argument to a new command.
7
20
 
@@ -10,17 +23,54 @@ Mongoid::Shell::Commands::Mongodump.new # will use Mongoid.default_session
10
23
  Mongoid::Shell::Commands::Mongodump.new(session: Moped::Session.new([ "127.0.0.1:27017" ]))
11
24
  ```
12
25
 
26
+ Commands accept parameters.
27
+
28
+ ``` ruby
29
+ out = File.join(Dir.tmpdir, 'db_backup')
30
+ mongodump = Mongoid::Shell::Commands::Mongodump.new(db: 'another_database', out: out)
31
+ system mongodump.to_s # mongodump --db another_database --out /tmp/db_backup
32
+ ```
33
+
13
34
  Supported Commands
14
35
  ==================
15
36
 
16
- ### Mongodump
37
+ ## Mongo
38
+
39
+ The mongo shell is an interactive JavaScript shell for MongoDB. The `Mongoid::Shell::Commands::Mongo` class generates a command line to connect to MongoDB. A particularly useful feature is that it will always yield the address of the master node of a MongoDB replica set.
40
+
41
+ ``` ruby
42
+ Mongoid::Shell::Commands::Mongo.new.to_s
43
+ ```
44
+
45
+ Supports `--username`, `--password`, `--eval`, `--nodb`, `--norc`, `--quiet` and `--ipv6`.
46
+
47
+ ## Mongodump
48
+
49
+ Mongodump is a utility for creating a binary export of the contents of a database.
17
50
 
18
51
  ``` ruby
19
52
  mongodump = Mongoid::Shell::Commands::Mongodump.new({ collection: 'test' })
20
- mongodump.to_s # mongodump --host localhost:27017 --db test --collection test
53
+ mongodump.to_s # mongodump --db test --collection test
54
+ ```
55
+
56
+ The `Mongoid::Shell::Commands::Mongodump` class supports `--db`, `--host`, `--username`, `--password`, `--query`, `--out`, `--collection`, `--directoryperdb`, `--journal`, `--oplog`, `--repair`, `--forceTableScan`, `--dbpath` and `--ipv6`.
57
+
58
+ ## Mongorestore
59
+
60
+ The mongorestore tool imports content from binary database dump, created by mongodump into a specific database.
61
+
62
+ ``` ruby
63
+ mongorestore = Mongoid::Shell::Commands::Mongorestore.new({ collection: 'test', restore: '/tmp/db_backup' })
64
+ mongorestore.to_s # mongorestore --db test --collection test /tmp/db_backup
21
65
  ```
22
66
 
23
- Supports `--db`, `--host`, `--username`, `--password` and `--collection`.
67
+ The `Mongoid::Shell::Commands::Mongorestore` class supports `--db`, `--host`, `--username`, `--password`, `--collection`, `--ipv6, `--dbpath`, `--directoryperdb`, `--journal`, `--objcheck`, `--filter`, `--drop`, `--oplogReplay` and `--keepIndexVersion`.
68
+
69
+ ## Mongostat
70
+
71
+ The mongostat utility provides a quick overview of the status of a currently running mongod or mongos instance.
72
+
73
+ The `Mongoid::Shell::Commands::Mongostat` class supports `--host`, `--username`, `--password`, `--rowcount`, `--discover`, `--noheaders`, `--http` and `--all`.
24
74
 
25
75
  Contributing
26
76
  ------------
@@ -6,5 +6,16 @@ en:
6
6
  missing_session:
7
7
  message: "Missing session."
8
8
  summary: "The command requires a session."
9
- resolution: "Your code is missing a parameter to `new` or `command_for`.
10
- Typically you can obtain a session from `Mongoid.default_session`."
9
+ resolution: "Your code is missing a parameter to `new` or `command_for`.\n
10
+ \_Typically you can obtain a session from `Mongoid.default_session`.\n
11
+ \_Examples:\n
12
+ \_\_Mongoid::Shell::Commands::Mongodump.new(session: Mongoid.default_session).to_s\n
13
+ \_\_Mongoid::Shell::Commands::Mongodump.command_for(Mongoid.default_session).to_s"
14
+ session_not_connected:
15
+ message: "Session is not connected."
16
+ summary: "The command requires a connected session."
17
+ resolution: "Ensure that the database is up and can be reached."
18
+ missing_primary_node:
19
+ message: "Session does not have a primary node."
20
+ summary: "The command requires a primary node."
21
+ resolution: "Ensure that the database is up and can be reached."
@@ -1,2 +1,5 @@
1
1
  require 'mongoid/shell/commands/base'
2
+ require 'mongoid/shell/commands/mongo'
2
3
  require 'mongoid/shell/commands/mongodump'
4
+ require 'mongoid/shell/commands/mongorestore'
5
+ require 'mongoid/shell/commands/mongostat'
@@ -30,9 +30,14 @@ module Mongoid
30
30
  args.map do |key, property|
31
31
  value = self.send(property)
32
32
  next unless value
33
- # TODO: quote other special characters?
34
- value = '"' + value + '"' if value.include? ' '
35
- "#{key} #{value}"
33
+ if value.is_a? Boolean
34
+ key
35
+ else
36
+ value = value.to_s
37
+ # TODO: quote other special characters?
38
+ value = '"' + value + '"' if value.include? ' '
39
+ key[0] == '-' ? "#{key} #{value}" : value
40
+ end
36
41
  end
37
42
  end
38
43
 
@@ -43,4 +48,4 @@ module Mongoid
43
48
  end
44
49
  end
45
50
  end
46
- end
51
+ end
@@ -0,0 +1,36 @@
1
+ module Mongoid
2
+ module Shell
3
+ module Commands
4
+ class Mongo < Mongoid::Shell::Commands::Base
5
+ include Mongoid::Shell::Properties::Primary
6
+ include Mongoid::Shell::Properties::Database
7
+ include Mongoid::Shell::Properties::Username
8
+ include Mongoid::Shell::Properties::Password
9
+
10
+ attr_accessor :eval, :nodb, :norc, :quiet, :ipv6
11
+
12
+ def initialize(attrs = {})
13
+ super
14
+ end
15
+
16
+ def host_port_and_db
17
+ [ primary, db ].compact.join("/")
18
+ end
19
+
20
+ def vargs
21
+ super({
22
+ 'db address' => :host_port_and_db,
23
+ '--username' => :username,
24
+ '--password' => :password,
25
+ '--eval' => :eval,
26
+ '--nodb' => :nodb,
27
+ '--norc' => :norc,
28
+ '--quiet' => :quiet,
29
+ '--ipv6' => :ipv6,
30
+ })
31
+ end
32
+
33
+ end
34
+ end
35
+ end
36
+ end
@@ -7,7 +7,7 @@ module Mongoid
7
7
  include Mongoid::Shell::Properties::Username
8
8
  include Mongoid::Shell::Properties::Password
9
9
 
10
- attr_accessor :collection, :query
10
+ attr_accessor :collection, :query, :out, :directoryperdb, :journal, :oplog, :repair, :forceTableScan, :dbpath, :ipv6
11
11
 
12
12
  def initialize(attrs = {})
13
13
  super
@@ -16,15 +16,23 @@ module Mongoid
16
16
  def vargs
17
17
  super({
18
18
  '--host' => :host,
19
- '--db' => :database_name,
19
+ '--db' => :db,
20
20
  '--username' => :username,
21
21
  '--password' => :password,
22
22
  '--collection' => :collection,
23
- '--query' => :query
23
+ '--query' => :query,
24
+ '--out' => :out,
25
+ '--directoryperdb' => :directoryperdb,
26
+ '--journal' => :journal,
27
+ '--oplog' => :oplog,
28
+ '--repair' => :repair,
29
+ '--forceTableScan' => :forceTableScan,
30
+ '--dbpath' => :dbpath,
31
+ '--ipv6' => :ipv6
24
32
  })
25
33
  end
26
34
 
27
35
  end
28
36
  end
29
37
  end
30
- end
38
+ end
@@ -0,0 +1,39 @@
1
+ module Mongoid
2
+ module Shell
3
+ module Commands
4
+ class Mongorestore < Mongoid::Shell::Commands::Base
5
+ include Mongoid::Shell::Properties::Host
6
+ include Mongoid::Shell::Properties::Database
7
+ include Mongoid::Shell::Properties::Username
8
+ include Mongoid::Shell::Properties::Password
9
+
10
+ attr_accessor :collection, :ipv6, :dbpath, :directoryperdb, :journal, :objcheck, :filter, :drop, :oplogReplay, :keepIndexVersion, :restore
11
+
12
+ def initialize(attrs = {})
13
+ super
14
+ end
15
+
16
+ def vargs
17
+ super({
18
+ '--host' => :host,
19
+ '--db' => :db,
20
+ '--username' => :username,
21
+ '--password' => :password,
22
+ '--collection' => :collection,
23
+ '--ipv6' => :ipv6,
24
+ '--dbpath' => :dbpath,
25
+ '--directoryperdb' => :directoryperdb,
26
+ '--journal' => :journal,
27
+ '--objcheck' => :objcheck,
28
+ '--filter' => :filter,
29
+ '--drop' => :drop,
30
+ '--oplogReplay' => :oplogReplay,
31
+ '--keepIndexVersion' => :keepIndexVersion,
32
+ 'directory or filename to restore from' => :restore
33
+ })
34
+ end
35
+
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,31 @@
1
+ module Mongoid
2
+ module Shell
3
+ module Commands
4
+ class Mongostat < Mongoid::Shell::Commands::Base
5
+ include Mongoid::Shell::Properties::Host
6
+ include Mongoid::Shell::Properties::Username
7
+ include Mongoid::Shell::Properties::Password
8
+
9
+ attr_accessor :rowcount, :discover, :all, :http, :noheaders
10
+
11
+ def initialize(attrs = {})
12
+ super
13
+ end
14
+
15
+ def vargs
16
+ super({
17
+ '--host' => :host,
18
+ '--username' => :username,
19
+ '--password' => :password,
20
+ '--rowcount' => :rowcount,
21
+ '--discover' => :discover,
22
+ '--noheaders' => :noheaders,
23
+ '--http' => :http,
24
+ '--all' => :all
25
+ })
26
+ end
27
+
28
+ end
29
+ end
30
+ end
31
+ end
@@ -1,2 +1,4 @@
1
1
  require 'mongoid/shell/errors/base'
2
2
  require 'mongoid/shell/errors/missing_session_error'
3
+ require 'mongoid/shell/errors/session_not_connected_error'
4
+ require 'mongoid/shell/errors/missing_primary_node_error'
@@ -0,0 +1,11 @@
1
+ module Mongoid
2
+ module Shell
3
+ module Errors
4
+ class MissingPrimaryNodeError < Mongoid::Shell::Errors::Base
5
+ def initialize
6
+ super(compose_message("missing_primary_node"))
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module Mongoid
2
+ module Shell
3
+ module Errors
4
+ class SessionNotConnectedError < Mongoid::Shell::Errors::Base
5
+ def initialize
6
+ super(compose_message("session_not_connected"))
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -1,4 +1,5 @@
1
1
  require 'mongoid/shell/properties/host'
2
+ require 'mongoid/shell/properties/primary'
2
3
  require 'mongoid/shell/properties/database'
3
4
  require 'mongoid/shell/properties/username'
4
5
  require 'mongoid/shell/properties/password'
@@ -3,12 +3,14 @@ module Mongoid
3
3
  module Properties
4
4
  module Database
5
5
 
6
+ attr_accessor :db
7
+
6
8
  # current database name
7
- def database_name
8
- session.send(:current_database).name
9
+ def db
10
+ @db || session.send(:current_database).name
9
11
  end
10
12
 
11
13
  end
12
14
  end
13
15
  end
14
- end
16
+ end
@@ -3,12 +3,18 @@ module Mongoid
3
3
  module Properties
4
4
  module Host
5
5
 
6
+ attr_accessor :host
7
+
6
8
  # database host
7
9
  def host
8
- session.cluster.nodes.first.address
10
+ @host || begin
11
+ node = session.cluster.nodes.first
12
+ raise Mongoid::Shell::Errors::SessionNotConnectedError unless node
13
+ node.address == "localhost:27017" ? nil : node.address
14
+ end
9
15
  end
10
16
 
11
17
  end
12
18
  end
13
19
  end
14
- end
20
+ end
@@ -3,13 +3,17 @@ module Mongoid
3
3
  module Properties
4
4
  module Password
5
5
 
6
+ attr_accessor :password
7
+
6
8
  # current password
7
9
  def password
8
- return nil unless session.context.cluster.auth && session.context.cluster.auth.first
9
- session.context.cluster.auth.first[1][1]
10
+ @password || begin
11
+ return nil unless session.context.cluster.auth && session.context.cluster.auth.first
12
+ session.context.cluster.auth.first[1][1]
13
+ end
10
14
  end
11
15
 
12
16
  end
13
17
  end
14
18
  end
15
- end
19
+ end
@@ -0,0 +1,21 @@
1
+ module Mongoid
2
+ module Shell
3
+ module Properties
4
+ module Primary
5
+
6
+ attr_accessor :primary
7
+
8
+ # primary database host
9
+ def primary
10
+ @primary || begin
11
+ raise Mongoid::Shell::Errors::SessionNotConnectedError unless session.cluster.nodes.any?
12
+ node = session.cluster.nodes.find(&:primary?)
13
+ raise Mongoid::Shell::Errors::MissingPrimaryNodeError unless node
14
+ node.address == "localhost:27017" ? nil : node.address
15
+ end
16
+ end
17
+
18
+ end
19
+ end
20
+ end
21
+ end
@@ -3,13 +3,17 @@ module Mongoid
3
3
  module Properties
4
4
  module Username
5
5
 
6
+ attr_accessor :username
7
+
6
8
  # current username
7
9
  def username
8
- return nil unless session.context.cluster.auth && session.context.cluster.auth.first
9
- session.context.cluster.auth.first[1][0]
10
+ @username || begin
11
+ return nil unless session.context.cluster.auth && session.context.cluster.auth.first
12
+ session.context.cluster.auth.first[1][0]
13
+ end
10
14
  end
11
15
 
12
16
  end
13
17
  end
14
18
  end
15
- end
19
+ end
@@ -1,5 +1,5 @@
1
1
  module Mongoid
2
2
  module Shell
3
- VERSION = '0.1.0'
3
+ VERSION = '0.2.0'
4
4
  end
5
5
  end
@@ -6,12 +6,12 @@ describe Mongoid::Shell::Commands::Base do
6
6
  Mongoid.stub(:default_session).and_return(nil)
7
7
  end
8
8
  it "raises an exception when a session is missing" do
9
- expect {
9
+ expect {
10
10
  Mongoid::Shell::Commands::Base.new({})
11
11
  }.to raise_error Mongoid::Shell::Errors::MissingSessionError, /Missing session./
12
12
  end
13
13
  it "raises an exception when options are missing" do
14
- expect {
14
+ expect {
15
15
  Mongoid::Shell::Commands::Base.new(nil)
16
16
  }.to raise_error Mongoid::Shell::Errors::MissingSessionError, /Missing session./
17
17
  end
@@ -0,0 +1,61 @@
1
+ require 'spec_helper'
2
+
3
+ describe Mongoid::Shell::Commands::Mongo do
4
+ include MopedSessionHelper
5
+ context "local" do
6
+ it "defaults to local" do
7
+ Mongoid::Shell::Commands::Mongo.new.to_s.should == "mongo mongoid_shell_tests"
8
+ end
9
+ it "includes eval" do
10
+ Mongoid::Shell::Commands::Mongo.new({
11
+ eval: 'find x'
12
+ }).to_s.should == 'mongo mongoid_shell_tests --eval "find x"'
13
+ end
14
+ it "overrides primary" do
15
+ Mongoid::Shell::Commands::Mongo.new({
16
+ eval: 'find x',
17
+ primary: 'my_primary'
18
+ }).to_s.should == 'mongo my_primary/mongoid_shell_tests --eval "find x"'
19
+ end
20
+ [ :nodb, :norc, :quiet, :ipv6 ].each do |option|
21
+ it "includes #{option}" do
22
+ Mongoid::Shell::Commands::Mongo.new({
23
+ option => true
24
+ }).to_s.should == "mongo mongoid_shell_tests --#{option}"
25
+ end
26
+ end
27
+
28
+ end
29
+ context "sessions" do
30
+ context "default" do
31
+ before :each do
32
+ @session = Mongoid::Sessions.with_name(:default)
33
+ end
34
+ it "includes username and password" do
35
+ Mongoid::Shell::Commands::Mongo.new({
36
+ session: @session
37
+ }).to_s.should == "mongo mongoid_shell_tests"
38
+ end
39
+ end
40
+ context "a replica set" do
41
+ before :each do
42
+ @session = moped_session(:replica_set)
43
+ end
44
+ it "includes username and password" do
45
+ Mongoid::Shell::Commands::Mongo.new({
46
+ session: @session
47
+ }).to_s.should == "mongo dedicated1.myapp.com:27017/mongoid --username user --password password"
48
+ end
49
+ end
50
+ context "url" do
51
+ before :each do
52
+ @session = moped_session(:url)
53
+ end
54
+ it "includes username and password" do
55
+ Mongoid::Shell::Commands::Mongo.new({
56
+ session: @session
57
+ }).to_s.should == "mongo 59.1.22.1:27017/mongoid --username user --password password"
58
+ end
59
+ end
60
+ end
61
+ end
@@ -1,19 +1,66 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Mongoid::Shell::Commands::Mongodump do
4
- context "default session" do
4
+ include MopedSessionHelper
5
+ context "local" do
5
6
  it "defaults to local" do
6
- Mongoid::Shell::Commands::Mongodump.new.to_s.should == "mongodump --host localhost:27017 --db mongoid_shell_tests"
7
+ Mongoid::Shell::Commands::Mongodump.new.to_s.should == "mongodump --db mongoid_shell_tests"
7
8
  end
8
9
  it "includes collection" do
9
- Mongoid::Shell::Commands::Mongodump.new({
10
+ Mongoid::Shell::Commands::Mongodump.new({
10
11
  collection: 'test'
11
- }).to_s.should == "mongodump --host localhost:27017 --db mongoid_shell_tests --collection test"
12
+ }).to_s.should == "mongodump --db mongoid_shell_tests --collection test"
12
13
  end
13
14
  it "includes query" do
14
- Mongoid::Shell::Commands::Mongodump.new({
15
- query: 'find x'
16
- }).to_s.should == 'mongodump --host localhost:27017 --db mongoid_shell_tests --query "find x"'
15
+ Mongoid::Shell::Commands::Mongodump.new({
16
+ query: 'find x'
17
+ }).to_s.should == 'mongodump --db mongoid_shell_tests --query "find x"'
18
+ end
19
+ [ :out, :dbpath ].each do |option|
20
+ it "includes #{option}" do
21
+ Mongoid::Shell::Commands::Mongodump.new({
22
+ option => '/this is a folder'
23
+ }).to_s.should == "mongodump --db mongoid_shell_tests --#{option} \"/this is a folder\""
24
+ end
25
+ end
26
+ [ :directoryperdb, :journal, :oplog, :repair, :forceTableScan, :dbpath, :ipv6 ].each do |option|
27
+ it "includes #{option}" do
28
+ Mongoid::Shell::Commands::Mongodump.new({
29
+ option => true
30
+ }).to_s.should == "mongodump --db mongoid_shell_tests --#{option}"
31
+ end
32
+ end
33
+ end
34
+ context "sessions" do
35
+ context "default" do
36
+ before :each do
37
+ @session = moped_session(:default)
38
+ end
39
+ it "includes username and password" do
40
+ Mongoid::Shell::Commands::Mongodump.new({
41
+ session: @session
42
+ }).to_s.should == "mongodump --db mongoid_shell_tests"
43
+ end
44
+ end
45
+ context "a replica set" do
46
+ before :each do
47
+ @session = moped_session(:replica_set)
48
+ end
49
+ it "includes username and password" do
50
+ Mongoid::Shell::Commands::Mongodump.new({
51
+ session: @session
52
+ }).to_s.should == "mongodump --host dedicated1.myapp.com:27017 --db mongoid --username user --password password"
53
+ end
54
+ end
55
+ context "url" do
56
+ before :each do
57
+ @session = moped_session(:url)
58
+ end
59
+ it "includes username and password" do
60
+ Mongoid::Shell::Commands::Mongodump.new({
61
+ session: @session
62
+ }).to_s.should == "mongodump --host 59.1.22.1:27017 --db mongoid --username user --password password"
63
+ end
17
64
  end
18
65
  end
19
66
  end
@@ -0,0 +1,76 @@
1
+ require 'spec_helper'
2
+
3
+ describe Mongoid::Shell::Commands::Mongorestore do
4
+ include MopedSessionHelper
5
+ context "local" do
6
+ it "defaults to local" do
7
+ Mongoid::Shell::Commands::Mongorestore.new.to_s.should == "mongorestore --db mongoid_shell_tests"
8
+ end
9
+ it "includes collection" do
10
+ Mongoid::Shell::Commands::Mongorestore.new({
11
+ collection: 'test',
12
+ restore: 'folder'
13
+ }).to_s.should == "mongorestore --db mongoid_shell_tests --collection test folder"
14
+ end
15
+ it "can override the database, username, password and host" do
16
+ Mongoid::Shell::Commands::Mongorestore.new({
17
+ host: 'my_host',
18
+ username: 'my_username',
19
+ password: 'my_password',
20
+ collection: 'test',
21
+ db: 'my_db',
22
+ restore: 'folder'
23
+ }).to_s.should == "mongorestore --host my_host --db my_db --username my_username --password my_password --collection test folder"
24
+ end
25
+ [ :collection, :dbpath, :filter ].each do |option|
26
+ it "includes #{option}" do
27
+ Mongoid::Shell::Commands::Mongorestore.new({
28
+ option => 'var arg',
29
+ restore: 'a folder'
30
+ }).to_s.should == "mongorestore --db mongoid_shell_tests --#{option} \"var arg\" \"a folder\""
31
+ end
32
+ end
33
+ [ :ipv6, :directoryperdb, :journal, :objcheck, :drop, :oplogReplay, :keepIndexVersion ].each do |option|
34
+ it "includes #{option}" do
35
+ Mongoid::Shell::Commands::Mongorestore.new({
36
+ option => true
37
+ }).to_s.should == "mongorestore --db mongoid_shell_tests --#{option}"
38
+ end
39
+ end
40
+ end
41
+ context "sessions" do
42
+ context "default" do
43
+ before :each do
44
+ @session = moped_session(:default)
45
+ end
46
+ it "includes username and password" do
47
+ Mongoid::Shell::Commands::Mongorestore.new({
48
+ session: @session,
49
+ restore: "a folder"
50
+ }).to_s.should == "mongorestore --db mongoid_shell_tests \"a folder\""
51
+ end
52
+ end
53
+ context "a replica set" do
54
+ before :each do
55
+ @session = moped_session(:replica_set)
56
+ end
57
+ it "includes username and password" do
58
+ Mongoid::Shell::Commands::Mongorestore.new({
59
+ session: @session,
60
+ restore: "a folder"
61
+ }).to_s.should == "mongorestore --host dedicated1.myapp.com:27017 --db mongoid --username user --password password \"a folder\""
62
+ end
63
+ end
64
+ context "url" do
65
+ before :each do
66
+ @session = moped_session(:url)
67
+ end
68
+ it "includes username and password" do
69
+ Mongoid::Shell::Commands::Mongorestore.new({
70
+ session: @session,
71
+ restore: "a folder"
72
+ }).to_s.should == "mongorestore --host 59.1.22.1:27017 --db mongoid --username user --password password \"a folder\""
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,54 @@
1
+ require 'spec_helper'
2
+
3
+ describe Mongoid::Shell::Commands::Mongostat do
4
+ include MopedSessionHelper
5
+ context "local" do
6
+ it "defaults to local" do
7
+ Mongoid::Shell::Commands::Mongostat.new.to_s.should == "mongostat"
8
+ end
9
+ it "rowcount" do
10
+ Mongoid::Shell::Commands::Mongostat.new({
11
+ :rowcount => 10
12
+ }).to_s.should == "mongostat --rowcount 10"
13
+ end
14
+ [ :http, :discover, :all, :noheaders ].each do |option|
15
+ it "includes #{option}" do
16
+ Mongoid::Shell::Commands::Mongostat.new({
17
+ option => true
18
+ }).to_s.should == "mongostat --#{option}"
19
+ end
20
+ end
21
+ end
22
+ context "sessions" do
23
+ context "default" do
24
+ before :each do
25
+ @session = Mongoid::Sessions.with_name(:default)
26
+ end
27
+ it "includes username and password" do
28
+ Mongoid::Shell::Commands::Mongostat.new({
29
+ session: @session
30
+ }).to_s.should == "mongostat"
31
+ end
32
+ end
33
+ context "a replica set" do
34
+ before :each do
35
+ @session = moped_session(:replica_set)
36
+ end
37
+ it "includes username and password" do
38
+ Mongoid::Shell::Commands::Mongostat.new({
39
+ session: @session
40
+ }).to_s.should == "mongostat --host dedicated1.myapp.com:27017 --username user --password password"
41
+ end
42
+ end
43
+ context "url" do
44
+ before :each do
45
+ @session = moped_session(:url)
46
+ end
47
+ it "includes username and password" do
48
+ Mongoid::Shell::Commands::Mongostat.new({
49
+ session: @session
50
+ }).to_s.should == "mongostat --host 59.1.22.1:27017 --username user --password password"
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+
3
+ describe Mongoid::Shell::Properties::Host do
4
+ subject do
5
+ klass = Class.new do
6
+ include Mongoid::Shell::Properties::Host
7
+
8
+ def session
9
+ Mongoid.default_session
10
+ end
11
+ end
12
+ klass.new
13
+ end
14
+ it "raises an exception when the session is not connected" do
15
+ Mongoid.default_session.cluster.stub(:nodes).and_return([])
16
+ expect {
17
+ subject.host
18
+ }.to raise_error Mongoid::Shell::Errors::SessionNotConnectedError, /Session is not connected./
19
+ end
20
+ end
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+
3
+ describe Mongoid::Shell::Properties::Primary do
4
+ subject do
5
+ klass = Class.new do
6
+ include Mongoid::Shell::Properties::Primary
7
+
8
+ def session
9
+ Mongoid.default_session
10
+ end
11
+ end
12
+ klass.new
13
+ end
14
+ it "raises an exception when the session is not connected" do
15
+ Mongoid.default_session.cluster.nodes.first.stub(:primary?).and_return(false)
16
+ expect {
17
+ subject.primary
18
+ }.to raise_error Mongoid::Shell::Errors::MissingPrimaryNodeError, /Session does not have a primary node./
19
+ end
20
+ end
data/spec/spec_helper.rb CHANGED
@@ -6,6 +6,12 @@ require 'rspec'
6
6
  require 'mongoid'
7
7
  require 'mongoid-shell'
8
8
 
9
+ [ "support/helpers/*.rb" ].each do |path|
10
+ Dir["#{File.dirname(__FILE__)}/#{path}"].each do |file|
11
+ require file
12
+ end
13
+ end
14
+
9
15
  Mongoid.configure do |config|
10
16
  config.connect_to('mongoid_shell_tests')
11
17
  end
@@ -0,0 +1,37 @@
1
+ # Tell Mongoid which environment this configuration is for.
2
+ production:
3
+ # This starts the session configuration settings. You may have as
4
+ # many sessions as you like, but you must have at least 1 named
5
+ # 'default'.
6
+ sessions:
7
+ # Define the default session.
8
+ default:
9
+ # A session can have any number of hosts. Usually 1 for a single
10
+ # server setup, and at least 3 for a replica set. Hosts must be
11
+ # an array of host:port pairs. This session is single server.
12
+ hosts:
13
+ - flame.mongohq.com:27017
14
+ # Define the default database name.
15
+ database: mongoid
16
+ # Since this database points at a session connected to MongoHQ, we must
17
+ # provide the authentication details.
18
+ username: user
19
+ password: password
20
+ # This defines a secondary session at a replica set.
21
+ replica_set:
22
+ # This configuration is a 3 node replica set.
23
+ hosts:
24
+ - dedicated1.myapp.com:27017
25
+ - dedicated2.myapp.com:27017
26
+ - dedicated3.myapp.com:27017
27
+ database: mongoid
28
+ # We can set session specific options, like reads executing
29
+ # on secondary nodes, and defaulting the session to safe mode.
30
+ options:
31
+ consistency: :eventual
32
+ safe: true
33
+ username: user
34
+ password: password
35
+ # This configuration shows an authenticated replica set via a uri.
36
+ url:
37
+ uri: mongodb://user:password@59.1.22.1:27017,59.1.22.2:27017/mongoid
@@ -0,0 +1,11 @@
1
+ module MopedSessionHelper
2
+ # returns a Moped session with stubbed address resolution
3
+ def moped_session(name)
4
+ config = File.join(File.dirname(__FILE__), "../../support/config/mongoid.yml")
5
+ Mongoid.load! config, :production
6
+ Moped::Node.any_instance.stub(:resolve_address)
7
+ session = Mongoid::Sessions.with_name(name)
8
+ session.cluster.nodes.last.stub(:primary?).and_return(true)
9
+ session
10
+ end
11
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid-shell
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-27 00:00:00.000000000 Z
12
+ date: 2013-01-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: mongoid
@@ -61,22 +61,35 @@ files:
61
61
  - lib/mongoid-shell.rb
62
62
  - lib/mongoid/shell/commands.rb
63
63
  - lib/mongoid/shell/commands/base.rb
64
+ - lib/mongoid/shell/commands/mongo.rb
64
65
  - lib/mongoid/shell/commands/mongodump.rb
66
+ - lib/mongoid/shell/commands/mongorestore.rb
67
+ - lib/mongoid/shell/commands/mongostat.rb
65
68
  - lib/mongoid/shell/errors.rb
66
69
  - lib/mongoid/shell/errors/base.rb
70
+ - lib/mongoid/shell/errors/missing_primary_node_error.rb
67
71
  - lib/mongoid/shell/errors/missing_session_error.rb
72
+ - lib/mongoid/shell/errors/session_not_connected_error.rb
68
73
  - lib/mongoid/shell/properties.rb
69
74
  - lib/mongoid/shell/properties/database.rb
70
75
  - lib/mongoid/shell/properties/host.rb
71
76
  - lib/mongoid/shell/properties/password.rb
77
+ - lib/mongoid/shell/properties/primary.rb
72
78
  - lib/mongoid/shell/properties/username.rb
73
79
  - lib/mongoid/shell/version.rb
74
80
  - lib/mongoid_shell.rb
75
81
  - mongoid-shell.gemspec
76
82
  - spec/mongoid/commands/base_spec.rb
83
+ - spec/mongoid/commands/mongo_spec.rb
77
84
  - spec/mongoid/commands/mongodump_spec.rb
85
+ - spec/mongoid/commands/mongorestore_spec.rb
86
+ - spec/mongoid/commands/mongostat_spec.rb
78
87
  - spec/mongoid/mongoid_shell_spec.rb
88
+ - spec/mongoid/properties/host_spec.rb
89
+ - spec/mongoid/properties/primary_spec.rb
79
90
  - spec/spec_helper.rb
91
+ - spec/support/config/mongoid.yml
92
+ - spec/support/helpers/moped_session_helper.rb
80
93
  homepage: http://github.com/dblock/mongoid-shell
81
94
  licenses:
82
95
  - MIT
@@ -92,7 +105,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
92
105
  version: '0'
93
106
  segments:
94
107
  - 0
95
- hash: 408535153
108
+ hash: 4237627171487641625
96
109
  required_rubygems_version: !ruby/object:Gem::Requirement
97
110
  none: false
98
111
  requirements: