Migrations need to be in two files to work

Good news: they run now
This commit is contained in:
Mouse Reeve 2022-07-03 18:41:09 -07:00
parent f8a1b37eda
commit 2cbc8db5f7
2 changed files with 56 additions and 31 deletions

View file

@ -1,7 +1,7 @@
# Generated by Django 3.2.13 on 2022-07-03 18:42 # Generated by Django 3.2.13 on 2022-07-03 18:42
from django.db import migrations, models from django.db import migrations, models
from django.db.models import OuterRef, Subquery, F, Q from django.db.models import OuterRef, Subquery, Q
# TODO: test this # TODO: test this
@ -23,12 +23,12 @@ def set_read_status(apps, schema_editor):
# if it has finished date, it's read. strictly speaking this is unnecessary if all # if it has finished date, it's read. strictly speaking this is unnecessary if all
# is well because this is the default value. # is well because this is the default value.
readthrough_model.objects.using(db_alias).filter( readthrough_model.objects.using(db_alias).filter(finish_date__isnull=False).update(
finished_date__isnull=False read_status="read"
).update(read_status="read") )
# if it has a stopped date, it's stopped # if it has a stopped date, it's stopped
readthrough_model.objects.using(db_alias).filter(stooped_date__isnull=False).update( readthrough_model.objects.using(db_alias).filter(stopped_date__isnull=False).update(
read_status="stopped-reading" read_status="stopped-reading"
) )
@ -41,7 +41,7 @@ def set_read_status(apps, schema_editor):
statuses = readthrough_model.objects.using(db_alias).filter( statuses = readthrough_model.objects.using(db_alias).filter(
user=OuterRef("shelf__user"), user=OuterRef("shelf__user"),
book=OuterRef("book"), book=OuterRef("book"),
status=OuterRef("shelf__identifier"), read_status=OuterRef("shelf__identifier"),
) )
statusesless_shelfbooks = ( statusesless_shelfbooks = (
shelfbook_model.objects.using(db_alias) shelfbook_model.objects.using(db_alias)
@ -49,8 +49,9 @@ def set_read_status(apps, schema_editor):
shelf__editable=False, # on a functional shelf shelf__editable=False, # on a functional shelf
) )
.annotate( # check if this shelbook has an associated status .annotate( # check if this shelbook has an associated status
status_exists=Subquery(statuses.exists()) status_exists=Subquery(statuses.values("id")[:1])
) )
.filter(status_exists__isnull=True)
) )
# create new statuses # create new statuses
@ -61,30 +62,20 @@ def set_read_status(apps, schema_editor):
book=sb.book, book=sb.book,
user=sb.shelf.user, user=sb.shelf.user,
) )
for sb in statusesless_shelfbooks.objects.all() for sb in statusesless_shelfbooks.all()
] ]
) )
def merge_finish_stopped_dates(apps, schema_editor): def reverse_read_status_step(apps, schema_editor):
"""Combine the finished and stopped dates fields""" """Infer the correct reading status from the existing readthrough data"""
db_alias = schema_editor.connection.alias db_alias = schema_editor.connection.alias
readthrough_model = apps.get_model("bookwyrm", "ReadThrough") readthrough_model = apps.get_model("bookwyrm", "ReadThrough")
readthrough_model.objects.using(db_alias).filter(stopped_date__isnull=False).update( readthrough_model.objects.using(db_alias).filter(read_status="reading").update(
finish_date=F("stopped_date") is_active=True
) )
def unmerge_finish_stopped_dates(apps, schema_editor):
"""Combine the finished and stopped dates fields"""
db_alias = schema_editor.connection.alias
readthrough_model = apps.get_model("bookwyrm", "ReadThrough")
readthrough_model.objects.using(db_alias).filter(
read_status="stopped-reading",
finish_date__isnull=False,
).update(stopped_date=F("finish_date"))
class Migration(migrations.Migration): class Migration(migrations.Migration):
dependencies = [ dependencies = [
@ -106,13 +97,5 @@ class Migration(migrations.Migration):
max_length=20, max_length=20,
), ),
), ),
migrations.RemoveField( migrations.RunPython(set_read_status, reverse_read_status_step),
model_name="readthrough",
name="is_active",
),
migrations.RunPython(merge_finish_stopped_dates, unmerge_finish_stopped_dates),
migrations.RemoveField(
model_name="readthrough",
name="stopped_date",
),
] ]

View file

@ -0,0 +1,42 @@
# Generated by Django 3.2.13 on 2022-07-04 01:35
from django.db import migrations
from django.db.models import F
def merge_finish_stopped_dates(apps, schema_editor):
"""Combine the finished and stopped dates fields"""
db_alias = schema_editor.connection.alias
readthrough_model = apps.get_model("bookwyrm", "ReadThrough")
readthrough_model.objects.using(db_alias).filter(stopped_date__isnull=False).update(
finish_date=F("stopped_date")
)
def unmerge_finish_stopped_dates(apps, schema_editor):
"""Combine the finished and stopped dates fields"""
db_alias = schema_editor.connection.alias
readthrough_model = apps.get_model("bookwyrm", "ReadThrough")
readthrough_model.objects.using(db_alias).filter(
read_status="stopped-reading",
finish_date__isnull=False,
).update(stopped_date=F("finish_date"))
class Migration(migrations.Migration):
dependencies = [
("bookwyrm", "0151_auto_20220703_1842"),
]
operations = [
migrations.RemoveField(
model_name="readthrough",
name="is_active",
),
migrations.RunPython(merge_finish_stopped_dates, unmerge_finish_stopped_dates),
migrations.RemoveField(
model_name="readthrough",
name="stopped_date",
),
]