moviewyrm/bookwyrm/migrations/0046_reviewrating.py
Joel Bradshaw ee7388052c Use SQL parameters, and make unconvert work
DBAs don't want you to know about this One Simple Trick
2021-02-26 22:57:26 -08:00

50 lines
1.7 KiB
Python

# 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
def convert_review_rating(app_registry, schema_editor):
''' take rating type Reviews and convert them to ReviewRatings '''
db_alias = schema_editor.connection.alias
reviews = app_registry.get_model(
'bookwyrm', 'Review'
).objects.using(db_alias).filter(
Q(content__isnull=True) | Q(content='')
)
with connection.cursor() as cursor:
for review in reviews:
cursor.execute('''
INSERT INTO bookwyrm_reviewrating(review_ptr_id)
SELECT status_ptr_id FROM bookwyrm_review
WHERE status_ptr_id=%s''', (review.id))
def unconvert_review_rating(app_registry, schema_editor):
''' 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
class Migration(migrations.Migration):
dependencies = [
('bookwyrm', '0045_auto_20210210_2114'),
]
operations = [
migrations.CreateModel(
name='ReviewRating',
fields=[
('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')),
],
options={
'abstract': False,
},
bases=('bookwyrm.review',),
),
migrations.RunPython(convert_review_rating, unconvert_review_rating),
]