test(migrations): add migrations test . closes #746#753
test(migrations): add migrations test . closes #746#753regulartim merged 8 commits intointelowlproject:developfrom
Conversation
|
just realized that ci is currently not installing dev dependencies, so the django-test-migrations module isn’t found. Locally, with dev requirements installed, the tests pass fine. We might need to update the CI to include dev requirements when running tests or some other alternative.. |
|
I think I fixed the CI issue, but your tests are failing for some reason. Any idea why? |
|
Suggestion from LLM: This is a PostgreSQL-specific issue with django-test-migrations. The first test (TestRemoveHardcodedHoneypots) creates an IOC and adds it to a GeneralHoneypot via the M2M relation. That M2M operation creates deferred constraint triggers. When the second test's setUp calls migrator.apply_initial_migration, it tries to DROP/TRUNCATE the greedybear_ioc table, and PostgreSQL refuses because those triggers are still pending. The fix is to force PostgreSQL to resolve deferred constraints before migrator.reset() runs. In the MigrationTestCase.tearDown, execute SET CONSTRAINTS ALL IMMEDIATE before the reset: def tearDown(self):
from django.db import connection
with connection.cursor() as cursor:
cursor.execute("SET CONSTRAINTS ALL IMMEDIATE")
self.migrator.reset()
super().tearDown() |
Thanks for the suggestion! I will apply the following snippet in tearDown and push the changes. Hopefully, this resolves the CI issue. |
Now the CI also installs the dev requirements but your tests still don't pass. I tried them locally on my machine and they also fail. Do you know why? Do they still pass on your machine? |
I'm actively working on this and trying to solve it. I discovered that the issue seems to be related to test isolation in the DB level. When I run only the migration test file in isolation (e.g., python manage.py test tests.test_migrations), it passes successfully. I initially thought it would work in the full test suite as well, so I mentioned it passed, but in reality, it fails when running the entire suite due to some DB state pollution or pending constraint/trigger events from other tests. I'm investigating further to identify the exact cause, and if I find a fix or more details, I'll update you right away. |
|
Hey @drona-gyawali ! Thanks for your investigation.
You're not missing anything that is obvious to me at least. What are the options that we have now, in you opinion? |
Thank you very much for taking the time to review this PR. I think that the case we have in our project isn’t solvable purely at the programmatic level. Interfering with the schema or forcing PostgreSQL to ignore deferred constraints can lead to bigger problems and break other tests. My simple and maintainable solution is to isolate migration tests using tags:
This approach keeps the developer experience clean, avoids DB conflicts, and ensures that future schema changes won’t accidentally break normal tests. Additionally, this separation makes it easier to configure CI pipelines, because normal tests and migration tests can be run independently, keeping CI fast and reliable while still allowing migration tests to run when needed. As of now, this seems like the practical and safe solution given db deferred constraints behavior. and I really want to hear your thoughts on this approach. |
|
Thanks for the explanation! :) The problem I have with your approach is that the migration tests won't run by default, reducing their value as regression guards. But if you are convinced that there is no better option, feel free to implement it that way. |
I completely agree with your point. For now, we can run a separate CI job for these test cases as a regression guard if that work. I assure you, In the future, if I find a reliable solution, workaround, or any relevant update, I’ll implement it or notify you :) |
regulartim
left a comment
There was a problem hiding this comment.
Looks good to me! :) I also added the migration tests to the CI. @mlodic Is this fine for you too?

Description
This PR adds focused regression tests for two historical data migrations that modify existing rows and depend on historical database state:
0029_remove_hardcoded_honeypots
0030_migrate_cowrie_log4j
These migrations currently run without errors but could silently break if related models, constraints, or relationships are changed in the future.
Please include a summary of the change.
Related issues
closes #746
Checklist
develop.Black,Flake,Isort) gave 0 errors. If you have correctly installed pre-commit, it does these checks and adjustments on your behalf.Important Rules