# -*- coding: utf-8 -*-
# Generated by Django 1.11.9 on 2018-03-21 13:00
from __future__ import unicode_literals

from django.db import migrations

from weblate.memory.storage import setup_index


def create_whoosh(apps, schema_editor):
    # Create the index
    index = setup_index()

    # Migrate any existing database entries to the fulltext
    writer = index.writer()
    Memory = apps.get_model('memory', 'Memory')
    for memory in Memory.objects.all():
        writer.add_document(
            source_language=memory.source_language.code,
            target_language=memory.target_language.code,
            source=memory.source,
            target=memory.target,
            origin=memory.origin,
        )
    writer.commit()


class Migration(migrations.Migration):

    dependencies = [
        ('memory', '0001_initial'),
    ]

    operations = [
        migrations.RunPython(create_whoosh, reverse_code=create_whoosh),
    ]
