Fix comment revision count

This commit is contained in:
int-y1 2024-04-11 23:46:35 -04:00 committed by Tudor Brindus
parent 724af7ef77
commit d2e9fac6a9
2 changed files with 40 additions and 1 deletions

View File

@ -0,0 +1,39 @@
from django.core.exceptions import ObjectDoesNotExist
from django.db import migrations, models
def populate_revisions(apps, schema_editor):
db_alias = schema_editor.connection.alias
ContentType = apps.get_model('contenttypes', 'ContentType')
try:
content_type = ContentType.objects.get(app_label='judge', model='Comment')
except ObjectDoesNotExist:
# If you don't have content types, then you obviously haven't had any edited comments.
# Therefore, it's safe to all revision counts as zero.
pass
else:
schema_editor.execute("""\
UPDATE `judge_comment` INNER JOIN (
SELECT CAST(`reversion_version`.`object_id` AS INT) AS `id`, COUNT(*) AS `count`
FROM `reversion_version`
WHERE `reversion_version`.`content_type_id` = %s AND
`reversion_version`.`db` = %s
GROUP BY 1
) `versions` ON (`judge_comment`.`id` = `versions`.`id`)
SET `judge_comment`.`revisions` = `versions`.`count`;
""", (content_type.id, db_alias))
class Migration(migrations.Migration):
dependencies = [
('judge', '0145_site_data_batch_prerequisites'),
]
operations = [
migrations.AlterField(
model_name='comment',
name='revisions',
field=models.IntegerField(default=1, verbose_name='revisions'),
),
migrations.RunPython(populate_revisions, migrations.RunPython.noop, atomic=False, elidable=True),
]

View File

@ -33,7 +33,7 @@ class Comment(MPTTModel):
hidden = models.BooleanField(verbose_name=_('hidden'), default=0)
parent = TreeForeignKey('self', verbose_name=_('parent'), null=True, blank=True, related_name='replies',
on_delete=CASCADE)
revisions = models.IntegerField(verbose_name=_('revisions'), default=0)
revisions = models.IntegerField(verbose_name=_('revisions'), default=1)
class Meta:
verbose_name = _('comment')