-
-
Notifications
You must be signed in to change notification settings - Fork 78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(StateLog): Migrated state log from integerfield to charfield #97
Open
josh-stableprice
wants to merge
12
commits into
jazzband:master
Choose a base branch
from
josh-stableprice:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
7443f2f
feat(StateLog): Migrated state log from integerfield to charfield
josh-stableprice 584639a
fix(checks): Imported StateLog too early for the tests
josh-stableprice b2314b7
python3 only
kcrebound 4682939
travis tox update
kcrebound 9da562a
travis tox update
kcrebound ad05c57
fix python2 models
kcrebound e446381
travis fix
kcrebound 74bdb46
tox fix
kcrebound d614849
fix flake
kcrebound 843366c
fix flake
kcrebound b2acc23
Merge pull request #1 from kcrebound/python3
kcrebound 3763368
Merge pull request #2 from kcrebound/master
josh-stableprice File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
default_app_config = 'django_fsm_log.apps.DjangoFSMLogAppConfig' | ||
from .checks import * # noqa |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import ast | ||
import inspect | ||
|
||
import django | ||
from django.core.checks import ( | ||
Warning, | ||
register, | ||
Tags, | ||
) | ||
from django.core.exceptions import FieldDoesNotExist | ||
|
||
|
||
@register(Tags.compatibility) | ||
def integer_object_id_check(app_configs, **kwargs): | ||
errors = [] | ||
for app in django.apps.apps.get_app_configs(): | ||
|
||
# Skip third party apps. | ||
if app.path.find('site-packages') > -1: | ||
continue | ||
|
||
for model in app.get_models(): | ||
for check_message in check_model_for_integer_object_id(model): | ||
errors.append(check_message) | ||
return errors | ||
|
||
|
||
def check_model_for_integer_object_id(model): | ||
"""Check a single model. | ||
|
||
Yields (django.checks.CheckMessage) | ||
""" | ||
|
||
from django_fsm_log.models import StateLog | ||
|
||
model_source = inspect.getsource(model) | ||
model_node = ast.parse(model_source) | ||
|
||
for node in model_node.body[0].body: | ||
|
||
# Check if node is a model field. | ||
if not isinstance(node, ast.Assign): | ||
continue | ||
|
||
if len(node.targets) != 1: | ||
continue | ||
|
||
if not isinstance(node.targets[0], ast.Name): | ||
continue | ||
|
||
field_name = node.targets[0].id | ||
try: | ||
field = model._meta.get_field(field_name) | ||
except FieldDoesNotExist: | ||
continue | ||
|
||
# Node is a model field here | ||
|
||
# Check if field has foreign key to StateLog defined | ||
for kw in node.value.keywords: | ||
if kw.arg == 'to': | ||
to = kw | ||
if to == 'django_fsm_log.StateLog' or isinstance(to, StateLog): | ||
yield Warning( | ||
'StateLog has changed its object_id from PositiveIntegerField to TextField to better support ' | ||
'the primary key types in Django.', | ||
hint='Migration PositiveIntegerField -> TextField is potentially needed on field {}.'.format( | ||
field.name | ||
), | ||
obj=field, | ||
id='django_fsm_log.W001', | ||
) | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Generated by Django 2.1.3 on 2018-11-16 12:57 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('django_fsm_log', '0005_description_null'), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name='statelog', | ||
name='object_id', | ||
field=models.TextField(db_index=True), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this warning absolutely necessary? I'm assuming it relates to https://code.djangoproject.com/ticket/25012 or similar?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Basically yeah, one of the only solid bits of contention in the issue where this was discussed was surrounding the use of the field by user based code which implicitly expected an integer.
I am also aware that I've been tripped up by types of foreign key references changing in the past in my own code and it's within the scope of the check framework to automate looking for these kinds of errors and warning users about them.
We can also nerf it again at some point in the futurewhen we feel we've had enough of a deprecation period. I just wish there was a way I could also check to see if any python code references these fields.