# -*- coding: utf-8 -*-
# Generated by Django 1.11.11 on 2018-03-22 07:48
from __future__ import unicode_literals

from django.db import migrations, models
from django.db.backends.postgresql.schema import DatabaseSchemaEditor
import django.db.models.deletion


def modify_fk_constraint(apps, schema_editor):
    """
    Delete's the current foreign key contraint on the outbound field, and adds
    it again, but this time with an ON DELETE clause
    """
    model = apps.get_model("message_sender", "OutboundSendFailure")
    table = model._meta.db_table

    with schema_editor.connection.cursor() as cursor:
        constraints = schema_editor.connection.introspection.get_constraints(
            cursor, table
        )
        [constraint] = filter(lambda c: c[1]["foreign_key"], constraints.items())
        [name, _] = constraint
    sql_delete_fk = (
        "SET CONSTRAINTS {name} IMMEDIATE; "
        "ALTER TABLE {table} DROP CONSTRAINT {name}"
    ).format(table=schema_editor.quote_name(table), name=schema_editor.quote_name(name))
    schema_editor.execute(sql_delete_fk)

    field = model.outbound.field
    to_table = field.remote_field.model._meta.db_table
    to_column = field.remote_field.model._meta.get_field(
        field.remote_field.field_name
    ).column
    sql_create_fk = (
        "ALTER TABLE {table} ADD CONSTRAINT {name} FOREIGN KEY "
        "({column}) REFERENCES {to_table} ({to_column}) "
        "ON DELETE CASCADE {deferrable};"
    ).format(
        table=schema_editor.quote_name(table),
        name=schema_editor.quote_name(name),
        column=schema_editor.quote_name(field.column),
        to_table=schema_editor.quote_name(to_table),
        to_column=schema_editor.quote_name(to_column),
        deferrable=schema_editor.connection.ops.deferrable_sql(),
    )
    schema_editor.execute(sql_create_fk)


class Migration(migrations.Migration):

    dependencies = [
        ("message_sender", "0007_outboundsendfailure"),
        ("message_sender", "0019_auto_20180319_1446"),
    ]

    operations = [
        migrations.AlterField(
            model_name="outboundsendfailure",
            name="outbound",
            field=models.ForeignKey(
                on_delete=django.db.models.deletion.DO_NOTHING,
                to="message_sender.Outbound",
            ),
        ),
        migrations.RunPython(modify_fk_constraint),
    ]
