models: Change attachment.size to not be nullable.

Attachment objects in production are only created in one place, which
passses a size.  Additionally, I verified in multiple production
environments with old data that this never actually happens (or has
happened).

So we should make the data model correctly reflect the possibilities here.
This commit is contained in:
Tim Abbott 2020-06-20 12:58:35 -07:00
parent 22ead64d54
commit f98d244ed6
4 changed files with 33 additions and 6 deletions

View File

@ -0,0 +1,25 @@
# Generated by Django 2.2.13 on 2020-06-20 19:57
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('zerver', '0288_reaction_unique_on_emoji_code'),
]
operations = [
migrations.AlterField(
model_name='archivedattachment',
name='size',
field=models.IntegerField(default=0),
preserve_default=False,
),
migrations.AlterField(
model_name='attachment',
name='size',
field=models.IntegerField(default=0),
preserve_default=False,
),
]

View File

@ -2053,7 +2053,8 @@ class AbstractAttachment(models.Model):
create_time: datetime.datetime = models.DateTimeField(
default=timezone_now, db_index=True,
)
size: Optional[int] = models.IntegerField(null=True)
# Size of the uploaded file, in bytes
size: int = models.IntegerField()
# Whether this attachment has been posted to a public stream, and
# thus should be available to all non-guest users in the

View File

@ -11,7 +11,8 @@ class AttachmentsTests(ZulipTestCase):
super().setUp()
user_profile = self.example_user('cordelia')
self.attachment = Attachment.objects.create(
file_name='test.txt', path_id='foo/bar/test.txt', owner=user_profile)
file_name='test.txt', path_id='foo/bar/test.txt', owner=user_profile,
size=512)
def test_list_by_user(self) -> None:
user_profile = self.example_user('cordelia')

View File

@ -795,12 +795,12 @@ class ScrubRealmTest(ZulipTestCase):
self.send_stream_message(king, "Shakespeare")
Attachment.objects.filter(realm=zulip).delete()
Attachment.objects.create(realm=zulip, owner=iago, path_id="a/b/temp1.txt")
Attachment.objects.create(realm=zulip, owner=othello, path_id="a/b/temp2.txt")
Attachment.objects.create(realm=zulip, owner=iago, path_id="a/b/temp1.txt", size=512)
Attachment.objects.create(realm=zulip, owner=othello, path_id="a/b/temp2.txt", size=512)
Attachment.objects.filter(realm=lear).delete()
Attachment.objects.create(realm=lear, owner=cordelia, path_id="c/d/temp1.txt")
Attachment.objects.create(realm=lear, owner=king, path_id="c/d/temp2.txt")
Attachment.objects.create(realm=lear, owner=cordelia, path_id="c/d/temp1.txt", size=512)
Attachment.objects.create(realm=lear, owner=king, path_id="c/d/temp2.txt", size=512)
CustomProfileField.objects.create(realm=lear)