Skip to content

Commit

Permalink
Merge pull request getredash#312 from erans/master
Browse files Browse the repository at this point in the history
MongoDB ReplicaSet support and a new connection string format.
  • Loading branch information
arikfr committed Oct 21, 2014
2 parents 8aa2d8e + a7bcc6d commit 3f6a0e8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
3 changes: 2 additions & 1 deletion redash/data/query_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ def get_query_runner(connection_type, connection_string):
runner = query_runner_url.url(connection_string)
elif connection_type == "mongo":
from redash.data import query_runner_mongodb
runner = query_runner_mongodb.mongodb(connection_string)
connection_params = json.loads(connection_string)
runner = query_runner_mongodb.mongodb(connection_params)
else:
from redash.data import query_runner_pg
runner = query_runner_pg.pg(connection_string)
Expand Down
19 changes: 17 additions & 2 deletions redash/data/query_runner_mongodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,24 @@ def _convert_date(q, field_name):
q[field_name] = datetime.datetime.fromtimestamp(time.mktime(time.strptime(m[0], "%Y-%m-%d %H:%M")))

def query_runner(query):
db_name = connection_string.split("/")[-1]
if not "dbName" in connection_string or not connection_string["dbName"]:
return None, "dbName is missing from connection string JSON or is empty"

db_name = connection_string["dbName"]

if not "connectionString" in connection_string or not connection_string["connectionString"]:
return None, "connectionString is missing from connection string JSON or is empty"

is_replica_set = True if "replicaSetName" in connection_string and connection_string["replicaSetName"] else False

if is_replica_set:
if not connection_string["replicaSetName"]:
return None, "replicaSetName is set in the connection string JSON but is empty"

db_connection = pymongo.MongoReplicaSetClient(connection_string["connectionString"], replicaSet=connection_string["replicaSetName"])
else:
db_connection = pymongo.MongoClient(connection_string["connectionString"])

db_connection = pymongo.MongoClient(connection_string)
if db_name not in db_connection.database_names():
return None, "Unknown database name '%s'" % db_name

Expand Down

0 comments on commit 3f6a0e8

Please sign in to comment.