Skip to content

Commit

Permalink
Cleanup migrations (#142)
Browse files Browse the repository at this point in the history
See #141 and #140, migration cleanup and django2/python3.6 compat
  • Loading branch information
EliotBerriot authored Jun 17, 2018
2 parents 1be3af7 + f40a00e commit d0afd8f
Show file tree
Hide file tree
Showing 19 changed files with 147 additions and 122 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,4 @@ example/*.sqlite*
.idea/
.python-version
.cache
.pytest_cache
48 changes: 29 additions & 19 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,45 @@ language: python
cache: pip
sudo: false

branches:
# to disable duplicated builds from PR/branches
only:
- "master"
- "develop"

python:
- "2.7"
- "3.5"
- "3.6"

env:
# - TOX_ENV=py36-django-110
- TOX_ENV=py35-django-110
- TOX_ENV=py34-django-110
- TOX_ENV=py27-django-110
# - TOX_ENV=py36-django-111
- TOX_ENV=py35-django-111
- TOX_ENV=py34-django-111
- TOX_ENV=py27-django-111
- TOX_ENV=py35-django-20
- TOX_ENV=py35-django-master
- DJANGO=1.11
- DJANGO=2.0
- DJANGO=2.1
- DJANGO=master

matrix:
fast_finish: true
allow_failures:
- env: TOX_ENV=py35-django-master
# - env: TOX_ENV=py36-django-master
exclude:
- { python: "2.7", env: DJANGO=master }
- { python: "2.7", env: DJANGO=2.0 }
- { python: "2.7", env: DJANGO=2.1 }
- { python: "3.5", env: DJANGO=master }
- { python: "3.5", env: DJANGO=2.1 }

# command to install dependencies, e.g. pip install -r requirements.txt --use-mirrors
# install: pip install pytest-django
allow_failures:
- env: DJANGO=master
- env: DJANGO=2.1

# command to run tests using coverage, e.g. python setup.py test
script: tox -e $TOX_ENV
script:
- tox

install:
- pip install tox codecov
- pip install tox tox-travis

after_success:
- codecov -e TOX_ENV
- pip install codecov
- codecov -e TOXENV,DJANGO

notifications:
email: false
60 changes: 60 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,66 @@
Changelog
=========

1.6 (Unreleased)
****************

- Fixed #141 and #141: migrations issues (see below)
- Dropped support for django < 1.11
- Dropped support for Python 3.4
- Better namespaces for urls

Better namespaces for urls
--------------------------

Historically, the package included multiple urls. To ensure compatibility with django 2
and better namespacing, you should update any references to those urls as described below:

+-------------------------------------+-------------------------------------+
| Old url | New url |
+=====================================+=====================================+
| dynamic_preferences.global | dynamic_preferences:global |
+-------------------------------------+-------------------------------------+
| dynamic_preferences.global.section | dynamic_preferences:global.section |
+-------------------------------------+-------------------------------------+
| dynamic_preferences.user | dynamic_preferences:user |
+-------------------------------------+-------------------------------------+
| dynamic_preferences.user.section | dynamic_preferences:user.section |
+-------------------------------------+-------------------------------------+


Migration cleanup
-----------------

This version includes a proper fix for migration issues.
Full background is available at https://github.com/EliotBerriot/django-dynamic-preferences/pull/142,
but here is the gist of it:

1. Early versions of dynamic_preferences included the user and global preferences models
in the same app
2. The community requested a way to disable user preferences. The only way to do that
was to move the user preference model in a dedicated app (dynamic_preferences_user
3. A migration was written to handle that transparently, but this was not actually possible
to have something that worked for both existing and new installations
4. Thus, we ended up with issues such as #140 or #141, inconsistent db state, tables
lying around in the database, etc.

I'd like to apologize to everyone impacted. By trying to make 3. completely transparent to everyone and
avoid a manual migration step for new installations, I actually made things worse.

This release should fix all that: any remains of the user app was removed from the main
app migrations. For any new user, it will be like nothing happened.

For existing installations with user preferences disabled, there is nothing to do,
apart from deleting the `dynamic_preferences_users_userpreferencemodel` table in your database.

For existing installations with user preferences enabled, there is nothing to do. You should have
``'dynamic_preferences.users.apps.UserPreferencesConfig'`` in your installed apps. If ``python manage.py migrate``
fails with ``django.db.utils.ProgrammingError: relation "dynamic_preferences_users_userpreferencemodel" already exists``,
this probably means you are upgrading for a really old release. In such event, simply skip the initial migration for the
``dynamic_preferences_user`` app by running ``python manage.py migrate dynamic_preferences_users 0001 --fake``.

Many thanks to all people who helped clearing this mess, especially @czlee.

1.5.1 (06-03-2018)
******************

Expand Down
22 changes: 0 additions & 22 deletions dynamic_preferences/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,6 @@ class Migration(migrations.Migration):
},
bases=(models.Model,),
),
migrations.CreateModel(
name='UserPreferenceModel',
fields=[
('id', models.AutoField(primary_key=True, serialize=False, verbose_name='ID', auto_created=True)),
('section', models.CharField(blank=True, default=None, null=True, max_length=150, db_index=True)),
('name', models.CharField(max_length=150, db_index=True)),
('raw_value', models.TextField(blank=True, null=True)),
('instance', models.ForeignKey(to=settings.AUTH_USER_MODEL,
on_delete=models.CASCADE,
related_name='preferences')),
],
options={
'verbose_name_plural': 'user preferences',
'abstract': False,
'verbose_name': 'user preference',
},
bases=(models.Model,),
),
migrations.AlterUniqueTogether(
name='userpreferencemodel',
unique_together=set([('instance', 'section', 'name')]),
),
migrations.AlterUniqueTogether(
name='globalpreferencemodel',
unique_together=set([('section', 'name')]),
Expand Down
15 changes: 0 additions & 15 deletions dynamic_preferences/migrations/0002_auto_20150712_0332.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,4 @@ class Migration(migrations.Migration):
name='section',
field=models.CharField(max_length=150, blank=True, db_index=True, default=None, null=True),
),
migrations.AlterField(
model_name='userpreferencemodel',
name='instance',
field=models.ForeignKey(to=settings.AUTH_USER_MODEL, on_delete=models.CASCADE),
),
migrations.AlterField(
model_name='userpreferencemodel',
name='name',
field=models.CharField(max_length=150, db_index=True),
),
migrations.AlterField(
model_name='userpreferencemodel',
name='section',
field=models.CharField(max_length=150, blank=True, db_index=True, default=None, null=True),
),
]
12 changes: 0 additions & 12 deletions dynamic_preferences/migrations/0003_auto_20151223_1407.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,4 @@ class Migration(migrations.Migration):
field=models.CharField(max_length=150, null=True, default=None, db_index=True, blank=True),
preserve_default=True,
),
migrations.AlterField(
model_name='userpreferencemodel',
name='name',
field=models.CharField(max_length=150, db_index=True),
preserve_default=True,
),
migrations.AlterField(
model_name='userpreferencemodel',
name='section',
field=models.CharField(max_length=150, null=True, default=None, db_index=True, blank=True),
preserve_default=True,
),
]
17 changes: 3 additions & 14 deletions dynamic_preferences/migrations/0004_move_user_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from __future__ import unicode_literals

from django.db import models, migrations
from django.conf import settings


class Migration(migrations.Migration):
Expand All @@ -13,17 +14,5 @@ class Migration(migrations.Migration):
('dynamic_preferences', '0003_auto_20151223_1407'),
]

database_operations = [
migrations.AlterModelTable(
'UserPreferenceModel', 'dynamic_preferences_users_userpreferencemodel')
]

state_operations = [
migrations.DeleteModel('UserPreferenceModel')
]

operations = [
migrations.SeparateDatabaseAndState(
database_operations=database_operations,
state_operations=state_operations)
]
# cf https://github.com/EliotBerriot/django-dynamic-preferences/pull/142
operations = []
2 changes: 1 addition & 1 deletion dynamic_preferences/registries.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ class PerInstancePreferenceRegistry(PreferenceRegistry):


class GlobalPreferenceRegistry(PreferenceRegistry):
section_url_namespace = 'dynamic_preferences.global.section'
section_url_namespace = 'dynamic_preferences:global.section'

def populate(self, **kwargs):
return self.models(**kwargs)
Expand Down
5 changes: 3 additions & 2 deletions dynamic_preferences/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@
from .registries import global_preferences_registry
from .forms import GlobalPreferenceForm

app_name = 'dynamic_preferences'

urlpatterns = [

url(r'^global/$',
staff_member_required(views.PreferenceFormView.as_view(
registry=global_preferences_registry,
form_class=GlobalPreferenceForm)),
name="dynamic_preferences.global"),
name="global"),
url(r'^global/(?P<section>[\w\ ]+)$',
staff_member_required(views.PreferenceFormView.as_view(
registry=global_preferences_registry,
form_class=GlobalPreferenceForm)),
name="dynamic_preferences.global.section"),
name="global.section"),

url(r'^user/', include('dynamic_preferences.users.urls')),
]
13 changes: 3 additions & 10 deletions dynamic_preferences/users/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10.1 on 2017-02-21 06:59
from __future__ import unicode_literals
# Generated by Django 2.0.6 on 2018-06-15 16:20

from django.conf import settings
from django.db import migrations, models
Expand All @@ -12,11 +10,10 @@ class Migration(migrations.Migration):
initial = True

dependencies = [
('dynamic_preferences', '0004_move_user_model'),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

state_operations = [
operations = [
migrations.CreateModel(
name='UserPreferenceModel',
fields=[
Expand All @@ -34,10 +31,6 @@ class Migration(migrations.Migration):
),
migrations.AlterUniqueTogether(
name='userpreferencemodel',
unique_together=set([('instance', 'section', 'name')]),
unique_together={('instance', 'section', 'name')},
),
]

operations = [
migrations.SeparateDatabaseAndState(state_operations=state_operations)
]
5 changes: 2 additions & 3 deletions dynamic_preferences/users/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@
from django.contrib.auth.decorators import login_required
from . import views


urlpatterns = [

url(r'^$',
login_required(views.UserPreferenceFormView.as_view()),
name="dynamic_preferences.user"),
name="user"),
url(r'^(?P<section>[\w\ ]+)$',
login_required(views.UserPreferenceFormView.as_view()),
name="dynamic_preferences.user.section"),
name="user.section"),
]
21 changes: 21 additions & 0 deletions example/example/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Generated by Django 2.0 on 2018-06-14 16:22

from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='MyModel',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('title', models.CharField(max_length=50)),
],
),
]
Empty file.
1 change: 0 additions & 1 deletion example/example/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
'django.contrib.staticfiles',
'dynamic_preferences',
'dynamic_preferences.users.apps.UserPreferencesConfig',
'debug_toolbar',
'example',
)

Expand Down
2 changes: 1 addition & 1 deletion example/example/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
# url(r'^$', 'example.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),

url(r'^admin/', include(admin.site.urls)),
url(r'^admin/', admin.site.urls),
url(r'^preferences/', include('dynamic_preferences.urls')),
]
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
],
include_package_data=True,
install_requires=[
'django>=1.8',
'django>=1.11',
'six',
'persisting_theory==0.2.1',
],
Expand Down
Loading

0 comments on commit d0afd8f

Please sign in to comment.