Skip to content
This repository has been archived by the owner on Mar 24, 2021. It is now read-only.

Commit

Permalink
Change to MongoReplicaSetClient with multiple hosts
Browse files Browse the repository at this point in the history
- Replace lots of instances of `'localhost'` with `['localhost']`
- Lots of tests hard code this: TODO - change to respect the app.config?
  • Loading branch information
Ralph Cowling committed Jan 23, 2014
1 parent 0c8247f commit daf87ec
Show file tree
Hide file tree
Showing 15 changed files with 30 additions and 18 deletions.
2 changes: 1 addition & 1 deletion backdrop/admin/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
app.url_map.converters["bucket"] = BucketConverter

db = database.Database(
app.config['MONGO_HOST'],
app.config['MONGO_HOSTS'],
app.config['MONGO_PORT'],
app.config['DATABASE_NAME']
)
Expand Down
2 changes: 1 addition & 1 deletion backdrop/admin/config/development.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
SECRET_KEY = "something unique and secret"

DATABASE_NAME = "backdrop"
MONGO_HOST = 'localhost'
MONGO_HOSTS = ['localhost']
MONGO_PORT = 27017

try:
Expand Down
2 changes: 1 addition & 1 deletion backdrop/admin/config/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
SECRET_KEY = "something unique and secret"

DATABASE_NAME = "backdrop_test"
MONGO_HOST = 'localhost'
MONGO_HOSTS = ['localhost']
MONGO_PORT = 27017

from test_environment import *
17 changes: 14 additions & 3 deletions backdrop/core/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,20 @@

class Database(object):

def __init__(self, host, port, name):
self._mongo = pymongo.MongoReplicaSetClient(host, port,
replicaSet='production')
def __init__(self, hosts, port, name):
"""
Create a list to feed to the MongoReplicaSetClient in a way it understands
e.g. MongoReplicaSetClient(
[u'hostname_one:27017', u'hostname_two:27017', u'hostname_three:27017'])
See http://api.mongodb.org/python/current/examples/high_availability.html#mongoreplicasetclient for more
"""

clientList = []
for host in hosts:
clientList.append('{0}:{1}'.format(host, port))

self._mongo = pymongo.MongoReplicaSetClient(
','.join(clientList), replicaSet='production')
self.name = name

def alive(self):
Expand Down
2 changes: 1 addition & 1 deletion backdrop/read/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"backdrop.read.config.{}".format(GOVUK_ENV))

db = database.Database(
app.config['MONGO_HOST'],
app.config['MONGO_HOSTS'],
app.config['MONGO_PORT'],
app.config['DATABASE_NAME']
)
Expand Down
2 changes: 1 addition & 1 deletion backdrop/read/config/development.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
DATABASE_NAME = "backdrop"
MONGO_HOST = 'localhost'
MONGO_HOSTS = ['localhost']
MONGO_PORT = 27017
LOG_LEVEL = "DEBUG"
RAW_QUERIES_ALLOWED = {
Expand Down
2 changes: 1 addition & 1 deletion backdrop/read/config/test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
DATABASE_NAME = "backdrop_test"
MONGO_HOST = 'localhost'
MONGO_HOSTS = ['localhost']
MONGO_PORT = 27017
LOG_LEVEL = "ERROR"
RAW_QUERIES_ALLOWED = {
Expand Down
2 changes: 1 addition & 1 deletion backdrop/write/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"backdrop.write.config.{}".format(GOVUK_ENV))

db = database.Database(
app.config['MONGO_HOST'],
app.config['MONGO_HOSTS'],
app.config['MONGO_PORT'],
app.config['DATABASE_NAME']
)
Expand Down
2 changes: 1 addition & 1 deletion backdrop/write/config/development.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
DATABASE_NAME = "backdrop"
MONGO_HOST = 'localhost'
MONGO_HOSTS = ['localhost']
MONGO_PORT = 27017
LOG_LEVEL = "DEBUG"
BUCKET_AUTO_ID_KEYS = {
Expand Down
2 changes: 1 addition & 1 deletion backdrop/write/config/test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
DATABASE_NAME = "backdrop_test"
MONGO_HOST = 'localhost'
MONGO_HOSTS = ['localhost']
MONGO_PORT = 27017
LOG_LEVEL = "DEBUG"
CLIENT_ID = "it's not important here"
Expand Down
3 changes: 2 additions & 1 deletion run_migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ def load_config(env):


def get_database(config):
return Database(config.MONGO_HOST, config.MONGO_PORT, config.DATABASE_NAME)
return Database(
config.MONGO_HOSTS, config.MONGO_PORT, config.DATABASE_NAME)


def get_migrations():
Expand Down
2 changes: 1 addition & 1 deletion tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def get_database():
"backdrop.write.config.%s" % environment()
)
return database.Database(
app.config['MONGO_HOST'],
app.config['MONGO_HOSTS'],
app.config['MONGO_PORT'],
app.config['DATABASE_NAME']
)
Expand Down
2 changes: 1 addition & 1 deletion tests/core/integration/test_bucket_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from backdrop.read.query import Query
from tests.support.test_helpers import d_tz

HOST = 'localhost'
HOST = ['localhost']
PORT = 27017
DB_NAME = 'performance_platform_test'
BUCKET = 'bucket_integration_test'
Expand Down
4 changes: 2 additions & 2 deletions tests/core/integration/test_database_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from backdrop.read.query import Query
from tests.support.test_helpers import d, d_tz

HOST = 'localhost'
HOST = ['localhost']
PORT = 27017
DB_NAME = 'performance_platform_test'
BUCKET = 'test_repository_integration'
Expand Down Expand Up @@ -762,7 +762,7 @@ def test_query_map(self):

class TestDatabase(unittest.TestCase):
def setUp(self):
self.db = Database('localhost', 27017, 'backdrop_test')
self.db = Database(HOST, PORT, DB_NAME)
self.db.mongo_database["my_capped_collection"].drop()

def test_alive(self):
Expand Down
2 changes: 1 addition & 1 deletion tests/core/integration/test_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from backdrop.core.repository import BucketConfigRepository, UserConfigRepository
from backdrop.core.user import UserConfig

HOST = 'localhost'
HOST = ['localhost']
PORT = 27017
DB_NAME = 'performance_platform_test'
BUCKET = 'buckets'
Expand Down

0 comments on commit daf87ec

Please sign in to comment.