From 5dfab4d9c5c5bc141eb66ea761efe7a7b51e1956 Mon Sep 17 00:00:00 2001
From: Inada Naoki <songofacandy@gmail.com>
Date: Fri, 19 May 2023 03:04:56 +0900
Subject: [PATCH] CI: Update Django test workflow (#610)

---
 .github/workflows/django.yaml | 32 ++++++++++++++++++++------------
 Makefile                      |  4 ++--
 ci/test_mysql.py              |  6 ++++--
 src/MySQLdb/cursors.py        | 10 +++++++---
 4 files changed, 33 insertions(+), 19 deletions(-)

diff --git a/.github/workflows/django.yaml b/.github/workflows/django.yaml
index 737f34be..a7e076ce 100644
--- a/.github/workflows/django.yaml
+++ b/.github/workflows/django.yaml
@@ -2,15 +2,22 @@ name: Django compat test
 
 on:
   push:
+  pull_request:
 
 jobs:
   build:
+    name: "Run Django LTS test suite"
     runs-on: ubuntu-latest
+    env:
+      PIP_NO_PYTHON_VERSION_WARNING: 1
+      PIP_DISABLE_PIP_VERSION_CHECK: 1
+      DJANGO_VERSION: "3.2.19"
     steps:
       - name: Start MySQL
         run: |
           sudo systemctl start mysql.service
           mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -uroot -proot mysql
+          mysql -uroot -proot -e "set global innodb_flush_log_at_trx_commit=0;"
           mysql -uroot -proot -e "CREATE USER 'scott'@'%' IDENTIFIED BY 'tiger'; GRANT ALL ON *.* TO scott;"
           mysql -uroot -proot -e "CREATE DATABASE django_default; CREATE DATABASE django_other;"
 
@@ -19,27 +26,28 @@ jobs:
       - name: Set up Python
         uses: actions/setup-python@v4
         with:
-          # https://www.mail-archive.com/django-updates@googlegroups.com/msg209056.html
-          python-version: "3.11"
+          # Django 3.2.9+ supports Python 3.10
+          # https://docs.djangoproject.com/ja/3.2/releases/3.2/
+          python-version: "3.10"
+          cache: "pip"
+          cache-dependency-path: "ci/django-requirements.txt"
 
       - name: Install mysqlclient
-        env:
-          PIP_NO_PYTHON_VERSION_WARNING: 1
-          PIP_DISABLE_PIP_VERSION_CHECK: 1
         run: |
-          pip install -r requirements.txt
+          #pip install -r requirements.txt
+          #pip install mysqlclient  # Use stable version
           pip install .
-          # pip install mysqlclient  # Use stable version
 
-      - name: Run Django test
-        env:
-          DJANGO_VERSION: "3.2.19"
+      - name: Setup Django
         run: |
           sudo apt-get install libmemcached-dev
           wget https://github.com/django/django/archive/${DJANGO_VERSION}.tar.gz
           tar xf ${DJANGO_VERSION}.tar.gz
           cp ci/test_mysql.py django-${DJANGO_VERSION}/tests/
+          cd django-${DJANGO_VERSION}
+          pip install . -r tests/requirements/py3.txt
+
+      - name: Run Django test
+        run: |
           cd django-${DJANGO_VERSION}/tests/
-          pip install ..
-          pip install -r requirements/py3.txt
           PYTHONPATH=.. python3 ./runtests.py --settings=test_mysql
diff --git a/Makefile b/Makefile
index f0e94c3b..bcd4334d 100644
--- a/Makefile
+++ b/Makefile
@@ -17,5 +17,5 @@ clean:
 
 .PHONY: check
 check:
-	ruff .
-	black *.py src
+	ruff *.py src ci
+	black *.py src ci
diff --git a/ci/test_mysql.py b/ci/test_mysql.py
index e285f4cf..9417fc9f 100644
--- a/ci/test_mysql.py
+++ b/ci/test_mysql.py
@@ -19,7 +19,7 @@
         "HOST": "127.0.0.1",
         "USER": "scott",
         "PASSWORD": "tiger",
-        "TEST": {"CHARSET": "utf8mb4", "COLLATION": "utf8mb4_general_ci"},
+        "TEST": {"CHARSET": "utf8mb3", "COLLATION": "utf8mb3_general_ci"},
     },
     "other": {
         "ENGINE": "django.db.backends.mysql",
@@ -27,7 +27,7 @@
         "HOST": "127.0.0.1",
         "USER": "scott",
         "PASSWORD": "tiger",
-        "TEST": {"CHARSET": "utf8mb4", "COLLATION": "utf8mb4_general_ci"},
+        "TEST": {"CHARSET": "utf8mb3", "COLLATION": "utf8mb3_general_ci"},
     },
 }
 
@@ -37,3 +37,5 @@
 PASSWORD_HASHERS = [
     "django.contrib.auth.hashers.MD5PasswordHasher",
 ]
+
+DEFAULT_AUTO_FIELD = "django.db.models.AutoField"
diff --git a/src/MySQLdb/cursors.py b/src/MySQLdb/cursors.py
index 785fa9a1..70fbeea4 100644
--- a/src/MySQLdb/cursors.py
+++ b/src/MySQLdb/cursors.py
@@ -66,7 +66,7 @@ def __init__(self, connection):
         self.connection = connection
         self.description = None
         self.description_flags = None
-        self.rowcount = -1
+        self.rowcount = 0
         self.arraysize = 1
         self._executed = None
 
@@ -78,8 +78,10 @@ def __init__(self, connection):
     def _discard(self):
         self.description = None
         self.description_flags = None
-        self.rowcount = -1
-        self.lastrowid = None
+        # Django uses some member after __exit__.
+        # So we keep rowcount and lastrowid here. They are cleared in Cursor._query().
+        # self.rowcount = 0
+        # self.lastrowid = None
         self._rows = None
         self.rownumber = None
 
@@ -323,6 +325,8 @@ def callproc(self, procname, args=()):
     def _query(self, q):
         db = self._get_db()
         self._result = None
+        self.rowcount = None
+        self.lastrowid = None
         db.query(q)
         self._do_get_result(db)
         self._post_get_result()