diff --git a/bookwyrm/migrations/0151_auto_20220703_1842.py b/bookwyrm/migrations/0151_auto_20220703_1842.py index 7ceda5abe..10d408127 100644 --- a/bookwyrm/migrations/0151_auto_20220703_1842.py +++ b/bookwyrm/migrations/0151_auto_20220703_1842.py @@ -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), ] diff --git a/bookwyrm/migrations/0152_auto_20220704_0135.py b/bookwyrm/migrations/0152_auto_20220704_0135.py new file mode 100644 index 000000000..882148715 --- /dev/null +++ b/bookwyrm/migrations/0152_auto_20220704_0135.py @@ -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", + ), + ]