# Generated by Django 3.0.8 on 2020-08-08 14:59
import logging

import django.contrib.postgres.indexes
import django.contrib.postgres.search
import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models, transaction
from django.db.utils import ProgrammingError


logger = logging.getLogger(__name__)


def create_f_unaccent(apps, schema_editor): # pylint: disable=unused-argument
	connection = schema_editor.connection
	if connection.vendor != 'postgresql':
		return
	cursor = connection.cursor()
	try:
		create_command = """
CREATE OR REPLACE FUNCTION f_unaccent(text)
  RETURNS text AS
$func$
SELECT public.unaccent('public.unaccent', $1)
$func$ LANGUAGE sql IMMUTABLE;
		"""
		cursor.execute(create_command)
	except ProgrammingError as e:
		logger.warning(str(e))
		logger.warning("SQL command failed, please run manually:\n%s\n" % create_command)
		transaction.rollback()


def create_extension_unaccent(apps, schema_editor): # pylint: disable=unused-argument
	connection = schema_editor.connection
	if connection.vendor != 'postgresql':
		return
	cursor = connection.cursor()
	try:
		create_command = """
		CREATE EXTENSION IF NOT EXISTS unaccent
		"""
		cursor.execute(create_command)
	except ProgrammingError as e:
		logger.warning(str(e))
		logger.warning("SQL command failed, please run manually:\n%s\n" % create_command)
		transaction.rollback()


class Migration(migrations.Migration):

	initial = True

	dependencies = [
		migrations.swappable_dependency(settings.AUTH_USER_MODEL),
		('contenttypes', '0002_remove_content_type_name'),
	]

	operations = [
		migrations.RunPython(create_extension_unaccent, migrations.RunPython.noop),
		migrations.RunPython(create_f_unaccent, migrations.RunPython.noop),
		migrations.CreateModel(
			name='SearchIndex',
			fields=[
				('id', models.AutoField(auto_created=True, primary_key=True, serialize=False)),
				('object_id', models.PositiveIntegerField()),
				('created', models.DateTimeField(blank=True, null=True)),
				('updated', models.DateTimeField(blank=True, null=True)),
				('authors_name', models.CharField(max_length=255)),
				('language_code', models.CharField(max_length=10)),
				('title', models.TextField()),
				('document', models.TextField()),
				('comments', models.TextField()),
				('document_search_vector', django.contrib.postgres.search.SearchVectorField()),
				('comments_search_vector', django.contrib.postgres.search.SearchVectorField()),
				('combined_search_vector', django.contrib.postgres.search.SearchVectorField()),
				('author', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to=settings.AUTH_USER_MODEL)),
				('content_type', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='contenttypes.ContentType')),
			],
		),
		migrations.AddIndex(
			model_name='searchindex',
			index=django.contrib.postgres.indexes.GinIndex(fields=['document_search_vector'], name='fulltext_se_documen_afe45c_gin'),
		),
		migrations.AddIndex(
			model_name='searchindex',
			index=django.contrib.postgres.indexes.GinIndex(fields=['comments_search_vector'], name='fulltext_se_comment_721841_gin'),
		),
		migrations.AddIndex(
			model_name='searchindex',
			index=django.contrib.postgres.indexes.GinIndex(fields=['combined_search_vector'], name='fulltext_se_combine_ac7e09_gin'),
		),
	]
