-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy path0052_auto_20241030_1757.py
58 lines (45 loc) · 1.57 KB
/
0052_auto_20241030_1757.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# Generated by Django 4.2.16 on 2024-10-30 17:57
from django.db import migrations
from django.db import connection
from django.conf import settings
from core.models import AsyncMigrationStatus
from core.redis import start_job_async_or_sync
import logging
logger = logging.getLogger(__name__)
migration_name = '0052_auto_20241030_1757'
if connection.vendor == 'sqlite':
sql_update_created_at = """
UPDATE tasks_tasklock
SET created_at = datetime(expire_at, %s);
"""
sql_params = (f'-{settings.TASK_LOCK_TTL} seconds',)
else:
sql_update_created_at = """
UPDATE tasks_tasklock
SET created_at = expire_at - INTERVAL %s;
"""
sql_params = ('%s seconds' % settings.TASK_LOCK_TTL,)
def forward_migration(migration_name):
migration = AsyncMigrationStatus.objects.create(
name=migration_name,
status=AsyncMigrationStatus.STATUS_STARTED,
)
logger.info(f'Start async migration {migration_name}')
with connection.cursor() as cursor:
cursor.execute(sql_update_created_at, sql_params)
migration.status = AsyncMigrationStatus.STATUS_FINISHED
migration.save()
logger.info(f'Async migration {migration_name} complete')
def forwards(apps, schema_editor):
# Dispatch migrations to rqworkers
start_job_async_or_sync(forward_migration, migration_name=migration_name)
def backwards(apps, schema_editor):
pass
class Migration(migrations.Migration):
atomic = False
dependencies = [
('tasks', '0051_tasklock_created_at'),
]
operations = [
migrations.RunPython(forwards, backwards),
]