
import pickle
from django.db import migrations, models
import evennia.utils.picklefield
from evennia.utils.utils import to_bytes

def migrate_serverconf(apps, schema_editor):
    """
    Move server conf from a custom binary field into a PickleObjectField
    """
    ServerConfig = apps.get_model("server", "ServerConfig")
    for conf in ServerConfig.objects.all():
        value = pickle.loads(to_bytes(conf.db_value))
        conf.db_value2 = value
        conf.save()


class Migration(migrations.Migration):

    dependencies = [
        ('server', '0001_initial'),
    ]

    operations = [
        migrations.AddField(
            model_name='serverconfig',
            name='db_value2',
            field=evennia.utils.picklefield.PickledObjectField(help_text='The data returned when the config value is accessed. Must be written as a Python literal if editing through the admin interface. Attribute values which are not Python literals cannot be edited through the admin interface.', null=True, verbose_name='value'),
        ),
        # migrate data
        migrations.RunPython(migrate_serverconf, migrations.RunPython.noop),


    ]


import pickle
from django.db import migrations, models
import evennia.utils.picklefield
from evennia.utils.utils import to_bytes, to_str

def migrate_serverconf(apps, schema_editor):
    """
    Move server conf from a custom binary field into a PickleObjectField
    """
    ServerConfig = apps.get_model("server", "ServerConfig")
    for conf in ServerConfig.objects.all():
        value = pickle.loads(to_bytes(conf.db_value))
        conf.db_value2 = to_str(value)
        conf.save(update_fields=["db_value2"])


class Migration(migrations.Migration):

    dependencies = [
        ('server', '0001_initial'),
    ]

    operations = [
        migrations.AddField(
            model_name='serverconfig',
            name='db_value2',
            field=evennia.utils.picklefield.PickledObjectField(help_text='The data returned when the config value is accessed. Must be written as a Python literal if editing through the admin interface. Attribute values which are not Python literals cannot be edited through the admin interface.', null=True, verbose_name='value'),
        ),
        # migrate data
        migrations.RunPython(migrate_serverconf, migrations.RunPython.noop),


    ]

Fix migration for various situations
Fix migration for various situations

  
  import pickle
  from django.db import migrations, models
  import evennia.utils.picklefield
- from evennia.utils.utils import to_bytes
+ from evennia.utils.utils import to_bytes, to_str
  
  def migrate_serverconf(apps, schema_editor):
      """
      Move server conf from a custom binary field into a PickleObjectField
      """
      ServerConfig = apps.get_model("server", "ServerConfig")
      for conf in ServerConfig.objects.all():
          value = pickle.loads(to_bytes(conf.db_value))
-         conf.db_value2 = value
+         conf.db_value2 = to_str(value)
-         conf.save()
+         conf.save(update_fields=["db_value2"])
  
  
  class Migration(migrations.Migration):
  
      dependencies = [
          ('server', '0001_initial'),
      ]
  
      operations = [
          migrations.AddField(
              model_name='serverconfig',
              name='db_value2',
              field=evennia.utils.picklefield.PickledObjectField(help_text='The data returned when the config value is accessed. Must be written as a Python literal if editing through the admin interface. Attribute values which are not Python literals cannot be edited through the admin interface.', null=True, verbose_name='value'),
          ),
          # migrate data
          migrations.RunPython(migrate_serverconf, migrations.RunPython.noop),
  
  
      ]
  
Fix migration for various situations
## Code Before:

import pickle
from django.db import migrations, models
import evennia.utils.picklefield
from evennia.utils.utils import to_bytes

def migrate_serverconf(apps, schema_editor):
    """
    Move server conf from a custom binary field into a PickleObjectField
    """
    ServerConfig = apps.get_model("server", "ServerConfig")
    for conf in ServerConfig.objects.all():
        value = pickle.loads(to_bytes(conf.db_value))
        conf.db_value2 = value
        conf.save()


class Migration(migrations.Migration):

    dependencies = [
        ('server', '0001_initial'),
    ]

    operations = [
        migrations.AddField(
            model_name='serverconfig',
            name='db_value2',
            field=evennia.utils.picklefield.PickledObjectField(help_text='The data returned when the config value is accessed. Must be written as a Python literal if editing through the admin interface. Attribute values which are not Python literals cannot be edited through the admin interface.', null=True, verbose_name='value'),
        ),
        # migrate data
        migrations.RunPython(migrate_serverconf, migrations.RunPython.noop),


    ]

## Instruction:
Fix migration for various situations
## Code After:

import pickle
from django.db import migrations, models
import evennia.utils.picklefield
from evennia.utils.utils import to_bytes, to_str

def migrate_serverconf(apps, schema_editor):
    """
    Move server conf from a custom binary field into a PickleObjectField
    """
    ServerConfig = apps.get_model("server", "ServerConfig")
    for conf in ServerConfig.objects.all():
        value = pickle.loads(to_bytes(conf.db_value))
        conf.db_value2 = to_str(value)
        conf.save(update_fields=["db_value2"])


class Migration(migrations.Migration):

    dependencies = [
        ('server', '0001_initial'),
    ]

    operations = [
        migrations.AddField(
            model_name='serverconfig',
            name='db_value2',
            field=evennia.utils.picklefield.PickledObjectField(help_text='The data returned when the config value is accessed. Must be written as a Python literal if editing through the admin interface. Attribute values which are not Python literals cannot be edited through the admin interface.', null=True, verbose_name='value'),
        ),
        # migrate data
        migrations.RunPython(migrate_serverconf, migrations.RunPython.noop),


    ]
