# Generated by Django 5.1.2 on 2024-11-27 16:35

import django.db.models.deletion
from django.db import migrations, models, connection
from django.db.utils import ProgrammingError, OperationalError


def basebook_to_book(apps, schema_editor):
    basebook = apps.get_model("bookshelf", "BaseBook")
    book = apps.get_model("bookshelf", "Book")
    for row in basebook.objects.all():
        b = book.objects.create(
            basebook_ptr=row,
            title=row.old_title,
            publisher=row.old_publisher,
        )
        b.authors.set(row.old_authors.all())


def drop_temporary_tables(apps, schema_editor):
    try:
        with connection.cursor() as cursor:
            cursor.execute(
                'DROP TABLE IF EXISTS bookshelf_basebook_old_authors'
            )
            cursor.execute(
                'DROP TABLE IF EXISTS bookshelf_basebook_authors'
            )
    except (ProgrammingError, OperationalError):
        pass


class Migration(migrations.Migration):

    dependencies = [
        ("bookshelf", "0015_alter_book_authors"),
        ("metadata", "0019_alter_scale_gauge"),
    ]

    operations = [
        migrations.AlterModelOptions(
            name="Book",
            options={"ordering": ["creation_time"]},
        ),
        migrations.RenameModel(
            old_name="BookImage",
            new_name="BaseBookImage",
        ),
        migrations.RenameModel(
            old_name="BookProperty",
            new_name="BaseBookProperty",
        ),
        migrations.RenameModel(
            old_name="Book",
            new_name="BaseBook",
        ),
        migrations.RenameField(
            model_name="basebook",
            old_name="title",
            new_name="old_title",
        ),
        migrations.RenameField(
            model_name="basebook",
            old_name="authors",
            new_name="old_authors",
        ),
        migrations.RenameField(
            model_name="basebook",
            old_name="publisher",
            new_name="old_publisher",
        ),
        migrations.AlterModelOptions(
            name="basebookimage",
            options={"ordering": ["order"], "verbose_name_plural": "Images"},
        ),
        migrations.CreateModel(
            name="Book",
            fields=[
                (
                    "basebook_ptr",
                    models.OneToOneField(
                        auto_created=True,
                        on_delete=django.db.models.deletion.CASCADE,
                        parent_link=True,
                        primary_key=True,
                        serialize=False,
                        to="bookshelf.basebook",
                    ),
                ),
                ("title", models.CharField(max_length=200)),
                (
                    "authors",
                    models.ManyToManyField(
                        blank=True,
                        to="bookshelf.author"
                    ),
                ),
                (
                    "publisher",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        to="bookshelf.publisher"
                    ),
                 ),
            ],
            options={
                "ordering": ["title"],
            },
        ),
        migrations.RunPython(
            basebook_to_book,
            reverse_code=migrations.RunPython.noop
        ),
        migrations.RemoveField(
            model_name="basebook",
            name="old_title",
        ),
        migrations.RemoveField(
            model_name="basebook",
            name="old_publisher",
        ),
        migrations.CreateModel(
            name="Catalog",
            fields=[
                (
                    "basebook_ptr",
                    models.OneToOneField(
                        auto_created=True,
                        on_delete=django.db.models.deletion.CASCADE,
                        parent_link=True,
                        primary_key=True,
                        serialize=False,
                        to="bookshelf.basebook",
                    ),
                ),
                ("years", models.CharField(max_length=12)),
                (
                    "manufacturer",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        to="metadata.manufacturer",
                    ),
                ),
                ("scales", models.ManyToManyField(to="metadata.scale")),
            ],
            options={
                "ordering": ["manufacturer", "publication_year"],
            },
            bases=("bookshelf.basebook",),
        ),
        # Required by Dajngo 6.0 on SQLite
        migrations.SeparateDatabaseAndState(
            state_operations=[
                migrations.RemoveField(
                    model_name="basebook",
                    name="old_authors",
                ),
            ],
            database_operations=[
                migrations.RunPython(drop_temporary_tables)
            ]
        ),
    ]
