django-constance icon indicating copy to clipboard operation
django-constance copied to clipboard

Refactor app and model

Open sergei-iurchenko opened this issue 3 years ago • 5 comments

This MR reconsiders model status in the constance application. In current state model Constance does not belong to application constance - it belongs to application database. This have several issues:

  1. Impossible to support content type and permission management via Django build-in functions. ConstanceConfig contains code to create them manually. Django does it automatically. Build-in cleaner of stale content types ./manage.py remove_stale_contenttypes --include-stale-apps removes manually created permissions and content types

  2. https://github.com/jazzband/django-constance/issues/454 My PR fixes it

  3. Different import errors which some users get. https://github.com/jazzband/django-constance/issues/452 Such issues would go away.

  4. Enables Django build-in database routing to work by default https://github.com/jazzband/django-constance/issues/414 https://github.com/jazzband/django-constance/issues/376

What changes? Database table would always be created. It would be empty if DatabaseBackend is not used. Migration on new table would be automatic for new and old installations.

Django is carp enough for model to be attached to application. django-constance is rather old application and earlier it was not so strict.

This PR is draft. It is working example with code refactoring tested on 2 projects and postgres database. If my refactoring can be accepted I would add documentation, release notes about migration and will test on sqlite, mysql and postgres carefully.

I am open to discuss MR

sergei-iurchenko avatar Jan 11 '22 16:01 sergei-iurchenko

Codecov Report

Merging #469 (3f4435e) into master (b6f8e2c) will increase coverage by 0.25%. The diff coverage is 82.14%.

:exclamation: Current head 3f4435e differs from pull request most recent head 4ffa756. Consider uploading reports for the commit 4ffa756 to get more accurate results

@@            Coverage Diff             @@
##           master     #469      +/-   ##
==========================================
+ Coverage   83.19%   83.44%   +0.25%     
==========================================
  Files          16       16              
  Lines         601      574      -27     
  Branches      120      115       -5     
==========================================
- Hits          500      479      -21     
+ Misses         67       63       -4     
+ Partials       34       32       -2     
Impacted Files Coverage Δ
constance/management/commands/constance.py 92.72% <77.77%> (+5.84%) :arrow_up:
constance/models.py 81.25% <81.25%> (ø)
constance/apps.py 100.00% <100.00%> (+5.00%) :arrow_up:
constance/backends/database/__init__.py 58.69% <100.00%> (ø)
constance/admin.py 87.81% <0.00%> (-0.74%) :arrow_down:
constance/checks.py 80.00% <0.00%> (+6.08%) :arrow_up:

:mega: We’re building smart automated test selection to slash your CI/CD build times. Learn more

codecov[bot] avatar Jan 11 '22 17:01 codecov[bot]

Hello @Mogost ! Review please =)

sergei-iurchenko avatar Feb 14 '22 09:02 sergei-iurchenko

I generally like the direction in which you are thinking. But I see a lot of risks in migrating old projects. Just for example, I found a place in my project where my migrations were dependent on Constance migrations like that:

class Migration(migrations.Migration):
    dependencies = [
        ('database', '0001_initial'),
    .....

Probably the right way would be not to delete migrations in database but to create new migrations in constance. Also, the database application needs a final migration to clean up all traces and remove the table from the database. Otherwise, this table will stay there forever. Also migrations and cleanup for permissions is needed. image If we do not migrate permissions constance users lost their perm configurations.

I also want to summon here @jezdez to review

Mogost avatar Feb 15 '22 16:02 Mogost

Hello @Mogost ! Sorry for so long review fixes. Now everything is saved: table, permissions, content types.

sergei-iurchenko avatar Jun 16 '22 18:06 sergei-iurchenko

@jazzband/django-constance Please help to review

Mogost avatar Jun 21 '22 13:06 Mogost

@Mogost @camilonova Well, I fixed everything. Review finally please =)

sergei-iurchenko avatar Feb 27 '23 01:02 sergei-iurchenko

Does this mean there's no more database backend for constance? I upgraded from 2.9.1 and I'm unable to get it to work. When I try to install django-constance[database] I get: WARNING: django-constance 3.1.0 does not provide the extra 'database'

When I try to run, I get: from constance.backends.database.models import Constance ModuleNotFoundError: No module named 'constance.backends.database.models'; 'constance.backends.database' is not a package

SpecialK118 avatar Oct 17 '23 09:10 SpecialK118

@SpecialK118 Hello) install just via pip install django-constance. 'constance.backends.database' should be removed from INSTALLED_APPS Changes are described in changelog

sergei-iurchenko avatar Oct 17 '23 16:10 sergei-iurchenko