# Generated by Django 2.2.23 on 2021-06-14 13:47

from django.conf import settings
from django.db import migrations, models, transaction
from django.db.utils import ProgrammingError


class Migration(migrations.Migration):
    dependencies = [
        ('auth', '0011_update_proxy_permissions'),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        ('core', '0050_ocrmodel_parent'),
    ]

    operations = [
        migrations.AddField(
            model_name='document',
            name='shared_with_groups',
            field=models.ManyToManyField(blank=True,
                                         related_name='shared_documents',
                                         to='auth.Group',
                                         verbose_name='Share with teams'),
        ),
        migrations.AddField(
            model_name='document',
            name='shared_with_users',
            field=models.ManyToManyField(blank=True,
                                         related_name='shared_documents',
                                         to='users.User',
                                         verbose_name='Share with users'),
        )
    ]

    def apply(self, project_state, schema_editor, collect_sql=False):
        try:
            # check actual db state
            with transaction.atomic():
                with schema_editor.connection.cursor() as cursor:
                    cursor.execute('SELECT id from core_document_shared_with_users LIMIT 1')
        except ProgrammingError:
            pass
        else:
            # they already exists
            self.operations = []

        return super().apply(project_state, schema_editor, collect_sql=collect_sql)

    def unapply(*args, **kwargs):
        pass
