# -*- coding: utf-8 -*-
# Generated by Django 1.11.3 on 2017-10-16 02:24
from __future__ import unicode_literals

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
import django_extensions.db.fields


class Migration(migrations.Migration):

    dependencies = [
        ('sync', '0003_add_index_on_update_synchornized'),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        ('trainings', '0046_training_activity_description_nullable'),
    ]

    operations = [
        migrations.CreateModel(
            name='TrainingActivityLibraryStatus',
            fields=[
                ('id', django_extensions.db.fields.UUIDField(blank=True, editable=False, primary_key=True, serialize=False)),
                ('is_deleted', models.BooleanField(default=False)),
                ('update_locally', models.BooleanField(default=False)),
                ('is_shared', models.BooleanField(default=False, verbose_name='Is shared')),
                ('first_update', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='trainings_trainingactivitylibrarystatus_by_first', to='sync.Update')),
                ('last_update', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='trainings_trainingactivitylibrarystatus_by_last', to='sync.Update')),
                ('owner', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='trainingactivity_library_statuses', to=settings.AUTH_USER_MODEL, verbose_name='Owner')),
            ],
            options={
                'abstract': False,
            },
        ),
        migrations.AddField(
            model_name='trainingactivity',
            name='library_status',
            field=models.OneToOneField(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='training_activity', to='trainings.TrainingActivityLibraryStatus', verbose_name='Library status'),
        ),
        migrations.AlterUniqueTogether(
            name='trainingactivity',
            unique_together=set([('sort_order', 'training', 'session', 'library_status')]),
        ),
        # We need to make the unique library_status constraint deffered because when duplicating
        # an activity, the polymorphic parent model (``TrainingActivity``) might still be linked
        # during his save (the relation is deleted afterwhile)
        migrations.RunSQL(
            """
            ALTER TABLE trainings_trainingactivity DROP CONSTRAINT trainings_trainingactivity_library_status_id_key;
            ALTER TABLE trainings_trainingactivity ADD CONSTRAINT trainings_trainingactivity_library_status_id_key UNIQUE (library_status_id) DEFERRABLE INITIALLY DEFERRED;
            """,
        ),
        migrations.RunSQL(
            """
            ALTER TABLE trainings_trainingactivity DROP CONSTRAINT chk_training_or_session_not_together;
            ALTER TABLE trainings_trainingactivity ADD CONSTRAINT chk_training_or_session_or_librarystatus_not_together CHECK (
                ( training_id IS NOT NULL AND session_id IS NULL AND library_status_id IS NULL )
                OR ( session_id IS NOT NULL AND training_id IS NULL AND library_status_id IS NULL )
                OR ( library_status_id IS NOT NULL AND session_id IS NULL AND training_id IS NULL )
            );
            """,
            """
            ALTER TABLE trainings_trainingactivity DROP CONSTRAINT chk_training_or_session_or_librarystatus_not_together;
            ALTER TABLE trainings_trainingactivity ADD CONSTRAINT chk_training_or_session_not_together CHECK (
                (training_id IS NOT NULL OR session_id IS NOT NULL)
                AND
                (training_id IS NULL OR session_id IS NULL)
            );
            """
        )
    ]
