xref: /aosp_15_r20/external/autotest/frontend/afe/readonly_connection.py (revision 9c5db1993ded3edbeafc8092d69fe5de2ee02df7)
1*9c5db199SXin Li# Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
2*9c5db199SXin Li# Use of this source code is governed by a BSD-style license that can be
3*9c5db199SXin Li# found in the LICENSE file.
4*9c5db199SXin Li
5*9c5db199SXin Li"""This files contains helper methods to interact with the readonly database."""
6*9c5db199SXin Li
7*9c5db199SXin Li# Please note this file doesn't contain and should not contain any logic to
8*9c5db199SXin Li# establish connections outside of Django, as that might lead to effects where
9*9c5db199SXin Li# connections get leaked, which will lead to Django not cleaning them up
10*9c5db199SXin Li# properly. See http://crbug.com/422637 for more details on this failure.
11*9c5db199SXin Li
12*9c5db199SXin Lifrom django import db as django_db
13*9c5db199SXin Li
14*9c5db199SXin Li_DISABLED = False
15*9c5db199SXin Li
16*9c5db199SXin Lidef set_globally_disabled(disable):
17*9c5db199SXin Li    """Disable and enable the use of readonly connections globally.
18*9c5db199SXin Li
19*9c5db199SXin Li    If disabled, connection() will return the global connection instead of the
20*9c5db199SXin Li    readonly connection.
21*9c5db199SXin Li
22*9c5db199SXin Li    @param disable: When True, readonly connections will be disabled.
23*9c5db199SXin Li    """
24*9c5db199SXin Li    _DISABLED = disable
25*9c5db199SXin Li
26*9c5db199SXin Li
27*9c5db199SXin Lidef connection():
28*9c5db199SXin Li    """Return a readonly database connection."""
29*9c5db199SXin Li    if _DISABLED:
30*9c5db199SXin Li        return django_db.connections['global']
31*9c5db199SXin Li    return django_db.connections['readonly']
32*9c5db199SXin Li
33*9c5db199SXin Li
34*9c5db199SXin Lidef cursor():
35*9c5db199SXin Li    """Return a cursor on the readonly database connection."""
36*9c5db199SXin Li    return connection().cursor()
37