-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Draft: add custom 'migrateredis' management command
- Loading branch information
1 parent
26f08ce
commit 7a32e8b
Showing
7 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# Copyright (C) 2025 CVAT.ai Corporation | ||
# | ||
# SPDX-License-Identifier: MIT | ||
|
||
import importlib.util as importlib_util | ||
import sys | ||
from pathlib import Path | ||
from typing import cast | ||
|
||
from django.conf import settings | ||
from django.core.management.base import BaseCommand | ||
|
||
from cvat.apps.engine.models import RedisMigration | ||
from cvat.apps.engine.redis_migrations import BaseMigration | ||
|
||
|
||
def get_migration_class(module_name: str, file_path: Path) -> BaseMigration: | ||
spec = importlib_util.spec_from_file_location(module_name, file_path) | ||
module = importlib_util.module_from_spec(spec) | ||
spec.loader.exec_module(module) | ||
MigrationClass = getattr(module, "Migration", None) | ||
if not MigrationClass or not issubclass(MigrationClass, BaseMigration): | ||
raise Exception(f"Invalid migration: {file_path}") | ||
|
||
return MigrationClass | ||
|
||
|
||
class Command(BaseCommand): | ||
help = "Applies Redis migrations and records them in the database" | ||
|
||
def handle(self, *args, **options) -> None: | ||
migrations_dir = Path(settings.REDIS_MIGRATIONS_ROOT) | ||
applied_migrations = RedisMigration.objects.all().values_list("name") | ||
|
||
for migration_file in sorted(migrations_dir.glob("[0-9]*.py")): | ||
migration_name = migration_file.stem | ||
if migration_name in applied_migrations: | ||
continue | ||
|
||
migration = get_migration_class(module_name=migration_name, file_path=migration_file) | ||
try: | ||
migration.run() | ||
RedisMigration.objects.create(name=migration_name) | ||
self.stdout.write( | ||
self.style.SUCCESS(f"Successfully applied migration: {migration_name}") | ||
) | ||
except Exception as e: | ||
self.stderr.write(self.style.ERROR(f"Failed to apply migration: {migration_name}")) | ||
self.stderr.write(str(e)) | ||
break |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# Generated by Django 4.2.16 on 2025-01-03 17:08 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("engine", "0086_profile_has_analytics_access"), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name="RedisMigration", | ||
fields=[ | ||
( | ||
"id", | ||
models.AutoField( | ||
auto_created=True, primary_key=True, serialize=False, verbose_name="ID" | ||
), | ||
), | ||
("name", models.CharField(max_length=256)), | ||
("applied_date", models.DateTimeField(auto_now_add=True)), | ||
], | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from abc import ABCMeta, abstractmethod | ||
|
||
|
||
class BaseMigration(metaclass=ABCMeta): | ||
|
||
@staticmethod | ||
@abstractmethod | ||
def run() -> None: ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters