moviewyrm/bookwyrm/migrations/0046_reviewrating.py

67 lines
1.9 KiB
Python
Raw Normal View History

2021-02-25 22:58:09 +00:00
# Generated by Django 3.0.7 on 2021-02-25 18:36
from django.db import migrations, models
from django.db import connection
from django.db.models import Q
import django.db.models.deletion
from psycopg2.extras import execute_values
2021-02-25 22:58:09 +00:00
2021-03-08 17:54:02 +00:00
2021-02-25 22:58:09 +00:00
def convert_review_rating(app_registry, schema_editor):
2021-04-26 16:15:42 +00:00
"""take rating type Reviews and convert them to ReviewRatings"""
2021-02-25 22:58:09 +00:00
db_alias = schema_editor.connection.alias
2021-03-08 17:54:02 +00:00
reviews = (
app_registry.get_model("bookwyrm", "Review")
.objects.using(db_alias)
.filter(Q(content__isnull=True) | Q(content=""))
2021-02-25 22:58:09 +00:00
)
with connection.cursor() as cursor:
values = [(r.id,) for r in reviews]
2021-03-08 17:54:02 +00:00
execute_values(
cursor,
"""
2021-02-25 22:58:09 +00:00
INSERT INTO bookwyrm_reviewrating(review_ptr_id)
2021-03-08 17:54:02 +00:00
VALUES %s""",
values,
)
2021-02-25 22:58:09 +00:00
def unconvert_review_rating(app_registry, schema_editor):
2021-04-26 16:15:42 +00:00
"""undo the conversion from ratings back to reviews"""
# All we need to do to revert this is drop the table, which Django will do
# on its own, as long as we have a valid reverse function. So, this is a
# no-op function so Django will do its thing
2021-02-25 22:58:09 +00:00
2021-03-08 17:54:02 +00:00
2021-02-25 22:58:09 +00:00
class Migration(migrations.Migration):
dependencies = [
2021-03-08 17:54:02 +00:00
("bookwyrm", "0045_auto_20210210_2114"),
2021-02-25 22:58:09 +00:00
]
operations = [
migrations.CreateModel(
2021-03-08 17:54:02 +00:00
name="ReviewRating",
2021-02-25 22:58:09 +00:00
fields=[
2021-03-08 17:54:02 +00:00
(
"review_ptr",
models.OneToOneField(
auto_created=True,
on_delete=django.db.models.deletion.CASCADE,
parent_link=True,
primary_key=True,
serialize=False,
to="bookwyrm.Review",
),
),
2021-02-25 22:58:09 +00:00
],
options={
2021-03-08 17:54:02 +00:00
"abstract": False,
2021-02-25 22:58:09 +00:00
},
2021-03-08 17:54:02 +00:00
bases=("bookwyrm.review",),
2021-02-25 22:58:09 +00:00
),
migrations.RunPython(convert_review_rating, unconvert_review_rating),
]