diff mbox

[v2,01/10] models: Resolve issues with Patch.state

Message ID 1443711154-18689-2-git-send-email-stephen.finucane@intel.com
State Superseded
Headers show

Commit Message

Stephen Finucane Oct. 1, 2015, 2:52 p.m. UTC
The initial migration was incomplete: running 'makemigrations' on
the current codebase would produce a migration which could not be
applied. Fix this issue and add a suitable migration to resolve the
issue henceforth.

Signed-off-by: Stephen Finucane <stephen.finucane@intel.com>
---
 patchwork/migrations/0001_initial.py                  |  2 +-
 .../migrations/0002_fix_patch_state_default_values.py | 19 +++++++++++++++++++
 patchwork/models.py                                   |  8 +++-----
 3 files changed, 23 insertions(+), 6 deletions(-)
 create mode 100644 patchwork/migrations/0002_fix_patch_state_default_values.py

Comments

Damien Lespiau Oct. 1, 2015, 3:09 p.m. UTC | #1
On Thu, Oct 01, 2015 at 03:52:25PM +0100, Stephen Finucane wrote:
> @@ -243,7 +243,7 @@ class Patch(models.Model):
>      date = models.DateTimeField(default=datetime.datetime.now)
>      submitter = models.ForeignKey(Person)
>      delegate = models.ForeignKey(User, blank = True, null = True)
> -    state = models.ForeignKey(State, default=get_default_initial_patch_state)
> +    state = models.ForeignKey(State, null=True)

I was wondering, is it necessary to change the null contraint here?
Stephen Finucane Oct. 1, 2015, 3:14 p.m. UTC | #2
> On Thu, Oct 01, 2015 at 03:52:25PM +0100, Stephen Finucane wrote:
> > @@ -243,7 +243,7 @@ class Patch(models.Model):
> >      date = models.DateTimeField(default=datetime.datetime.now)
> >      submitter = models.ForeignKey(Person)
> >      delegate = models.ForeignKey(User, blank = True, null = True)
> > -    state = models.ForeignKey(State,
> default=get_default_initial_patch_state)
> > +    state = models.ForeignKey(State, null=True)
> 
> I was wondering, is it necessary to change the null contraint here?

To the best of my knowledge, yes. We need a default but we don't always have any entries in the 'State' table (for example, when testing). As a result the only possible "default" is None. Note that this will never be set to a null value however due to the additions to the 'save' function.

> --
> Damien
> 
> >      archived = models.BooleanField(default = False)
> >      headers = models.TextField(blank = True)
> >      content = models.TextField(null = True, blank = True)
> > @@ -279,10 +279,8 @@ class Patch(models.Model):
> >              self._set_tag(tag, counter[tag])
> >
> >      def save(self):
> > -        try:
> > -            s = self.state
> > -        except:
> > -            self.state = State.objects.get(ordering =  0)
> > +        if not hasattr(self, 'state') or not self.state:
> > +            self.state = get_default_initial_patch_state()
> >
> >          if self.hash is None and self.content is not None:
> >              self.hash = hash_patch(self.content).hexdigest()
> > --
> > 2.0.0
> >
> > _______________________________________________
> > Patchwork mailing list
> > Patchwork@lists.ozlabs.org
> > https://lists.ozlabs.org/listinfo/patchwork
Damien Lespiau Oct. 1, 2015, 3:24 p.m. UTC | #3
On Thu, Oct 01, 2015 at 04:14:23PM +0100, Finucane, Stephen wrote:
> > On Thu, Oct 01, 2015 at 03:52:25PM +0100, Stephen Finucane wrote:
> > > @@ -243,7 +243,7 @@ class Patch(models.Model):
> > >      date = models.DateTimeField(default=datetime.datetime.now)
> > >      submitter = models.ForeignKey(Person)
> > >      delegate = models.ForeignKey(User, blank = True, null = True)
> > > -    state = models.ForeignKey(State,
> > default=get_default_initial_patch_state)
> > > +    state = models.ForeignKey(State, null=True)
> > 
> > I was wondering, is it necessary to change the null contraint here?
> 
> To the best of my knowledge, yes. We need a default but we don't
> always have any entries in the 'State' table (for example, when
> testing). As a result the only possible "default" is None. Note that
> this will never be set to a null value however due to the additions to
> the 'save' function.

I guess it's a bit weird to weaken the db schema for the testing use
case but it's doesn't really matter much to me. By the way this patch
already had an a-b tag from me.

Acked-by: Damien Lespiau <damien.lespiau@intel.com>
Stephen Finucane Oct. 1, 2015, 3:26 p.m. UTC | #4
> On Thu, Oct 01, 2015 at 04:14:23PM +0100, Finucane, Stephen wrote:
> > > On Thu, Oct 01, 2015 at 03:52:25PM +0100, Stephen Finucane wrote:
> > > > @@ -243,7 +243,7 @@ class Patch(models.Model):
> > > >      date = models.DateTimeField(default=datetime.datetime.now)
> > > >      submitter = models.ForeignKey(Person)
> > > >      delegate = models.ForeignKey(User, blank = True, null = True)
> > > > -    state = models.ForeignKey(State,
> > > default=get_default_initial_patch_state)
> > > > +    state = models.ForeignKey(State, null=True)
> > >
> > > I was wondering, is it necessary to change the null contraint here?
> >
> > To the best of my knowledge, yes. We need a default but we don't
> > always have any entries in the 'State' table (for example, when
> > testing). As a result the only possible "default" is None. Note that
> > this will never be set to a null value however due to the additions to
> > the 'save' function.
> 
> I guess it's a bit weird to weaken the db schema for the testing use
> case but it's doesn't really matter much to me. By the way this patch
> already had an a-b tag from me.

Agreed - it wouldn't have been my first choice but it ended up being the only viable one.

> Acked-by: Damien Lespiau <damien.lespiau@intel.com>

Thanks for the review!

> --
> Damien
Stephen Finucane Oct. 16, 2015, 10:26 p.m. UTC | #5
> > On Thu, Oct 01, 2015 at 04:14:23PM +0100, Finucane, Stephen wrote:

> > > > On Thu, Oct 01, 2015 at 03:52:25PM +0100, Stephen Finucane wrote:

> > > > > @@ -243,7 +243,7 @@ class Patch(models.Model):

> > > > >      date = models.DateTimeField(default=datetime.datetime.now)

> > > > >      submitter = models.ForeignKey(Person)

> > > > >      delegate = models.ForeignKey(User, blank = True, null = True)

> > > > > -    state = models.ForeignKey(State,

> > > > default=get_default_initial_patch_state)

> > > > > +    state = models.ForeignKey(State, null=True)

> > > >

> > > > I was wondering, is it necessary to change the null contraint here?

> > >

> > > To the best of my knowledge, yes. We need a default but we don't

> > > always have any entries in the 'State' table (for example, when

> > > testing). As a result the only possible "default" is None. Note that

> > > this will never be set to a null value however due to the additions to

> > > the 'save' function.

> >

> > I guess it's a bit weird to weaken the db schema for the testing use

> > case but it's doesn't really matter much to me. By the way this patch

> > already had an a-b tag from me.

> 

> Agreed - it wouldn't have been my first choice but it ended up being the

> only viable one.

> 

> > Acked-by: Damien Lespiau <damien.lespiau@intel.com>

> 

> Thanks for the review!


Merged.
diff mbox

Patch

diff --git a/patchwork/migrations/0001_initial.py b/patchwork/migrations/0001_initial.py
index 65d1c35..812558a 100644
--- a/patchwork/migrations/0001_initial.py
+++ b/patchwork/migrations/0001_initial.py
@@ -2,12 +2,12 @@ 
 from __future__ import unicode_literals
 
 from django.db import models, migrations
-from django.core.management import call_command
 import datetime
 import patchwork.models
 import django.db.models.deletion
 from django.conf import settings
 
+
 class Migration(migrations.Migration):
 
     dependencies = [
diff --git a/patchwork/migrations/0002_fix_patch_state_default_values.py b/patchwork/migrations/0002_fix_patch_state_default_values.py
new file mode 100644
index 0000000..4887935
--- /dev/null
+++ b/patchwork/migrations/0002_fix_patch_state_default_values.py
@@ -0,0 +1,19 @@ 
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+
+from django.db import models, migrations
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('patchwork', '0001_initial'),
+    ]
+
+    operations = [
+        migrations.AlterField(
+            model_name='patch',
+            name='state',
+            field=models.ForeignKey(to='patchwork.State', null=True),
+        ),
+    ]
diff --git a/patchwork/models.py b/patchwork/models.py
index c2b8a9c..0c8022c 100644
--- a/patchwork/models.py
+++ b/patchwork/models.py
@@ -243,7 +243,7 @@  class Patch(models.Model):
     date = models.DateTimeField(default=datetime.datetime.now)
     submitter = models.ForeignKey(Person)
     delegate = models.ForeignKey(User, blank = True, null = True)
-    state = models.ForeignKey(State, default=get_default_initial_patch_state)
+    state = models.ForeignKey(State, null=True)
     archived = models.BooleanField(default = False)
     headers = models.TextField(blank = True)
     content = models.TextField(null = True, blank = True)
@@ -279,10 +279,8 @@  class Patch(models.Model):
             self._set_tag(tag, counter[tag])
 
     def save(self):
-        try:
-            s = self.state
-        except:
-            self.state = State.objects.get(ordering =  0)
+        if not hasattr(self, 'state') or not self.state:
+            self.state = get_default_initial_patch_state()
 
         if self.hash is None and self.content is not None:
             self.hash = hash_patch(self.content).hexdigest()