django-commons/django-cookie-consent

Missing migration 0003_alter_cookie_id_alter_cookiegroup_id_and_more

Closed this issue · 1 comments

I ran into a problem while trying to convert a site over to docker. I discovered that there is a missing migration 0003.

It would seem that running migrate on a new install will cause an error about the missing file. In a dev or local situation in which it's being run by a privileged user, it's fairly easy to solve by running makemigrations once, which will generate the missing file.

But, in docker, it's common to switch to a non-privileged user before running migrations, and it's generally discouraged from running makemigrations on prod servers. Attempting to run makemigrations as a non-privileged user causes an error when attempting to write out the missing migration file:

PermissionError: [Errno 13] Permission denied: '/usr/local/lib/python3.9/site-packages/cookie_consent/migrations/0003_alter_cookie_id_alter_cookiegroup_id_and_more.py'

So, ideally the migration file needs to be added to the package before that point. It looks like there is a pending PR for this very thing, which is quite convenient. I'm not entirely sure what the process is for approving PR's and getting those changes published out, but I'm willing to learn and help out where able.

In the mean time, my temporary fix which might work for others that are dealing with similar issues, is I just chowned the migrations directory and ran makemigrations. So the end of the dockerfile looks like:

# hack to fix cookie toolbar
RUN chown -R appuser:appuser /usr/local/lib/python3.9/site-packages/cookie_consent/migrations

# change to the app user
USER appuser

# run entrypoint.prod.sh
ENTRYPOINT ["/home/appuser/web/entrypoint.sh"]

and these two lines in entrypoint.sh:

python manage.py makemigrations
python manage.py migrate

This is related to Django's DEFAULT_AUTO_FIELD, which in your project is likely set to something else than "django.db.models.AutoField" and it is affecting cookie-consent because we don't specify it on the app-level.

This setting was added in Django 3.2. I'm working on Django 4.1 and 4.2 support and part of that is specifying this setting at the app config level which will prevent the "missing migration" situation.

Due to publishing issues you will have to install the updated package version from Github rather than PyPI as it will probably take some time for it to be available there :(

There is a workaround available though! You can specify your own app config and wire that up instead of the default one:

# put this in `yourproject/cookie_consent_config.py`
from cookie_consent.apps import CookieConsentConf

class MyCookieConsentConf(CookieConsentConf):
    default_auto_field = "django.db.models.AutoField"

and then in your settings, replace cookie_consent in your INSTALLED_APPS with yourproject.cookie_consent_config.MyCookieConsentConf, so it will use your app config rather than the default one.


P.S. - unrelated, but in your (prod) docker image/container you likely don't want to run makemigrations, only migrate. Migrations are supposed to be generated at development time and checked into version control + your image build.