Use SQL parameters, and make unconvert work

DBAs don't want you to know about this One Simple Trick
This commit is contained in:
Joel Bradshaw 2021-02-26 22:57:26 -08:00
parent 3ba6479e79
commit ee7388052c

View file

@ -6,7 +6,7 @@ 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 '''
''' take rating type Reviews and convert them to ReviewRatings '''
db_alias = schema_editor.connection.alias
reviews = app_registry.get_model(
@ -20,23 +20,13 @@ def convert_review_rating(app_registry, schema_editor):
cursor.execute('''
INSERT INTO bookwyrm_reviewrating(review_ptr_id)
SELECT status_ptr_id FROM bookwyrm_review
WHERE status_ptr_id={:d}'''.format(review.id))
WHERE status_ptr_id=%s''', (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))
# 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):