Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Fix errors in migrations database compatibility workflow (#9996)

GrzegorzKowalikb4e9289d22b5
diff --git a/.github/workflows/test-migrations-compatibility.yml b/.github/workflows/test-migrations-compatibility.yml
index 2f8806abac..28bc79b87e 100644
--- a/.github/workflows/test-migrations-compatibility.yml
+++ b/.github/workflows/test-migrations-compatibility.yml
@@ -51,6 +51,7 @@ jobs:
- name: Migrate
run: |
+ export DJANGO_SETTINGS_MODULE=saleor.tests.settings
./manage.py migrate
- name: Checkout base
diff --git a/conftest.py b/conftest.py
index 9bd0deb896..56b6a8add6 100644
--- a/conftest.py
+++ b/conftest.py
@@ -1,8 +1,12 @@
import os
import dj_database_url
+import django.test
import pytest
from django.conf import settings
+from django.core.management import call_command
+from django.db import connections
+from django.test.testcases import TransactionTestCase
pytest_plugins = [
"saleor.tests.fixtures",
@@ -25,3 +29,31 @@ if os.environ.get("PYTEST_DB_URL"):
env="PYTEST_DB_URL", conn_max_age=600
),
}
+
+ # In case transactional tests are run against a DB that has additional models
+ # defined, test cleanup can fail when Django sends TRUNCATE queries for all tables
+ # used during tests. It has no idea about this additional model, so it's not
+ # included in this query. If this model references any table that is being
+ # truncated, query will fail with an error:
+ # django.db.utils.NotSupportedError: cannot truncate a table referenced in a
+ # foreign key constraint
+ # DETAIL: Table "new_table" references "existing_table".
+ # HINT: Truncate table "new_table" at the same time, or use TRUNCATE ... CASCADE
+ class Custom(TransactionTestCase):
+ def _fixture_teardown(self):
+ for db_name in self._databases_names(include_mirrors=False):
+ inhibit_post_migrate = self.available_apps is not None or (
+ self.serialized_rollback
+ and hasattr(connections[db_name], "_test_serialized_contents")
+ )
+ call_command(
+ "flush",
+ verbosity=0,
+ interactive=False,
+ database=db_name,
+ reset_sequences=False,
+ allow_cascade=True,
+ inhibit_post_migrate=inhibit_post_migrate,
+ )
+
+ django.test.TransactionTestCase = Custom # type:ignore[misc]
diff --git a/saleor/tests/migrations/0001_initial.py b/saleor/tests/migrations/0001_initial.py
new file mode 100644
index 0000000000..d2bec9d44f
--- /dev/null
+++ b/saleor/tests/migrations/0001_initial.py
@@ -0,0 +1,28 @@
+# Generated by Django 3.0.6 on 2020-05-12 12:10
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ initial = True
+
+ dependencies = []
+
+ operations = [
+ migrations.CreateModel(
+ name="Book",
+ fields=[
+ (
+ "id",
+ models.AutoField(
+ auto_created=True,
+ primary_key=True,
+ serialize=False,
+ verbose_name="ID",
+ ),
+ ),
+ ("name", models.CharField(max_length=30)),
+ ],
+ ),
+ ]
diff --git a/saleor/tests/migrations/__init__.py b/saleor/tests/migrations/__init__.py
new file mode 100644
index 0000000000..e69de29bb2