<reponame>pierredup/sentry
# -*- coding: utf-8 -*-
# Generated by Django 1.11.27 on 2020-01-23 19:07
from __future__ import unicode_literals

import logging

from django.db import migrations

from sentry import eventstore
from sentry.utils.query import RangeQuerySetWrapper
from sentry.utils.snuba import (
    SnubaError,
    QueryOutsideRetentionError,
    QueryOutsideGroupActivityError,
)

logger = logging.getLogger(__name__)


def backfill_user_reports(apps, schema_editor):
    """
    Processes user reports that are missing event data, and adds the appropriate data
    if the event exists in Clickhouse.
    """
    UserReport = apps.get_model("sentry", "UserReport")

    user_reports = UserReport.objects.filter(group__isnull=True, environment__isnull=True)

    for report in RangeQuerySetWrapper(user_reports, step=1000):
        try:
            event = eventstore.get_event_by_id(report.project_id, report.event_id)
        except (SnubaError, QueryOutsideGroupActivityError, QueryOutsideRetentionError) as se:
            logger.warn(
                "failed to fetch event %s for project %d: %s"
                % (report.event_id, report.project_id, se)
            )
            continue

        if event:
            report.update(group_id=event.group_id, environment=event.get_environment())


class Migration(migrations.Migration):
    # This flag is used to mark that a migration shouldn't be automatically run in
    # production. We set this to True for operations that we think are risky and want
    # someone from ops to run manually and monitor.
    # General advice is that if in doubt, mark your migration as `is_dangerous`.
    # Some things you should always mark as dangerous:
    # - Adding indexes to large tables. These indexes should be created concurrently,
    #   unfortunately we can't run migrations outside of a transaction until Django
    #   1.10. So until then these should be run manually.
    # - Large data migrations. Typically we want these to be run manually by ops so that
    #   they can be monitored. Since data migrations will now hold a transaction open
    #   this is even more important.
    # - Adding columns to highly active tables, even ones that are NULL.
    is_dangerous = True

    dependencies = [
        ("sentry", "0027_exporteddata"),
    ]

    operations = [
        migrations.RunPython(backfill_user_reports, migrations.RunPython.noop),
    ]

# -*- coding: utf-8 -*-
# Generated by Django 1.11.27 on 2020-01-23 19:07
from __future__ import unicode_literals

import logging

from django.db import migrations

from sentry import eventstore
from sentry.utils.query import RangeQuerySetWrapper
from sentry.utils.snuba import (
    SnubaError,
    QueryOutsideRetentionError,
    QueryOutsideGroupActivityError,
)

logger = logging.getLogger(__name__)


def backfill_user_reports(apps, schema_editor):
    """
    Processes user reports that are missing event data, and adds the appropriate data
    if the event exists in Clickhouse.
    """
    UserReport = apps.get_model("sentry", "UserReport")

    user_reports = UserReport.objects.filter(group__isnull=True, environment__isnull=True)

    for report in RangeQuerySetWrapper(user_reports, step=1000):
        try:
            event = eventstore.get_event_by_id(report.project_id, report.event_id)
        except (SnubaError, QueryOutsideGroupActivityError, QueryOutsideRetentionError) as se:
            logger.warn(
                "failed to fetch event %s for project %d: %s"
                % (report.event_id, report.project_id, se)
            )
            continue

        if event:
            report.update(group_id=event.group_id, environment=event.get_environment())


class Migration(migrations.Migration):
    # This flag is used to mark that a migration shouldn't be automatically run in
    # production. We set this to True for operations that we think are risky and want
    # someone from ops to run manually and monitor.
    # General advice is that if in doubt, mark your migration as `is_dangerous`.
    # Some things you should always mark as dangerous:
    # - Adding indexes to large tables. These indexes should be created concurrently,
    #   unfortunately we can't run migrations outside of a transaction until Django
    #   1.10. So until then these should be run manually.
    # - Large data migrations. Typically we want these to be run manually by ops so that
    #   they can be monitored. Since data migrations will now hold a transaction open
    #   this is even more important.
    # - Adding columns to highly active tables, even ones that are NULL.
    is_dangerous = True

    dependencies = [
        ("sentry", "0027_exporteddata"),
    ]

    operations = [
        migrations.RunPython(backfill_user_reports, migrations.RunPython.noop),
    ]
# -*- coding: utf-8 -*- # Generated by Django 1.11.27 on 2020-01-23 19:07 Processes user reports that are missing event data, and adds the appropriate data     if the event exists in Clickhouse. # This flag is used to mark that a migration shouldn't be automatically run in # production. We set this to True for operations that we think are risky and want # someone from ops to run manually and monitor. # General advice is that if in doubt, mark your migration as `is_dangerous`. # Some things you should always mark as dangerous: # - Adding indexes to large tables. These indexes should be created concurrently, #   unfortunately we can't run migrations outside of a transaction until Django #   1.10. So until then these should be run manually. # - Large data migrations. Typically we want these to be run manually by ops so that #   they can be monitored. Since data migrations will now hold a transaction open #   this is even more important. # - Adding columns to highly active tables, even ones that are NULL.