alias51
alias51

Reputation: 8648

How to exclude certain system checks from running on `manage.py migrate`?

I have created a series of checks using Django's System check framework.

Some of the checks are used to confirm that fixtures are set up correctly. For example, I have a check that confirms if all users have at least one group.

@register(Tag.database)
def check_users_have_group(app_configs, **kwargs):
    errors = []
    users = UserModel.objects.all()
    for user in users:
         if not user.groups.exists():
              message = f'{user} has no permission groups set.'
                    errors.append(
                        Error(
                            message,
                            obj='account',
                            id=f'check_user_{user.id}_permission_groups'
                        )
                    )
    return errors

Django's default is to run checks on migration. If I deploy the app without an existing database, then when I run migrate to set up the database the above check will cause a ProgrammingError because the table is not yet created:

django.db.utils.ProgrammingError: relation "accounts_account" does not exist

How can I exclude this test from running on python manage.py migrate? I want to run this after the migration is complete.

Upvotes: 0

Views: 613

Answers (1)

Gonçalo Peres
Gonçalo Peres

Reputation: 13592

Django's default is to run checks on migration

OP can configure it to not be so using the flag --skip-checks, like

django-admin migrate --skip-checks

As mentioned in the previous link

This option is only available if the requires_system_checks command attribute is not an empty list or tuple.

So, OP then has to configure the requires_system_checks to match what OP wants, because the default value is 'all'.

Since OP doesn't want to run Tags.database, don't include that one in the list. So OP will have something like

requires_system_checks = [Tags.staticfiles, Tags.models]

In light of the new specifications (that OP wants to run other database checks), let's get some more context.

Django's system checks are organized using specific built-in tags. Reading more about the database one, we see

database: Checks database-related configuration issues. Database checks are not run by default because they do more than static code analysis as regular checks do. They are only run by the migrate command or if you specify configured database aliases using the --database option when calling the check command.

This points to what Abdul Aziz Barkat mentions in the comment that

The System check framework is for static checks so I don't know if implementing your checks over there is the best place to do so. You might instead want to come up with a custom management command to do this checking.

In other words, it's advisable that, for such thing, one creates a custom command. In this other answer I explain how to do so.

Upvotes: 1

Related Questions