from django.conf import settings
from django.core.exceptions import ObjectDoesNotExist
from django.db import migrations, models
import django.db.models.deletion

import logging
logger = logging.getLogger(__name__)


class Migration(migrations.Migration):

    def backfill_learner(apps, schema_editor):
        """
        Use the 'student_id' to link stacks to the User model.
        """
        Stack = apps.get_model("hastexo", "Stack")
        AnonymousUserId = apps.get_model("student", "AnonymousUserId")
        for stack in Stack.objects.all():
            try:
                stack.learner = AnonymousUserId.objects.get(
                    anonymous_user_id=stack.student_id).user
                stack.save(update_fields=['learner'])
            except ObjectDoesNotExist:
                logger.warning('Unable to link stack to user: '
                               f'{stack.name}')

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        ('hastexo', '0009_add_null_true_for_key_and_password'),
    ]

    operations = [
        migrations.AddField(
            model_name='stack',
            name='learner',
            field=models.ForeignKey(
                db_constraint=False,
                null=True,
                on_delete=django.db.models.deletion.PROTECT,
                to=settings.AUTH_USER_MODEL),
        ),
        migrations.RunPython(backfill_learner),
    ]

from django.conf import settings
from django.core.exceptions import ObjectDoesNotExist
from django.db import migrations, models
import django.db.models.deletion

import logging
logger = logging.getLogger(__name__)


class Migration(migrations.Migration):

    def backfill_learner(apps, schema_editor):
        """
        Use the 'student_id' to link stacks to the User model.
        """
        Stack = apps.get_model("hastexo", "Stack")
        AnonymousUserId = apps.get_model("student", "AnonymousUserId")
        for stack in Stack.objects.all():
            try:
                stack.learner = AnonymousUserId.objects.get(
                    anonymous_user_id=stack.student_id).user
                stack.save(update_fields=['learner'])
            except ObjectDoesNotExist:
                logger.warning('Unable to link stack to user: '
                               f'{stack.name}')

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        ('hastexo', '0009_add_null_true_for_key_and_password'),
    ]

    operations = [
        migrations.AddField(
            model_name='stack',
            name='learner',
            field=models.ForeignKey(
                db_constraint=False,
                null=True,
                on_delete=django.db.models.deletion.PROTECT,
                to=settings.AUTH_USER_MODEL),
        ),
        migrations.RunPython(backfill_learner),
        migrations.AlterField(
            model_name='stack',
            name='learner',
            field=models.ForeignKey(
                db_constraint=True,
                null=True,
                on_delete=django.db.models.deletion.PROTECT,
                to=settings.AUTH_USER_MODEL),
        ),
    ]

Apply additional fix to add_user_foreign_key migration
Apply additional fix to add_user_foreign_key migration

The hack in 583fb729b1e201c830579345dca5beca4b131006 modified
0010_add_user_foreign_key in such a way that it ended up *not* setting
a database constraint when it should have.

Enable the database-enforced constraint in the right place.

Co-authored-by: Florian Haas <73262ad0334ab37227b2f7a0205f51db1e606681@cleura.com>

  from django.conf import settings
  from django.core.exceptions import ObjectDoesNotExist
  from django.db import migrations, models
  import django.db.models.deletion
  
  import logging
  logger = logging.getLogger(__name__)
  
  
  class Migration(migrations.Migration):
  
      def backfill_learner(apps, schema_editor):
          """
          Use the 'student_id' to link stacks to the User model.
          """
          Stack = apps.get_model("hastexo", "Stack")
          AnonymousUserId = apps.get_model("student", "AnonymousUserId")
          for stack in Stack.objects.all():
              try:
                  stack.learner = AnonymousUserId.objects.get(
                      anonymous_user_id=stack.student_id).user
                  stack.save(update_fields=['learner'])
              except ObjectDoesNotExist:
                  logger.warning('Unable to link stack to user: '
                                 f'{stack.name}')
  
      dependencies = [
          migrations.swappable_dependency(settings.AUTH_USER_MODEL),
          ('hastexo', '0009_add_null_true_for_key_and_password'),
      ]
  
      operations = [
          migrations.AddField(
              model_name='stack',
              name='learner',
              field=models.ForeignKey(
                  db_constraint=False,
                  null=True,
                  on_delete=django.db.models.deletion.PROTECT,
                  to=settings.AUTH_USER_MODEL),
          ),
          migrations.RunPython(backfill_learner),
+         migrations.AlterField(
+             model_name='stack',
+             name='learner',
+             field=models.ForeignKey(
+                 db_constraint=True,
+                 null=True,
+                 on_delete=django.db.models.deletion.PROTECT,
+                 to=settings.AUTH_USER_MODEL),
+         ),
      ]
  
Apply additional fix to add_user_foreign_key migration
## Code Before:
from django.conf import settings
from django.core.exceptions import ObjectDoesNotExist
from django.db import migrations, models
import django.db.models.deletion

import logging
logger = logging.getLogger(__name__)


class Migration(migrations.Migration):

    def backfill_learner(apps, schema_editor):
        """
        Use the 'student_id' to link stacks to the User model.
        """
        Stack = apps.get_model("hastexo", "Stack")
        AnonymousUserId = apps.get_model("student", "AnonymousUserId")
        for stack in Stack.objects.all():
            try:
                stack.learner = AnonymousUserId.objects.get(
                    anonymous_user_id=stack.student_id).user
                stack.save(update_fields=['learner'])
            except ObjectDoesNotExist:
                logger.warning('Unable to link stack to user: '
                               f'{stack.name}')

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        ('hastexo', '0009_add_null_true_for_key_and_password'),
    ]

    operations = [
        migrations.AddField(
            model_name='stack',
            name='learner',
            field=models.ForeignKey(
                db_constraint=False,
                null=True,
                on_delete=django.db.models.deletion.PROTECT,
                to=settings.AUTH_USER_MODEL),
        ),
        migrations.RunPython(backfill_learner),
    ]

## Instruction:
Apply additional fix to add_user_foreign_key migration
## Code After:
from django.conf import settings
from django.core.exceptions import ObjectDoesNotExist
from django.db import migrations, models
import django.db.models.deletion

import logging
logger = logging.getLogger(__name__)


class Migration(migrations.Migration):

    def backfill_learner(apps, schema_editor):
        """
        Use the 'student_id' to link stacks to the User model.
        """
        Stack = apps.get_model("hastexo", "Stack")
        AnonymousUserId = apps.get_model("student", "AnonymousUserId")
        for stack in Stack.objects.all():
            try:
                stack.learner = AnonymousUserId.objects.get(
                    anonymous_user_id=stack.student_id).user
                stack.save(update_fields=['learner'])
            except ObjectDoesNotExist:
                logger.warning('Unable to link stack to user: '
                               f'{stack.name}')

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        ('hastexo', '0009_add_null_true_for_key_and_password'),
    ]

    operations = [
        migrations.AddField(
            model_name='stack',
            name='learner',
            field=models.ForeignKey(
                db_constraint=False,
                null=True,
                on_delete=django.db.models.deletion.PROTECT,
                to=settings.AUTH_USER_MODEL),
        ),
        migrations.RunPython(backfill_learner),
        migrations.AlterField(
            model_name='stack',
            name='learner',
            field=models.ForeignKey(
                db_constraint=True,
                null=True,
                on_delete=django.db.models.deletion.PROTECT,
                to=settings.AUTH_USER_MODEL),
        ),
    ]
