60 lines
1.9 KiB
Python
60 lines
1.9 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 conver 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={:d}'''.format(review.id))
|
||
|
|
||
|
def unconvert_review_rating(app_registry, schema_editor):
|
||
|
''' undo the conversion from ratings back to reviews'''
|
||
|
# TODO: this does not work
|
||
|
db_alias = schema_editor.connection.alias
|
||
|
|
||
|
ratings = app_registry.get_model(
|
||
|
'bookwyrm', 'ReviewRating'
|
||
|
).objects.using(db_alias).all()
|
||
|
|
||
|
with connection.cursor() as cursor:
|
||
|
for rating in ratings:
|
||
|
cursor.execute('''
|
||
|
INSERT INTO bookwyrm_review(status_ptr_id)
|
||
|
SELECT review_ptr_id FROM bookwyrm_reviewrating
|
||
|
WHERE review_ptr_id={:d}'''.format(rating.id))
|
||
|
|
||
|
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),
|
||
|
]
|