From a488d533a660c49752d768c8d0b5e463e0fd46db Mon Sep 17 00:00:00 2001 From: Filipe Pina Date: Mon, 6 Feb 2023 00:06:36 +0000 Subject: [PATCH 1/5] fix workflows: style, bump to py37, tox config, pin DB requirements --- .github/workflows/test.yml | 10 +++++----- Makefile | 6 +++--- setup.cfg | 2 +- testapp/requirements.txt | 2 +- testapp/testapp/migrations/0001_initial.py | 1 - testapp/testapp/migrations/0002_auto_20211014_1519.py | 1 - tox.ini | 8 ++++---- 7 files changed, 14 insertions(+), 16 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 4548505..fc8b7ff 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -14,10 +14,10 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - - name: Set up Python 3.6 + - name: Set up Python 3.7 uses: actions/setup-python@v2 with: - python-version: 3.6 + python-version: 3.7 - name: Install tox run: pip install tox - name: Style check @@ -27,12 +27,12 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - python-version: [3.6, 3.7, 3.8, 3.9] + python-version: [3.7, 3.8, 3.9] database: [sqlite, mysql, postgresql] services: mysql: - image: mysql:5 + image: mysql:8 env: MYSQL_ROOT_PASSWORD: root ports: @@ -40,7 +40,7 @@ jobs: # needed because the container does not provide a healthcheck options: --health-cmd "mysqladmin ping" --health-interval 10s --health-timeout 5s --health-retries=5 postgres: - image: postgres:10 + image: postgres:12-alpine env: POSTGRES_USER: postgres POSTGRES_PASSWORD: postgres diff --git a/Makefile b/Makefile index f99a33d..9fe513a 100644 --- a/Makefile +++ b/Makefile @@ -3,14 +3,14 @@ TEST_CONTAINER := django-dbcleanup-test .PHONY: style style: - black --target-version=py36 \ + black --target-version=py37 \ --line-length=120 \ --skip-string-normalization \ dbcleanup testapp setup.py .PHONY: style_check style_check: - black --target-version=py36 \ + black --target-version=py37 \ --line-length=120 \ --skip-string-normalization \ --check \ @@ -40,7 +40,7 @@ startpg: --health-interval 10s \ --health-timeout 5s \ --health-retries 5 \ - postgres:10 + postgres:11-alpine until [ "`docker inspect -f {{.State.Health.Status}} ${TEST_CONTAINER}-pg`" == "healthy" ]; do sleep 0.1; done; testpg: startpg diff --git a/setup.cfg b/setup.cfg index 9469954..307a9fd 100644 --- a/setup.cfg +++ b/setup.cfg @@ -28,7 +28,7 @@ include_package_data = True packages = find: install_requires = Django >= 3.0 -python_requires = >=3.6 +python_requires = >=3.7 [options.packages.find] exclude = diff --git a/testapp/requirements.txt b/testapp/requirements.txt index 8cf168f..5361e25 100644 --- a/testapp/requirements.txt +++ b/testapp/requirements.txt @@ -2,6 +2,6 @@ pytest==6.2.5 pytest-cov==2.12.1 pytest-django==4.4.0 -black==20.8b1 +black>=22.0 mysqlclient==2.0.3 psycopg2-binary==2.9.2 diff --git a/testapp/testapp/migrations/0001_initial.py b/testapp/testapp/migrations/0001_initial.py index 93ef7f1..b0e734a 100644 --- a/testapp/testapp/migrations/0001_initial.py +++ b/testapp/testapp/migrations/0001_initial.py @@ -5,7 +5,6 @@ class Migration(migrations.Migration): - initial = True dependencies = [] diff --git a/testapp/testapp/migrations/0002_auto_20211014_1519.py b/testapp/testapp/migrations/0002_auto_20211014_1519.py index 7faa06b..41d530f 100644 --- a/testapp/testapp/migrations/0002_auto_20211014_1519.py +++ b/testapp/testapp/migrations/0002_auto_20211014_1519.py @@ -4,7 +4,6 @@ class Migration(migrations.Migration): - dependencies = [ ('testapp', '0001_initial'), ] diff --git a/tox.ini b/tox.ini index 3354d02..c9ee253 100644 --- a/tox.ini +++ b/tox.ini @@ -8,15 +8,15 @@ deps = dj22: Django==2.2.* dj30: Django==3.0.* dj32: Django==3.2.* - postgresql: psycopg2-binary - mysql: mysqlclient + postgresql: psycopg2-binary==2.9.5 + mysql: mysqlclient==2.1.1 coverage setenv = PYTHONPATH = {toxinidir} sqlite: DJANGO_SETTINGS_MODULE = testapp.settings postgresql: DJANGO_SETTINGS_MODULE = testapp.settings_postgresql mysql: DJANGO_SETTINGS_MODULE = testapp.settings_mysql -whitelist_externals = make +allowlist_externals = make pip_pre = True commands = make coverage TEST_ARGS='{posargs:tests}' @@ -30,6 +30,6 @@ skip_install = true basepython = python3 commands = make style_check deps = - black>=19.10b0 + black>=22.0 flake8 skip_install = true From 8d6a498a763b25d655d25052d3bad8b4aef36164 Mon Sep 17 00:00:00 2001 From: Filipe Pina Date: Mon, 6 Feb 2023 00:18:21 +0000 Subject: [PATCH 2/5] add test to cover issue with NoTable --- .gitignore | 1 + testapp/tests/test_admin.py | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+) create mode 100644 testapp/tests/test_admin.py diff --git a/.gitignore b/.gitignore index ac476a2..276b401 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ __pycache__ .pytest_cache .vscode /.tox +*.sqlite3 diff --git a/testapp/tests/test_admin.py b/testapp/tests/test_admin.py new file mode 100644 index 0000000..b563906 --- /dev/null +++ b/testapp/tests/test_admin.py @@ -0,0 +1,20 @@ +from django.test import TestCase +from django.contrib.auth import get_user_model +from django.urls import reverse + + +class Test(TestCase): + def setUp(self): + self.user = get_user_model().objects.create_user('tester', 'tester@ppb.it', 'tester') + + def test_changelist(self): + """ + test to make sure that every DB engine allows to *at least* list tables + """ + self.user.is_staff = True + self.user.is_superuser = True + self.user.save() + self.client.force_login(self.user) + + r = self.client.get(reverse('admin:dbcleanup_table_changelist')) + self.assertEqual(r.status_code, 200) From 3f98808610b9071075efbe7d033d7333ff20eaab Mon Sep 17 00:00:00 2001 From: Filipe Pina Date: Mon, 6 Feb 2023 00:27:59 +0000 Subject: [PATCH 3/5] return empty table list for DB engines that are not supported (instead of blowing up) --- dbcleanup/models.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/dbcleanup/models.py b/dbcleanup/models.py index 52c8f96..22863fd 100644 --- a/dbcleanup/models.py +++ b/dbcleanup/models.py @@ -103,14 +103,21 @@ def __str__(self) -> str: return PGTable + class TableManager(models.Manager): + def get_queryset(self): + return super().get_queryset().none() + class NoTable(models.Model): """ bogus to allow projects to run with unsupported DB engines (but without any functionality from this app) """ + objects = TableManager() + name = models.CharField(max_length=64, primary_key=True) rows = models.PositiveBigIntegerField(null=True) + size = models.IntegerField(default=0) class Meta: managed = False From fcaa68aa47fc711b49d68a7c2c3dde7de76d7400 Mon Sep 17 00:00:00 2001 From: Filipe Pina Date: Sun, 5 Feb 2023 18:18:54 +0000 Subject: [PATCH 4/5] update postgresql engine match condition to include `django.db.backends.postgresql` --- dbcleanup/models.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/dbcleanup/models.py b/dbcleanup/models.py index 22863fd..0d0b34e 100644 --- a/dbcleanup/models.py +++ b/dbcleanup/models.py @@ -40,7 +40,10 @@ def __str__(self) -> str: return MySQLTable - if settings.DATABASES['default']['ENGINE'] == 'django.db.backends.postgresql_psycopg2': + if settings.DATABASES['default']['ENGINE'] in ( + 'django.db.backends.postgresql_psycopg2', + 'django.db.backends.postgresql', + ): """ based on https://wiki.postgresql.org/wiki/Disk_Usage From c14649ced7670861ae5a831fb59d08532e0242c7 Mon Sep 17 00:00:00 2001 From: Filipe Pina <636320+fopina@users.noreply.github.com> Date: Mon, 6 Feb 2023 08:06:06 +0000 Subject: [PATCH 5/5] Update testapp/tests/test_admin.py Co-authored-by: Gustavo Silva Signed-off-by: Filipe Pina <636320+fopina@users.noreply.github.com> --- testapp/tests/test_admin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/testapp/tests/test_admin.py b/testapp/tests/test_admin.py index b563906..8354446 100644 --- a/testapp/tests/test_admin.py +++ b/testapp/tests/test_admin.py @@ -5,7 +5,7 @@ class Test(TestCase): def setUp(self): - self.user = get_user_model().objects.create_user('tester', 'tester@ppb.it', 'tester') + self.user = get_user_model().objects.create_user('tester', 'tester@test.it', 'tester') def test_changelist(self): """