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
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
@ -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
# is well because this is the default value.
readthrough_model.objects.using(db_alias).filter(
finished_date__isnull=False
).update(read_status="read")
readthrough_model.objects.using(db_alias).filter(finish_date__isnull=False).update(
read_status="read"
)
# 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"
)
@ -41,7 +41,7 @@ def set_read_status(apps, schema_editor):
statuses = readthrough_model.objects.using(db_alias).filter(
user=OuterRef("shelf__user"),
book=OuterRef("book"),
status=OuterRef("shelf__identifier"),
read_status=OuterRef("shelf__identifier"),
)
statusesless_shelfbooks = (
shelfbook_model.objects.using(db_alias)
@ -49,8 +49,9 @@ def set_read_status(apps, schema_editor):
shelf__editable=False, # on a functional shelf
)
.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
@ -61,30 +62,20 @@ def set_read_status(apps, schema_editor):
book=sb.book,
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):
"""Combine the finished and stopped dates fields"""
def reverse_read_status_step(apps, schema_editor):
"""Infer the correct reading status from the existing readthrough data"""
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")
readthrough_model.objects.using(db_alias).filter(read_status="reading").update(
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):
dependencies = [
@ -106,13 +97,5 @@ class Migration(migrations.Migration):
max_length=20,
),
),
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",
),
migrations.RunPython(set_read_status, reverse_read_status_step),
]

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",
),
]