Skip to content

Commit

Permalink
Pass connection to migration class && do not use key prefix in the set
Browse files Browse the repository at this point in the history
  • Loading branch information
Marishka17 committed Feb 4, 2025
1 parent 98d0037 commit b2b7a1b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 14 deletions.
15 changes: 9 additions & 6 deletions cvat/apps/redis_handler/migration_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,20 @@ class AppliedMigration:
)

def get_key(self) -> str:
return f"{self.KEY_PREFIX}{self.app_label}.{self.name}"
return f"{self.app_label}.{self.name}"

def get_key_with_prefix(self) -> str:
return self.KEY_PREFIX + self.get_key()

def to_dict(self) -> dict[str, Any]:
return {
"applied_date": self.applied_date.timestamp(),
}

def save(self, *, connection: Redis) -> None:
migration_key = self.get_key()

with connection.pipeline() as pipe:
pipe.hset(migration_key, mapping=self.to_dict())
pipe.sadd(self.SET_KEY, migration_key)
pipe.hset(self.get_key_with_prefix(), mapping=self.to_dict())
pipe.sadd(self.SET_KEY, self.get_key())
pipe.execute()


Expand Down Expand Up @@ -104,7 +105,9 @@ def _init_unapplied_migrations(self):
)
for migration_name in app_unapplied_migrations:
MigrationClass = self.get_migration_class(app_config.name, migration_name)
self._unapplied_migrations.append(MigrationClass(migration_name, app_config.label))
self._unapplied_migrations.append(
MigrationClass(migration_name, app_config.label, connection=self._connection)
)

def get_migration_class(self, app_name: str, migration_name: str) -> BaseMigration:
migration_module_path = ".".join([app_name, self.REDIS_MIGRATIONS_DIR_NAME, migration_name])
Expand Down
9 changes: 1 addition & 8 deletions cvat/apps/redis_handler/redis_migrations/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,16 @@
# SPDX-License-Identifier: MIT

from abc import ABCMeta, abstractmethod
from typing import ClassVar

from attrs import define, field, validators
from django.conf import settings
from redis import Redis


@define
class BaseMigration(metaclass=ABCMeta):
CONNECTION: ClassVar[Redis] = Redis(
host=settings.REDIS_INMEM_SETTINGS["HOST"],
port=settings.REDIS_INMEM_SETTINGS["PORT"],
db=settings.REDIS_INMEM_SETTINGS["DB"],
password=settings.REDIS_INMEM_SETTINGS["PASSWORD"],
)
name: str = field(validator=[validators.instance_of(str)])
app_label: str = field(validator=[validators.instance_of(str)])
connection: Redis = field(validator=[validators.instance_of(Redis)], kw_only=True)

@classmethod
@abstractmethod
Expand Down

0 comments on commit b2b7a1b

Please sign in to comment.