Repository: adamchainz/django-mysql

Issue:
Fix name consistency
In all the django multiple database code, the user says which database to use with an argument `using`, whilst `BaseStatus` and `Lock` in django-mysql currently use the lengthy `connection_name`. To reduce cognitive load, renaming them to `using` would be great.

Also they don't have explicit multiple database tests but they should.



Discussion:

Resolving patch:
diff --git a/django_mysql/locks.py b/django_mysql/locks.py
--- a/django_mysql/locks.py
+++ b/django_mysql/locks.py
@@ -6,21 +6,23 @@
 
 
 class Lock(object):
-    def __init__(self, name, acquire_timeout=10.0, connection_name=None):
+    def __init__(self, name, acquire_timeout=10.0, using=None):
         self.acquire_timeout = acquire_timeout
-        if connection_name is None:
-            connection_name = DEFAULT_DB_ALIAS
-        self.connection_name = connection_name
+
+        if using is None:
+            self.db = DEFAULT_DB_ALIAS
+        else:
+            self.db = using
 
         # For multi-database servers, we prefix the name of the lock wth
         # the database, to protect against concurrent apps with the same locks
         self.name = '.'.join((
-            connections[self.connection_name].settings_dict['NAME'],
+            connections[self.db].settings_dict['NAME'],
             name
         ))
 
     def get_cursor(self):
-        return connections[self.connection_name].cursor()
+        return connections[self.db].cursor()
 
     def __enter__(self):
         with self.get_cursor() as cursor:
diff --git a/django_mysql/status.py b/django_mysql/status.py
--- a/django_mysql/status.py
+++ b/django_mysql/status.py
@@ -16,13 +16,14 @@ class BaseStatus(object):
     """
     query = ""
 
-    def __init__(self, connection_name=None):
-        if connection_name is None:
-            connection_name = DEFAULT_DB_ALIAS
-        self.connection_name = connection_name
+    def __init__(self, using=None):
+        if using is None:
+            self.db = DEFAULT_DB_ALIAS
+        else:
+            self.db = using
 
     def get_cursor(self):
-        return connections[self.connection_name].cursor()
+        return connections[self.db].cursor()
 
     def get(self, name):
         if '%' in name:


Test patch:
diff --git a/tests/django_mysql_tests/test_locks.py b/tests/django_mysql_tests/test_locks.py
--- a/tests/django_mysql_tests/test_locks.py
+++ b/tests/django_mysql_tests/test_locks.py
@@ -133,13 +133,23 @@ def check_it_lock_it():
             pass
 
     @expectedFailure
-    def test_something(self):
+    def test_holding_more_than_one(self):
         """
         Only MySQL 5.7 gives the ability to hold more than one named lock, so
         this is an expectedFailure until then.
 
         N.B. MariaDB 10.0.2+ already has the patch.
         """
-        with Lock("a"):
-            with Lock("b"):
-                pass
+        lock_a = Lock("a")
+        lock_b = Lock("b")
+        with lock_a, lock_b:
+            self.assertTrue(lock_a.is_held())
+
+    def test_multi_connection(self):
+        lock_a = Lock("a")
+        lock_b = Lock("b", using='secondary')
+
+        with lock_a, lock_b:
+            # Different connections = can hold > 1!
+            self.assertTrue(lock_a.is_held())
+            self.assertTrue(lock_b.is_held())
diff --git a/tests/django_mysql_tests/test_status.py b/tests/django_mysql_tests/test_status.py
--- a/tests/django_mysql_tests/test_status.py
+++ b/tests/django_mysql_tests/test_status.py
@@ -98,6 +98,13 @@ def test_wait_until_load_low(self):
         self.assertNotIn('Threads_running', message)
         self.assertNotIn('1000000', message)
 
+    def test_other_databases(self):
+        status = GlobalStatus(using='secondary')
+
+        running = status.get('Threads_running')
+        self.assertTrue(isinstance(running, int))
+        self.assertGreaterEqual(running, 1)
+
 
 class SessionStatusTests(TestCase):