# Generated by Django 2.2.9 on 2020-01-06 10:57

import django.db.models.deletion
import wagtailmetadata.models
from django.db import connection, migrations, models


class RenamePrimaryKey(migrations.RenameField):
    @property
    def reversible(self):
        return False

    def update_primary_key_relations(self, schema_editor, new_field):
        if schema_editor.connection.vendor == "sqlite":
            for rel in new_field.model._meta.related_objects:
                if not rel.many_to_many:
                    schema_editor._remake_table(rel.related_model)  # pylint: disable=protected-access

    def database_forwards(self, app_label, schema_editor, from_state, to_state):
        super().database_forwards(app_label, schema_editor, from_state, to_state)

        to_model = to_state.apps.get_model(app_label, self.model_name)
        new_field = to_model._meta.get_field(self.new_name)

        self.update_primary_key_relations(schema_editor, new_field)

    def database_backwards(self, app_label, schema_editor, from_state, to_state):
        raise NotImplementedError("You cannot reverse this operation")


# pylint: disable=unused-argument
def disable_check_constraints(apps, schema_editor):
    if schema_editor.connection.vendor == "sqlite":
        schema_editor.connection.check_constraints = lambda *args, **x: None


class Migration(migrations.Migration):
    atomic = connection.features.supports_atomic_references_rename

    dependencies = [
        ("ocyan_plugin_wagtail", "0026_remove_wagtailtrans"),
    ]

    operations = [
        migrations.RunPython(disable_check_constraints, migrations.RunPython.noop),
        migrations.SeparateDatabaseAndState(
            state_operations=[
                migrations.DeleteModel("AbstractBasePage"),
                migrations.CreateModel(
                    name="AbstractBasePage",
                    fields=[
                        (
                            "page_ptr",
                            models.OneToOneField(
                                auto_created=True,
                                on_delete=django.db.models.deletion.CASCADE,
                                parent_link=True,
                                primary_key=True,
                                serialize=False,
                                to="wagtailcore.Page",
                            ),
                        ),
                        (
                            "meta_keywords",
                            models.TextField(
                                blank=True, null=True, verbose_name="Meta Keywords"
                            ),
                        ),
                        (
                            "search_image",
                            models.ForeignKey(
                                blank=True,
                                null=True,
                                on_delete=django.db.models.deletion.SET_NULL,
                                related_name="+",
                                to="wagtailimages.Image",
                                verbose_name="Search image",
                            ),
                        ),
                    ],
                    options={
                        "abstract": False,
                    },
                    bases=(
                        wagtailmetadata.models.WagtailImageMetadataMixin,
                        "wagtailcore.page",
                        models.Model,
                    ),
                ),
            ],
            database_operations=[
                RenamePrimaryKey(
                    model_name="abstractbasepage",
                    old_name="translatablepage_ptr",
                    new_name="page_ptr",
                ),
            ],
        ),
    ]
