forked from mirrors/bookwyrm
commit
3fd76a42f3
1 changed files with 8 additions and 18 deletions
|
@ -4,9 +4,10 @@ from django.db import migrations, models
|
||||||
from django.db import connection
|
from django.db import connection
|
||||||
from django.db.models import Q
|
from django.db.models import Q
|
||||||
import django.db.models.deletion
|
import django.db.models.deletion
|
||||||
|
from psycopg2.extras import execute_values
|
||||||
|
|
||||||
def convert_review_rating(app_registry, schema_editor):
|
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
|
db_alias = schema_editor.connection.alias
|
||||||
|
|
||||||
reviews = app_registry.get_model(
|
reviews = app_registry.get_model(
|
||||||
|
@ -16,27 +17,16 @@ def convert_review_rating(app_registry, schema_editor):
|
||||||
)
|
)
|
||||||
|
|
||||||
with connection.cursor() as cursor:
|
with connection.cursor() as cursor:
|
||||||
for review in reviews:
|
values = [(r.id,) for r in reviews]
|
||||||
cursor.execute('''
|
execute_values(cursor, '''
|
||||||
INSERT INTO bookwyrm_reviewrating(review_ptr_id)
|
INSERT INTO bookwyrm_reviewrating(review_ptr_id)
|
||||||
SELECT status_ptr_id FROM bookwyrm_review
|
VALUES %s''', values)
|
||||||
WHERE status_ptr_id={:d}'''.format(review.id))
|
|
||||||
|
|
||||||
def unconvert_review_rating(app_registry, schema_editor):
|
def unconvert_review_rating(app_registry, schema_editor):
|
||||||
''' undo the conversion from ratings back to reviews'''
|
''' undo the conversion from ratings back to reviews'''
|
||||||
# TODO: this does not work
|
# All we need to do to revert this is drop the table, which Django will do
|
||||||
db_alias = schema_editor.connection.alias
|
# 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
|
||||||
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):
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue