<commit_before># -*- coding: utf-8 -*-
from south.utils import datetime_utils as datetime
from south.db import db
from south.v2 import DataMigration
from django.db import models
from sentry.utils.query import RangeQuerySetWrapperWithProgressBar


class Migration(DataMigration):

    def forwards(self, orm):
        "Write your forwards methods here."
        release_environments = orm.ReleaseEnvironment.objects.filter(
            organization_id__isnull=True
        )
        for re in RangeQuerySetWrapperWithProgressBar(release_environments):
            try:
                org_id = orm.Project.objects.filter(
                    id=re.project_id
                ).values_list('organization_id', flat=True)[0]
            except IndexError:
                pass
            else:
                orm.ReleaseEnvironment.objects.filter(
                    id=re.id
                ).update(
                    organization_id=org_id
                )


        release_files = orm.ReleaseFile.objects.filter(
            organization__isnull=True
        ).select_related('project')
        for rf in RangeQuerySetWrapperWithProgressBar(release_files):
            orm.ReleaseFile.objects.filter(
                id=rf.id
            ).update(organization=rf.project.organization_id)

    def backwards(self, orm):
        "Write your backwards methods here."

    models = {
        'sentry.activity': {
            'Meta': {'object_name': 'Activity'},
            'data': ('sentry.db.models.fields.gzippeddict.GzippedDictField', [], {'null': 'True'}),
            'datetime': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
            'group': ('sentry.db.models.fields.foreignkey.FlexibleForeignKey', [], {'to': "orm['sentry.Group']", 'null': 'True'}),
            'id': ('sentry.db.models.fields.bounded.BoundedBigAutoField', [], {'primary_key': 'True'}),
            'ident': ('django.db.models.fields.CharField', [], {'max_length': '64', 'null': 'True'}),
            'project': ('sentry.db.models.fields.foreignkey.FlexibleForeignKey', [], {'to': "orm['sentry.Project']"}),
            'type': ('sentry.db.models.fields.bounded.BoundedPositiveIntegerField', [], {}),
            'user': ('sentry.db.models.fields.foreignkey.FlexibleForeignKey', [], {'to': "orm['sentry.User']", 'null': 'True'})
        },<commit_msg>optimize ReleaseEnvironment and ReleaseFile migrations (#4680)<commit_after># -*- coding: utf-8 -*-
from south.utils import datetime_utils as datetime
from south.db import db
from south.v2 import DataMigration
from django.db import models
from sentry.utils.query import WithProgressBar


class Migration(DataMigration):

    def forwards(self, orm):
        "Write your forwards methods here."
        for project_id, organization_id in WithProgressBar(orm.Project.objects.all().values_list('id', 'organization_id')):
            orm.ReleaseEnvironment.objects.filter(
                project_id=project_id,
                organization_id__isnull=True
            ).update(
                organization_id=organization_id
            )

            orm.ReleaseFile.objects.filter(
                project_id=project_id,
                organization_id__isnull=True
            ).update(
                organization=organization_id
            )


    def backwards(self, orm):
        "Write your backwards methods here."

    models = {
        'sentry.activity': {
            'Meta': {'object_name': 'Activity'},
            'data': ('sentry.db.models.fields.gzippeddict.GzippedDictField', [], {'null': 'True'}),
            'datetime': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
            'group': ('sentry.db.models.fields.foreignkey.FlexibleForeignKey', [], {'to': "orm['sentry.Group']", 'null': 'True'}),
            'id': ('sentry.db.models.fields.bounded.BoundedBigAutoField', [], {'primary_key': 'True'}),
            'ident': ('django.db.models.fields.CharField', [], {'max_length': '64', 'null': 'True'}),
            'project': ('sentry.db.models.fields.foreignkey.FlexibleForeignKey', [], {'to': "orm['sentry.Project']"}),
            'type': ('sentry.db.models.fields.bounded.BoundedPositiveIntegerField', [], {}),
            'user': ('sentry.db.models.fields.foreignkey.FlexibleForeignKey', [], {'to': "orm['sentry.User']", 'null': 'True'})
        },
{'fraction_non_alphanumeric': 0.1506724413038523, 'fraction_numerical': 0.0029633006610439937, 'mean_word_length': 2.64149377593361, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5456709', 'n_tokens_mistral': 1267, 'n_tokens_neox': 1201, 'n_words': 221}