Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Mogost committed Aug 14, 2024
1 parent 9ca8c1c commit e4726ac
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions constance/migrations/0003_drop_pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
import pickle
from base64 import b64decode

from django.core.cache import caches
from django.db import migrations

from constance import settings
from constance import utils
from constance.serializers import dumps

logger = logging.getLogger(__name__)
Expand All @@ -15,8 +18,30 @@ def migrate_pickled_data(apps, schema_editor) -> None:
for constance in Constance.objects.exclude(value=None):
constance.value = dumps(pickle.loads(b64decode(constance.value.encode()))) # noqa: S301
constance.save(update_fields=['value'])
# TODO: Clear caches
# TODO: Migrate in redis

if settings.DATABASE_CACHE_BACKEND:
cache = caches[settings.DATABASE_CACHE_BACKEND]
cache.delete_many(list(settings.CONFIG.keys()))

if settings.BACKEND in ('constance.backends.redisd.RedisBackend', 'constance.backends.redisd.CachingRedisBackend'):
import redis

_prefix = settings.REDIS_PREFIX
connection_cls = settings.REDIS_CONNECTION_CLASS
if connection_cls is not None:
_rd = utils.import_module_attr(connection_cls)()
else:
if isinstance(settings.REDIS_CONNECTION, str):
_rd = redis.from_url(settings.REDIS_CONNECTION)
else:
_rd = redis.Redis(**settings.REDIS_CONNECTION)
redis_migrated_data = {}
for key in settings.CONFIG:
prefixed_key = f'{_prefix}{key}'
value = _rd.get(prefixed_key)
redis_migrated_data[prefixed_key] = dumps(pickle.loads(value)) # noqa: S301
for prefixed_key, value in redis_migrated_data.items():
_rd.set(prefixed_key, value)


class Migration(migrations.Migration):
Expand Down

0 comments on commit e4726ac

Please sign in to comment.