old_contents: # -*- coding: utf-8 -*-
  # Generated by Django 1.11.13 on 2018-08-27 21:07
  from __future__ import unicode_literals
  
  import logging
  from django.db import migrations, connection
  from django.core.management.sql import emit_post_migrate_signal
  from osf.utils.migrations import batch_node_migrations
  
  logger = logging.getLogger(__name__)
  
  """
  This file exists separately from 0164_add_guardian_to_nodes.py for optimization purposes.
  Half of the reverse guardian migration is contained in this file.
  """
  
  # Reverse migration - Drop NodeGroupObjectPermission table - table gives node django groups
  # permissions to node
  drop_node_group_object_permission_table = """
      TRUNCATE TABLE osf_nodegroupobjectpermission;
      COMMIT;
      """
  
  # Reverse migration - Remove user membership in Node read/write/admin Django groups
  remove_users_from_node_django_groups = """
      DELETE FROM osf_osfuser_groups
        WHERE group_id IN (
          SELECT id FROM auth_group WHERE name LIKE '%node_%'
        );
      COMMIT;
      """
  
  # Reverse migration - Remove admin/write/read node django groups
  remove_node_django_groups = """
      ALTER TABLE auth_group DISABLE TRIGGER ALL;
      DELETE FROM auth_group WHERE name LIKE '%node_%';
      ALTER TABLE auth_group ENABLE TRIGGER ALL;
      COMMIT;
      """
  
  def reverse_node_guardian_migration(state, schema):
      migrations = [
          {'sql': drop_node_group_object_permission_table, 'description': 'Deleting all records in NodeGroupObjectPermission table.'},
          {'sql': remove_users_from_node_django_groups, 'description': 'Removing users from Node Django Groups.'},
          {'sql': remove_node_django_groups, 'description': 'Deleting Node Django Groups.'}
      ]
  
      batch_node_migrations(state, migrations)
      logger.info('Finished reversing guardian migration.')
      return
  
  def finalize_reverse_node_guardian_migration(state, schema):
      with connection.cursor() as cursor:
          cursor.execute(drop_node_group_object_permission_table)
          logger.info('Finished deleting NodeGroupObjectPermission table.')
          cursor.execute(remove_users_from_node_django_groups)
          logger.info('Finished removing users from guardian node django groups.')
          cursor.execute(remove_node_django_groups)
          logger.info('Finished removing guardian node django groups.')
  
  def post_migrate_signal(state, schema):
      # this is to make sure that the permissions created earlier exist!
      emit_post_migrate_signal(3, False, 'default')
      logger.info('Starting guardian/groups migration [SQL]:')
  
  
  class Migration(migrations.Migration):
  
      dependencies = [
          ('osf', '0161_guardian_direct_fks'),
      ]
  
      operations = [
          migrations.RunPython(post_migrate_signal, finalize_reverse_node_guardian_migration),
      ]
  
new_contents: # -*- coding: utf-8 -*-
  # Generated by Django 1.11.13 on 2018-08-27 21:07
  from __future__ import unicode_literals
  
  import logging
  from django.db import migrations, connection
  from django.core.management.sql import emit_post_migrate_signal
  
  logger = logging.getLogger(__name__)
  
  """
  This file exists separately from 0164_add_guardian_to_nodes.py for optimization purposes.
  Half of the reverse guardian migration is contained in this file.
  """
  
  # Reverse migration - Drop NodeGroupObjectPermission table - table gives node django groups
  # permissions to node
  drop_node_group_object_permission_table = """
      TRUNCATE TABLE osf_nodegroupobjectpermission;
      COMMIT;
      """
  
  # Reverse migration - Remove user membership in Node read/write/admin Django groups
  remove_users_from_node_django_groups = """
      DELETE FROM osf_osfuser_groups
        WHERE group_id IN (
          SELECT id FROM auth_group WHERE name LIKE '%node_%'
        );
      COMMIT;
      """
  
  # Reverse migration - Remove admin/write/read node django groups
  remove_node_django_groups = """
      ALTER TABLE auth_group DISABLE TRIGGER ALL;
      DELETE FROM auth_group WHERE name LIKE '%node_%';
      ALTER TABLE auth_group ENABLE TRIGGER ALL;
      COMMIT;
      """
  
  def finalize_reverse_node_guardian_migration(state, schema):
      with connection.cursor() as cursor:
          cursor.execute(drop_node_group_object_permission_table)
          logger.info('Finished deleting NodeGroupObjectPermission table.')
          cursor.execute(remove_users_from_node_django_groups)
          logger.info('Finished removing users from guardian node django groups.')
          cursor.execute(remove_node_django_groups)
          logger.info('Finished removing guardian node django groups.')
  
  def post_migrate_signal(state, schema):
      # this is to make sure that the permissions created earlier exist!
      emit_post_migrate_signal(3, False, 'default')
      logger.info('Starting guardian/groups migration [SQL]:')
  
  
  class Migration(migrations.Migration):
  
      dependencies = [
          ('osf', '0161_guardian_direct_fks'),
      ]
  
      operations = [
          migrations.RunPython(post_migrate_signal, finalize_reverse_node_guardian_migration),
      ]
  
subject: Remove old reverse_node_guardian_migration.