Creates new semi-functional migration

This commit is contained in:
Mouse Reeve 2021-02-25 14:58:09 -08:00
parent ffd57dfef4
commit 2e36cfbcf6
2 changed files with 59 additions and 72 deletions

View file

@ -1,72 +0,0 @@
# Generated by Django 3.0.7 on 2021-01-05 05:32
from django.db.models.fields.reverse_related import ManyToOneRel
from django.db.models import Q
from django.db import migrations, models
import django.db.models.deletion
def convert_review_rating(app_registry, schema_editor):
''' take reviews with no content and turn them into ratings '''
db_alias = schema_editor.connection.alias
reviews = app_registry.get_model('bookwyrm', 'Review')
review_ratings = app_registry.get_model('bookwyrm', 'ReviewRating')
ratings = reviews.objects.using(db_alias).filter(
Q(content__isnull=True) | Q(content=''))
# replace the old review with the rating
for review in ratings:
rating = review_ratings.objects.create(
user=review.user, rating=review.rating, book=review.book)
print('-----')
print(rating, review)
for field in review._meta.get_fields():
if isinstance(field, ManyToOneRel) or field.name in ['status_ptr', 'id', 'remote_id']:
continue
value = getattr(review, field.name)
try:
setattr(rating, field.name, value)
except TypeError:
getattr(rating, field.name).set(value.all())
rating.save()
# move related models away from old review
related_models = [
(r.remote_field.name, r.related_model.__name__) for r in \
review._meta.related_objects]
for (related_field, related_model_name) in related_models:
related_model = app_registry.get_model('bookwyrm', related_model_name)
related_objs = related_model.objects.using(db_alias).filter(
**{related_field: review})
for related_obj in related_objs:
print('related_obj', related_obj)
print('field', related_field)
try:
print(rating)
print('before', getattr(related_obj, related_field))
setattr(related_obj, related_field, rating)
related_obj.save()
print('after', getattr(related_obj, related_field))
except TypeError:
getattr(related_obj, related_field).add(rating)
getattr(related_obj, related_field).remove(review)
review.delete()
class Migration(migrations.Migration):
dependencies = [
('bookwyrm', '0032_auto_20210104_2055'),
]
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),
]

View file

@ -0,0 +1,59 @@
# 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),
]