Skip to content

Commit a1ae34e

Browse files
fopinagsilvapt
andauthored
Add django.db.backends.postgresql to postgresql ENGINE matching (#7)
* fix workflows: style, bump to py37, tox config, pin DB requirements * add test to cover issue with NoTable * return empty table list for DB engines that are not supported (instead of blowing up) * update postgresql engine match condition to include `django.db.backends.postgresql` * Update testapp/tests/test_admin.py Co-authored-by: Gustavo Silva <gustavosantaremsilva@gmail.com> Signed-off-by: Filipe Pina <636320+fopina@users.noreply.github.com> --------- Signed-off-by: Filipe Pina <636320+fopina@users.noreply.github.com> Co-authored-by: Gustavo Silva <gustavosantaremsilva@gmail.com>
1 parent f35239a commit a1ae34e

File tree

10 files changed

+46
-17
lines changed

10 files changed

+46
-17
lines changed

.github/workflows/test.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ jobs:
1414
runs-on: ubuntu-latest
1515
steps:
1616
- uses: actions/checkout@v2
17-
- name: Set up Python 3.6
17+
- name: Set up Python 3.7
1818
uses: actions/setup-python@v2
1919
with:
20-
python-version: 3.6
20+
python-version: 3.7
2121
- name: Install tox
2222
run: pip install tox
2323
- name: Style check
@@ -27,20 +27,20 @@ jobs:
2727
runs-on: ubuntu-latest
2828
strategy:
2929
matrix:
30-
python-version: [3.6, 3.7, 3.8, 3.9]
30+
python-version: [3.7, 3.8, 3.9]
3131
database: [sqlite, mysql, postgresql]
3232

3333
services:
3434
mysql:
35-
image: mysql:5
35+
image: mysql:8
3636
env:
3737
MYSQL_ROOT_PASSWORD: root
3838
ports:
3939
- 8877:3306
4040
# needed because the container does not provide a healthcheck
4141
options: --health-cmd "mysqladmin ping" --health-interval 10s --health-timeout 5s --health-retries=5
4242
postgres:
43-
image: postgres:10
43+
image: postgres:12-alpine
4444
env:
4545
POSTGRES_USER: postgres
4646
POSTGRES_PASSWORD: postgres

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ __pycache__
88
.pytest_cache
99
.vscode
1010
/.tox
11+
*.sqlite3

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ TEST_CONTAINER := django-dbcleanup-test
33

44
.PHONY: style
55
style:
6-
black --target-version=py36 \
6+
black --target-version=py37 \
77
--line-length=120 \
88
--skip-string-normalization \
99
dbcleanup testapp setup.py
1010

1111
.PHONY: style_check
1212
style_check:
13-
black --target-version=py36 \
13+
black --target-version=py37 \
1414
--line-length=120 \
1515
--skip-string-normalization \
1616
--check \
@@ -40,7 +40,7 @@ startpg:
4040
--health-interval 10s \
4141
--health-timeout 5s \
4242
--health-retries 5 \
43-
postgres:10
43+
postgres:11-alpine
4444
until [ "`docker inspect -f {{.State.Health.Status}} ${TEST_CONTAINER}-pg`" == "healthy" ]; do sleep 0.1; done;
4545

4646
testpg: startpg

dbcleanup/models.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ def __str__(self) -> str:
4040

4141
return MySQLTable
4242

43-
if settings.DATABASES['default']['ENGINE'] == 'django.db.backends.postgresql_psycopg2':
43+
if settings.DATABASES['default']['ENGINE'] in (
44+
'django.db.backends.postgresql_psycopg2',
45+
'django.db.backends.postgresql',
46+
):
4447
"""
4548
based on https://wiki.postgresql.org/wiki/Disk_Usage
4649
@@ -103,14 +106,21 @@ def __str__(self) -> str:
103106

104107
return PGTable
105108

109+
class TableManager(models.Manager):
110+
def get_queryset(self):
111+
return super().get_queryset().none()
112+
106113
class NoTable(models.Model):
107114
"""
108115
bogus to allow projects to run with unsupported DB engines
109116
(but without any functionality from this app)
110117
"""
111118

119+
objects = TableManager()
120+
112121
name = models.CharField(max_length=64, primary_key=True)
113122
rows = models.PositiveBigIntegerField(null=True)
123+
size = models.IntegerField(default=0)
114124

115125
class Meta:
116126
managed = False

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ include_package_data = True
2828
packages = find:
2929
install_requires =
3030
Django >= 3.0
31-
python_requires = >=3.6
31+
python_requires = >=3.7
3232

3333
[options.packages.find]
3434
exclude =

testapp/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
pytest==6.2.5
33
pytest-cov==2.12.1
44
pytest-django==4.4.0
5-
black==20.8b1
5+
black>=22.0
66
mysqlclient==2.0.3
77
psycopg2-binary==2.9.2

testapp/testapp/migrations/0001_initial.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66

77
class Migration(migrations.Migration):
8-
98
initial = True
109

1110
dependencies = []

testapp/testapp/migrations/0002_auto_20211014_1519.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55

66
class Migration(migrations.Migration):
7-
87
dependencies = [
98
('testapp', '0001_initial'),
109
]

testapp/tests/test_admin.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from django.test import TestCase
2+
from django.contrib.auth import get_user_model
3+
from django.urls import reverse
4+
5+
6+
class Test(TestCase):
7+
def setUp(self):
8+
self.user = get_user_model().objects.create_user('tester', 'tester@test.it', 'tester')
9+
10+
def test_changelist(self):
11+
"""
12+
test to make sure that every DB engine allows to *at least* list tables
13+
"""
14+
self.user.is_staff = True
15+
self.user.is_superuser = True
16+
self.user.save()
17+
self.client.force_login(self.user)
18+
19+
r = self.client.get(reverse('admin:dbcleanup_table_changelist'))
20+
self.assertEqual(r.status_code, 200)

tox.ini

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ deps =
88
dj22: Django==2.2.*
99
dj30: Django==3.0.*
1010
dj32: Django==3.2.*
11-
postgresql: psycopg2-binary
12-
mysql: mysqlclient
11+
postgresql: psycopg2-binary==2.9.5
12+
mysql: mysqlclient==2.1.1
1313
coverage
1414
setenv =
1515
PYTHONPATH = {toxinidir}
1616
sqlite: DJANGO_SETTINGS_MODULE = testapp.settings
1717
postgresql: DJANGO_SETTINGS_MODULE = testapp.settings_postgresql
1818
mysql: DJANGO_SETTINGS_MODULE = testapp.settings_mysql
19-
whitelist_externals = make
19+
allowlist_externals = make
2020
pip_pre = True
2121
commands = make coverage TEST_ARGS='{posargs:tests}'
2222

@@ -30,6 +30,6 @@ skip_install = true
3030
basepython = python3
3131
commands = make style_check
3232
deps =
33-
black>=19.10b0
33+
black>=22.0
3434
flake8
3535
skip_install = true

0 commit comments

Comments
 (0)