from __future__ import unicode_literals

from django.db import migrations, models
from django.apps import apps
from django.db.models import F

from django.db.backends.postgresql_psycopg2.schema import DatabaseSchemaEditor
from django.db.migrations.state import StateApps

def copy_email_field(apps: StateApps, schema_editor: DatabaseSchemaEditor) -> None:
    UserProfile = apps.get_model('zerver', 'UserProfile')
    UserProfile.objects.all().update(delivery_email=F('email'))


class Migration(migrations.Migration):

    dependencies = [
        ('zerver', '0173_support_seat_based_plans'),
    ]

    operations = [
        migrations.AddField(
            model_name='userprofile',
            name='delivery_email',
            field=models.EmailField(db_index=True, default='', max_length=254),
            preserve_default=False,
        ),
        migrations.RunPython(copy_email_field,
                             reverse_code=migrations.RunPython.noop),
    ]

from __future__ import unicode_literals

from django.db import migrations, models
from django.apps import apps
from django.db.models import F

from django.db.backends.postgresql_psycopg2.schema import DatabaseSchemaEditor
from django.db.migrations.state import StateApps

def copy_email_field(apps: StateApps, schema_editor: DatabaseSchemaEditor) -> None:
    UserProfile = apps.get_model('zerver', 'UserProfile')
    UserProfile.objects.all().update(delivery_email=F('email'))


class Migration(migrations.Migration):
    atomic = False

    dependencies = [
        ('zerver', '0173_support_seat_based_plans'),
    ]

    operations = [
        migrations.AddField(
            model_name='userprofile',
            name='delivery_email',
            field=models.EmailField(db_index=True, default='', max_length=254),
            preserve_default=False,
        ),
        migrations.RunPython(copy_email_field,
                             reverse_code=migrations.RunPython.noop),
    ]

Disable atomic for delivery_email migration.
migrations: Disable atomic for delivery_email migration.

I'm not sure theoretically why this should be required only for some
installations, but these articles all suggest the root problem is
doing these two migrations together atomically (creating the field and
setting a value for it), so the right answer is to declare the
migration as not atomic:

https://stackoverflow.com/questions/12838111/django-db-migrations-cannot-alter-table-because-it-has-pending-trigger-events
https://confluence.atlassian.com/confkb/upgrade-failed-with-the-error-message-error-cannot-alter-table-content-because-it-has-pending-trigger-events-747606853.html

  from __future__ import unicode_literals
  
  from django.db import migrations, models
  from django.apps import apps
  from django.db.models import F
  
  from django.db.backends.postgresql_psycopg2.schema import DatabaseSchemaEditor
  from django.db.migrations.state import StateApps
  
  def copy_email_field(apps: StateApps, schema_editor: DatabaseSchemaEditor) -> None:
      UserProfile = apps.get_model('zerver', 'UserProfile')
      UserProfile.objects.all().update(delivery_email=F('email'))
  
  
  class Migration(migrations.Migration):
+     atomic = False
  
      dependencies = [
          ('zerver', '0173_support_seat_based_plans'),
      ]
  
      operations = [
          migrations.AddField(
              model_name='userprofile',
              name='delivery_email',
              field=models.EmailField(db_index=True, default='', max_length=254),
              preserve_default=False,
          ),
          migrations.RunPython(copy_email_field,
                               reverse_code=migrations.RunPython.noop),
      ]
  
Disable atomic for delivery_email migration.
## Code Before:
from __future__ import unicode_literals

from django.db import migrations, models
from django.apps import apps
from django.db.models import F

from django.db.backends.postgresql_psycopg2.schema import DatabaseSchemaEditor
from django.db.migrations.state import StateApps

def copy_email_field(apps: StateApps, schema_editor: DatabaseSchemaEditor) -> None:
    UserProfile = apps.get_model('zerver', 'UserProfile')
    UserProfile.objects.all().update(delivery_email=F('email'))


class Migration(migrations.Migration):

    dependencies = [
        ('zerver', '0173_support_seat_based_plans'),
    ]

    operations = [
        migrations.AddField(
            model_name='userprofile',
            name='delivery_email',
            field=models.EmailField(db_index=True, default='', max_length=254),
            preserve_default=False,
        ),
        migrations.RunPython(copy_email_field,
                             reverse_code=migrations.RunPython.noop),
    ]

## Instruction:
Disable atomic for delivery_email migration.
## Code After:
from __future__ import unicode_literals

from django.db import migrations, models
from django.apps import apps
from django.db.models import F

from django.db.backends.postgresql_psycopg2.schema import DatabaseSchemaEditor
from django.db.migrations.state import StateApps

def copy_email_field(apps: StateApps, schema_editor: DatabaseSchemaEditor) -> None:
    UserProfile = apps.get_model('zerver', 'UserProfile')
    UserProfile.objects.all().update(delivery_email=F('email'))


class Migration(migrations.Migration):
    atomic = False

    dependencies = [
        ('zerver', '0173_support_seat_based_plans'),
    ]

    operations = [
        migrations.AddField(
            model_name='userprofile',
            name='delivery_email',
            field=models.EmailField(db_index=True, default='', max_length=254),
            preserve_default=False,
        ),
        migrations.RunPython(copy_email_field,
                             reverse_code=migrations.RunPython.noop),
    ]
