Adds SQL steps to move author data to the new table

This commit is contained in:
Mouse Reeve 2022-07-09 15:08:27 -07:00
parent 4a980cd0e5
commit ef833f0e42
2 changed files with 47 additions and 4 deletions

View file

@ -0,0 +1,44 @@
# Generated by Django 3.2.14 on 2022-07-09 21:21
import bookwyrm.models.fields
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
("bookwyrm", "0154_bookauthor"),
]
operations = [
migrations.AddField(
model_name="book",
name="new_authors",
field=bookwyrm.models.fields.ManyToManyField(
related_name="new_authors",
through="bookwyrm.BookAuthor",
to="bookwyrm.Author",
),
),
migrations.RunSQL(
sql="""
INSERT INTO bookwyrm_bookauthor (id, book_id, author_id, created_date, updated_date, author_type)
SELECT id, book_id, author_id, NOW(), NOW(), 'author' from bookwyrm_book_authors;
""",
reverse_sql="""
INSERT INTO bookwyrm_book_authors (id, book_id, author_id)
SELECT id, book_id, author_id from bookwyrm_bookauthor;
""",
),
migrations.RemoveField(model_name="book", name="authors"),
migrations.RenameField(
model_name="book", old_name="new_authors", new_name="authors"
),
migrations.AlterField(
model_name="book",
name="authors",
field=bookwyrm.models.fields.ManyToManyField(
through="bookwyrm.BookAuthor", to="bookwyrm.Author"
),
),
]

View file

@ -114,10 +114,9 @@ class Book(BookDataModel):
subject_places = fields.ArrayField(
models.CharField(max_length=255), blank=True, null=True, default=list
)
authors = fields.ManyToManyField("Author")
# authors = fields.ManyToManyField(
# "Author", through="BookAuthor", through_fields=("book", "author")
# )
authors = fields.ManyToManyField(
"Author", through="BookAuthor", through_fields=("book", "author")
)
cover = fields.ImageField(
upload_to="covers/", blank=True, null=True, alt_field="alt_text"
)