
from django.db import migrations
from django.db.utils import OperationalError, ProgrammingError

from part.models import Part
from stdimage.utils import render_variations


def create_thumbnails(apps, schema_editor):
    """
    Create thumbnails for all existing Part images.
    """

    try:
        for part in Part.objects.all():
            # Render thumbnail for each existing Part 
            if part.image:
                try:
                    part.image.render_variations()
                except FileNotFoundError:
                    print("Missing image:", part.image())
                    # The image is missing, so clear the field
                    part.image = None
                    part.save()
                    
    except (OperationalError, ProgrammingError):
        # Migrations have not yet been applied - table does not exist
        print("Could not generate Part thumbnails")

class Migration(migrations.Migration):

    dependencies = [
        ('part', '0033_auto_20200404_0445'),
    ]

    operations = [
        migrations.RunPython(create_thumbnails),
    ]


from django.db import migrations


def create_thumbnails(apps, schema_editor):
    """
    Create thumbnails for all existing Part images.

    Note: This functionality is now performed in apps.py, 
    as running the thumbnail script here caused too many database level errors.

    This migration is left here to maintain the database migration history

    """
    pass


class Migration(migrations.Migration):

    dependencies = [
        ('part', '0033_auto_20200404_0445'),
    ]

    operations = [
        migrations.RunPython(create_thumbnails, reverse_code=create_thumbnails),
    ]

Remove the problematic migration entirely
Remove the problematic migration entirely

- The thumbnail check code is run every time the server is started anyway!

  
  from django.db import migrations
- from django.db.utils import OperationalError, ProgrammingError
- 
- from part.models import Part
- from stdimage.utils import render_variations
  
  
  def create_thumbnails(apps, schema_editor):
      """
      Create thumbnails for all existing Part images.
+ 
+     Note: This functionality is now performed in apps.py, 
+     as running the thumbnail script here caused too many database level errors.
+ 
+     This migration is left here to maintain the database migration history
+ 
      """
+     pass
  
-     try:
-         for part in Part.objects.all():
-             # Render thumbnail for each existing Part 
-             if part.image:
-                 try:
-                     part.image.render_variations()
-                 except FileNotFoundError:
-                     print("Missing image:", part.image())
-                     # The image is missing, so clear the field
-                     part.image = None
-                     part.save()
-                     
-     except (OperationalError, ProgrammingError):
-         # Migrations have not yet been applied - table does not exist
-         print("Could not generate Part thumbnails")
  
  class Migration(migrations.Migration):
  
      dependencies = [
          ('part', '0033_auto_20200404_0445'),
      ]
  
      operations = [
-         migrations.RunPython(create_thumbnails),
+         migrations.RunPython(create_thumbnails, reverse_code=create_thumbnails),
      ]
  
Remove the problematic migration entirely
## Code Before:

from django.db import migrations
from django.db.utils import OperationalError, ProgrammingError

from part.models import Part
from stdimage.utils import render_variations


def create_thumbnails(apps, schema_editor):
    """
    Create thumbnails for all existing Part images.
    """

    try:
        for part in Part.objects.all():
            # Render thumbnail for each existing Part 
            if part.image:
                try:
                    part.image.render_variations()
                except FileNotFoundError:
                    print("Missing image:", part.image())
                    # The image is missing, so clear the field
                    part.image = None
                    part.save()
                    
    except (OperationalError, ProgrammingError):
        # Migrations have not yet been applied - table does not exist
        print("Could not generate Part thumbnails")

class Migration(migrations.Migration):

    dependencies = [
        ('part', '0033_auto_20200404_0445'),
    ]

    operations = [
        migrations.RunPython(create_thumbnails),
    ]

## Instruction:
Remove the problematic migration entirely
## Code After:

from django.db import migrations


def create_thumbnails(apps, schema_editor):
    """
    Create thumbnails for all existing Part images.

    Note: This functionality is now performed in apps.py, 
    as running the thumbnail script here caused too many database level errors.

    This migration is left here to maintain the database migration history

    """
    pass


class Migration(migrations.Migration):

    dependencies = [
        ('part', '0033_auto_20200404_0445'),
    ]

    operations = [
        migrations.RunPython(create_thumbnails, reverse_code=create_thumbnails),
    ]
