blob: b8dad9c7b003485c43fcdda44e9bcbf733b8a971 [file] [log] [blame]
Jakob Juelich7bef8412014-10-14 19:11:54 -07001# Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""This files contains helper methods to interact with the readonly database."""
6
7# Please note this file doesn't contain and should not contain any logic to
8# establish connections outside of Django, as that might lead to effects where
9# connections get leaked, which will lead to Django not cleaning them up
10# properly. See http://crbug.com/422637 for more details on this failure.
11
Eric Lib31e2a42011-05-24 11:08:12 -070012from django import db as django_db
showard09096d82008-07-07 23:20:49 +000013
Jakob Juelich7bef8412014-10-14 19:11:54 -070014_DISABLED = False
15
16def set_globally_disabled(disable):
17 """Disable and enable the use of readonly connections globally.
18
19 If disabled, connection() will return the global connection instead of the
20 readonly connection.
21
22 @param disable: When True, readonly connections will be disabled.
showard09096d82008-07-07 23:20:49 +000023 """
Jakob Juelich7bef8412014-10-14 19:11:54 -070024 _DISABLED = disable
showard56e93772008-10-06 10:06:22 +000025
26
showard56e93772008-10-06 10:06:22 +000027def connection():
Jakob Juelich7bef8412014-10-14 19:11:54 -070028 """Return a readonly database connection."""
29 if _DISABLED:
30 return django_db.connections['global']
31 return django_db.connections['readonly']
showard56e93772008-10-06 10:06:22 +000032
33
Jakob Juelich7bef8412014-10-14 19:11:54 -070034def cursor():
35 """Return a cursor on the readonly database connection."""
36 return connection().cursor()