Skip to content

Commit

Permalink
Merge pull request #75 from callat-qcd/issue-74-migration-checks
Browse files Browse the repository at this point in the history
Issue 74 migration checks
  • Loading branch information
ckoerber committed Jul 27, 2020
2 parents 1d40500 + 26d4317 commit 784dc2d
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 12 deletions.
19 changes: 12 additions & 7 deletions doc-src/features/init-checks.rst
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,18 @@ The check function :func:`espressodb.management.checks.run_all_checks` is called
This function is run whenever you import your project.

EspressoDB projects before version 1.1.0 do not contain the above ``if`` statement.
Copy these lines in your project's ``_init()`` function to allow automated checks.
.. note::
The method :func:`~espressodb.management.checks.run_all_checks` wraps :func:`~espressodb.management.checks.migrations.check_model_state` and :func:`~espressodb.management.checks.migrations.check_migration_state`.
If you do not want to check the state of migrations all apps, you can provide a list of apps to exclude to :func:`~espressodb.management.checks.migrations.check_migration_state`.

.. warning::
EspressoDB projects before version 1.1.0 do not contain the above ``if`` statement.
Copy these lines in your project's ``_init()`` function to allow automated checks.

This ``_init()`` function is the place where you can globally change the default behavior for checks.
For example, to turn on checks if the ``ESPRESSODB_INIT_CHECKS`` is not set, change the ``if`` condition to
This ``_init()`` function is the place where you can globally change the default behavior for checks.
For example, to turn on checks if the ``ESPRESSODB_INIT_CHECKS`` is not set, change the ``if`` condition to

.. code ::
.. code ::
if os.environ.get("ESPRESSODB_INIT_CHECKS", "1") != "0":
...
if os.environ.get("ESPRESSODB_INIT_CHECKS", "1") != "0":
...
12 changes: 11 additions & 1 deletion espressodb/management/checks/migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,23 @@ def check_model_state():
raise MigrationStateError(f"Migrations have changed", changes)


def check_migration_state():
def check_migration_state(exclude: Optional[List[str]] = None):
"""Checks if the state of local migrations is represented by the database.
This code follows the logic of Djangos showmigrations
https://github.com/django/django/blob/master/django/core/management/commands/showmigrations.py
Arguments:
exclude: List of apps to ignore when checking migration states.
Raises:
MigrationStateError: If the loader detects conflicts or unapplied changes.
Future:
It might be desirable to allow partial checks by, e.g., providing an app_labels
argument.
"""
exclude = exclude or []
try:
connection = connections[DEFAULT_DB_ALIAS]
loader = MigrationLoader(connection, ignore_no_migrations=True)
Expand All @@ -120,6 +124,12 @@ def check_migration_state():
f"Error when checking state of migrations conflicts:\n{error}"
)

if exclude:
applied_migrations = set(
[el for el in applied_migrations if el[0] not in exclude]
)
plan = set([el for el in plan if el[0] not in exclude])

if applied_migrations != plan:
data = {
"The DB is ahead of your tables by": applied_migrations.difference(plan),
Expand Down
2 changes: 1 addition & 1 deletion tests/migration_tests/tests/test_state1.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def test_01_check_model_state():
def test_02_migration_state():
"""Checks if all migrations agree with database state
"""
check_migration_state()
check_migration_state(exclude=["auth"])


if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion tests/migration_tests/tests/test_state2.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def test_01_check_model_state(self):
def test_02_migration_state():
"""Checks if all migrations agree with database state
"""
check_migration_state()
check_migration_state(exclude=["auth"])


if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion tests/migration_tests/tests/test_state3.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def test_02_migration_state(self):
"""Checks if all migrations agree with database state
"""
with self.assertRaises(MigrationStateError):
check_migration_state()
check_migration_state(exclude=["auth"])


if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion tests/migration_tests/tests/test_state4.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def test_02_migration_state(self):
"""Checks if all migrations agree with database state
"""
with self.assertRaises(MigrationStateError):
check_migration_state()
check_migration_state(exclude=["auth"])


if __name__ == "__main__":
Expand Down

0 comments on commit 784dc2d

Please sign in to comment.