Bin
2025-12-17 611bfe34c3c96199eaaf6cf9e41a75892e44e879
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
# Generated by Django 3.2.20 on 2023-11-24 12:38
 
import logging
from django.db import migrations
from core.redis import start_job_async_or_sync
 
logger = logging.getLogger(__name__)
 
def async_index_creation(db_alias):
    from django.db import connections
    create_index_sql_1 = (
        'CREATE INDEX CONCURRENTLY IF NOT EXISTS task_comple_project_0bc0be_idx '
        'ON task_completion (project_id, completed_by_id);'
    )
 
    create_index_sql_2 = (
        'CREATE INDEX CONCURRENTLY IF NOT EXISTS task_comple_task_id_a6bdec_idx '
        'ON task_completion (task_id, id);'
    )
 
    with connections[db_alias].schema_editor(atomic=False) as schema_editor:
        schema_editor.execute(create_index_sql_1)
        schema_editor.execute(create_index_sql_2)
        logger.info('Indexes created concurrently on annotation model')
 
def forwards(apps, schema_editor):
    database_vendor = schema_editor.connection.vendor
    if database_vendor != 'postgresql':
        logger.info(f'Database vendor: {database_vendor}')
        logger.info('Skipping async index creation for non-PostgreSQL databases')
        return
 
    # Schedule the index creation job asynchronously using RQ worker
    db_alias = schema_editor.connection.alias
    start_job_async_or_sync(async_index_creation, db_alias=db_alias)
 
def backwards(apps, schema_editor):
    pass
 
class Migration(migrations.Migration):
    atomic = False
 
    dependencies = [
        ('tasks', '0044_auto_20230907_0155'),
    ]
 
    operations = [
        migrations.RunPython(forwards, backwards)
    ]