From ee7388052c45f521cdff5d00c63ab6b07d43b2c1 Mon Sep 17 00:00:00 2001 From: Joel Bradshaw Date: Fri, 26 Feb 2021 22:57:26 -0800 Subject: [PATCH 1/2] Use SQL parameters, and make unconvert work DBAs don't want you to know about this One Simple Trick --- bookwyrm/migrations/0046_reviewrating.py | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/bookwyrm/migrations/0046_reviewrating.py b/bookwyrm/migrations/0046_reviewrating.py index b29cdbd2..776cae84 100644 --- a/bookwyrm/migrations/0046_reviewrating.py +++ b/bookwyrm/migrations/0046_reviewrating.py @@ -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): From 717cbe303412e40feb4cf3553ed0cc760eec581c Mon Sep 17 00:00:00 2001 From: Joel Bradshaw Date: Fri, 26 Feb 2021 23:12:39 -0800 Subject: [PATCH 2/2] Use very fancy SQL nonsense This should be more efficient than running the queries one by one --- bookwyrm/migrations/0046_reviewrating.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/bookwyrm/migrations/0046_reviewrating.py b/bookwyrm/migrations/0046_reviewrating.py index 776cae84..e410ba91 100644 --- a/bookwyrm/migrations/0046_reviewrating.py +++ b/bookwyrm/migrations/0046_reviewrating.py @@ -4,6 +4,7 @@ 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 def convert_review_rating(app_registry, schema_editor): ''' take rating type Reviews and convert them to ReviewRatings ''' @@ -16,11 +17,10 @@ def convert_review_rating(app_registry, schema_editor): ) with connection.cursor() as cursor: - for review in reviews: - cursor.execute(''' + values = [(r.id,) for r in reviews] + execute_values(cursor, ''' INSERT INTO bookwyrm_reviewrating(review_ptr_id) -SELECT status_ptr_id FROM bookwyrm_review -WHERE status_ptr_id=%s''', (review.id)) +VALUES %s''', values) def unconvert_review_rating(app_registry, schema_editor): ''' undo the conversion from ratings back to reviews'''