from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ('attempts', '0007_auto_20161004_0927'),
    ]

    operations = [
        migrations.AddField(
            model_name='attempt',
            name='submission_date',
            field=models.DateTimeField(null=True),
        ),
        migrations.AddField(
            model_name='historicalattempt',
            name='submission_date',
            field=models.DateTimeField(null=True),
        ),
        migrations.RunSQL(
            'UPDATE attempts_historicalattempt SET submission_date = history_date'
        ),
        migrations.RunSQL(
            '''UPDATE attempts_attempt
                  SET submission_date = (
                     SELECT max(history_date)
                      FROM attempts_historicalattempt
                     WHERE attempts_attempt.user_id = user_id
                       AND attempts_attempt.part_id = part_id
                 )
            '''
        ),
        migrations.AlterField(
            model_name='attempt',
            name='submission_date',
            field=models.DateTimeField(auto_now=True),
        ),
        migrations.AlterField(
            model_name='historicalattempt',
            name='submission_date',
            field=models.DateTimeField(blank=True, editable=False),
        ),
    ]

from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ('attempts', '0007_auto_20161004_0927'),
    ]

    operations = [
        migrations.AddField(
            model_name='attempt',
            name='submission_date',
            field=models.DateTimeField(null=True),
        ),
        migrations.AddField(
            model_name='historicalattempt',
            name='submission_date',
            field=models.DateTimeField(null=True),
        ),
        migrations.RunSQL(
            'UPDATE attempts_historicalattempt SET submission_date = history_date'
        ),
        migrations.RunSQL(
            '''UPDATE attempts_attempt
                  SET submission_date = subquery.submission_date
                 FROM (
                    SELECT user_id, part_id, max(history_date) AS submission_date
                      FROM attempts_historicalattempt
                     GROUP BY user_id, part_id
                 ) AS subquery
                WHERE attempts_attempt.user_id = subquery.user_id
                  AND attempts_attempt.part_id = subquery.part_id
            '''
        ),
        migrations.AlterField(
            model_name='attempt',
            name='submission_date',
            field=models.DateTimeField(auto_now=True),
        ),
        migrations.AlterField(
            model_name='historicalattempt',
            name='submission_date',
            field=models.DateTimeField(blank=True, editable=False),
        ),
    ]

Revert "Make migration SQLite compatible"
Revert "Make migration SQLite compatible"

This reverts commit 768d85cccb17c8757dd8d14dad220d0b87568264.

  from __future__ import unicode_literals
  
  from django.db import migrations, models
  
  
  class Migration(migrations.Migration):
  
      dependencies = [
          ('attempts', '0007_auto_20161004_0927'),
      ]
  
      operations = [
          migrations.AddField(
              model_name='attempt',
              name='submission_date',
              field=models.DateTimeField(null=True),
          ),
          migrations.AddField(
              model_name='historicalattempt',
              name='submission_date',
              field=models.DateTimeField(null=True),
          ),
          migrations.RunSQL(
              'UPDATE attempts_historicalattempt SET submission_date = history_date'
          ),
          migrations.RunSQL(
              '''UPDATE attempts_attempt
-                   SET submission_date = (
+                   SET submission_date = subquery.submission_date
-                      SELECT max(history_date)
+                  FROM (
+                     SELECT user_id, part_id, max(history_date) AS submission_date
                        FROM attempts_historicalattempt
+                      GROUP BY user_id, part_id
+                  ) AS subquery
-                      WHERE attempts_attempt.user_id = user_id
+                 WHERE attempts_attempt.user_id = subquery.user_id
-                        AND attempts_attempt.part_id = part_id
+                   AND attempts_attempt.part_id = subquery.part_id
-                  )
              '''
          ),
          migrations.AlterField(
              model_name='attempt',
              name='submission_date',
              field=models.DateTimeField(auto_now=True),
          ),
          migrations.AlterField(
              model_name='historicalattempt',
              name='submission_date',
              field=models.DateTimeField(blank=True, editable=False),
          ),
      ]
  
Revert "Make migration SQLite compatible"
## Code Before:
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ('attempts', '0007_auto_20161004_0927'),
    ]

    operations = [
        migrations.AddField(
            model_name='attempt',
            name='submission_date',
            field=models.DateTimeField(null=True),
        ),
        migrations.AddField(
            model_name='historicalattempt',
            name='submission_date',
            field=models.DateTimeField(null=True),
        ),
        migrations.RunSQL(
            'UPDATE attempts_historicalattempt SET submission_date = history_date'
        ),
        migrations.RunSQL(
            '''UPDATE attempts_attempt
                  SET submission_date = (
                     SELECT max(history_date)
                      FROM attempts_historicalattempt
                     WHERE attempts_attempt.user_id = user_id
                       AND attempts_attempt.part_id = part_id
                 )
            '''
        ),
        migrations.AlterField(
            model_name='attempt',
            name='submission_date',
            field=models.DateTimeField(auto_now=True),
        ),
        migrations.AlterField(
            model_name='historicalattempt',
            name='submission_date',
            field=models.DateTimeField(blank=True, editable=False),
        ),
    ]

## Instruction:
Revert "Make migration SQLite compatible"
## Code After:
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ('attempts', '0007_auto_20161004_0927'),
    ]

    operations = [
        migrations.AddField(
            model_name='attempt',
            name='submission_date',
            field=models.DateTimeField(null=True),
        ),
        migrations.AddField(
            model_name='historicalattempt',
            name='submission_date',
            field=models.DateTimeField(null=True),
        ),
        migrations.RunSQL(
            'UPDATE attempts_historicalattempt SET submission_date = history_date'
        ),
        migrations.RunSQL(
            '''UPDATE attempts_attempt
                  SET submission_date = subquery.submission_date
                 FROM (
                    SELECT user_id, part_id, max(history_date) AS submission_date
                      FROM attempts_historicalattempt
                     GROUP BY user_id, part_id
                 ) AS subquery
                WHERE attempts_attempt.user_id = subquery.user_id
                  AND attempts_attempt.part_id = subquery.part_id
            '''
        ),
        migrations.AlterField(
            model_name='attempt',
            name='submission_date',
            field=models.DateTimeField(auto_now=True),
        ),
        migrations.AlterField(
            model_name='historicalattempt',
            name='submission_date',
            field=models.DateTimeField(blank=True, editable=False),
        ),
    ]
