<filename>apps/logs/migrations/0053_clickhouse_add_import_batch_idx.py
# Generated by Django 3.2.12 on 2022-02-11 16:53
import sys

from django.conf import settings
from django.db import migrations

from logs.cubes import AccessLogCube, ch_backend


def clickhouse_add_import_batch_id_idx(apps, schema_editor):
    """
    We have no mechanism to migrate clickhouse, so we do it in a normal django database migration.
    This provides us at least with the mechanism of remembering which migration was applied.
    """
    if settings.CLICKHOUSE_SYNC_ACTIVE:
        with ch_backend.pool.get_client() as client:
            for idx in AccessLogCube.Clickhouse.indexes:
                table_name = ch_backend.cube_to_table_name(AccessLogCube)
                if client.execute(f"EXISTS TABLE {table_name}")[0][0]:
                    columns = client.execute(f'SHOW CREATE TABLE {table_name}')
                    desc = columns[0][0]
                    if f'INDEX {idx.name}' not in desc:
                        idx_def = idx.definition()
                        client.execute(f'ALTER TABLE {table_name} ADD {idx_def}')
                        client.execute(f'ALTER TABLE {table_name} MATERIALIZE INDEX {idx.name}')
                        print(f'Created Clickhouse index: {idx.name}')


class Migration(migrations.Migration):

    dependencies = [
        ('logs', '0052_remove_importbatch_system_created'),
    ]

    operations = [
        migrations.RunPython(clickhouse_add_import_batch_id_idx, migrations.RunPython.noop),
    ]

<filename>apps/logs/migrations/0053_clickhouse_add_import_batch_idx.py
# Generated by Django 3.2.12 on 2022-02-11 16:53
import sys

from django.conf import settings
from django.db import migrations

from logs.cubes import AccessLogCube, ch_backend


def clickhouse_add_import_batch_id_idx(apps, schema_editor):
    """
    We have no mechanism to migrate clickhouse, so we do it in a normal django database migration.
    This provides us at least with the mechanism of remembering which migration was applied.
    """
    if settings.CLICKHOUSE_SYNC_ACTIVE:
        with ch_backend.pool.get_client() as client:
            for idx in AccessLogCube.Clickhouse.indexes:
                table_name = ch_backend.cube_to_table_name(AccessLogCube)
                if client.execute(f"EXISTS TABLE {table_name}")[0][0]:
                    columns = client.execute(f'SHOW CREATE TABLE {table_name}')
                    desc = columns[0][0]
                    if f'INDEX {idx.name}' not in desc:
                        idx_def = idx.definition()
                        client.execute(f'ALTER TABLE {table_name} ADD {idx_def}')
                        client.execute(f'ALTER TABLE {table_name} MATERIALIZE INDEX {idx.name}')
                        print(f'Created Clickhouse index: {idx.name}')


class Migration(migrations.Migration):

    dependencies = [
        ('logs', '0052_remove_importbatch_system_created'),
    ]

    operations = [
        migrations.RunPython(clickhouse_add_import_batch_id_idx, migrations.RunPython.noop),
    ]

# Generated by Django 3.2.12 on 2022-02-11 16:53 We have no mechanism to migrate clickhouse, so we do it in a normal django database migration.     This provides us at least with the mechanism of remembering which migration was applied.