Jakob Juelich | 7bef841 | 2014-10-14 19:11:54 -0700 | [diff] [blame] | 1 | # 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 Li | b31e2a4 | 2011-05-24 11:08:12 -0700 | [diff] [blame] | 12 | from django import db as django_db |
showard | 09096d8 | 2008-07-07 23:20:49 +0000 | [diff] [blame] | 13 | |
Jakob Juelich | 7bef841 | 2014-10-14 19:11:54 -0700 | [diff] [blame] | 14 | _DISABLED = False |
| 15 | |
| 16 | def 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. |
showard | 09096d8 | 2008-07-07 23:20:49 +0000 | [diff] [blame] | 23 | """ |
Jakob Juelich | 7bef841 | 2014-10-14 19:11:54 -0700 | [diff] [blame] | 24 | _DISABLED = disable |
showard | 56e9377 | 2008-10-06 10:06:22 +0000 | [diff] [blame] | 25 | |
| 26 | |
showard | 56e9377 | 2008-10-06 10:06:22 +0000 | [diff] [blame] | 27 | def connection(): |
Jakob Juelich | 7bef841 | 2014-10-14 19:11:54 -0700 | [diff] [blame] | 28 | """Return a readonly database connection.""" |
| 29 | if _DISABLED: |
| 30 | return django_db.connections['global'] |
| 31 | return django_db.connections['readonly'] |
showard | 56e9377 | 2008-10-06 10:06:22 +0000 | [diff] [blame] | 32 | |
| 33 | |
Jakob Juelich | 7bef841 | 2014-10-14 19:11:54 -0700 | [diff] [blame] | 34 | def cursor(): |
| 35 | """Return a cursor on the readonly database connection.""" |
| 36 | return connection().cursor() |