Repository: getsentry/sentry

Issue:
Upgrade on-premise from 9.1.2 to 20.10.1 fails
## Important Details

How are you running Sentry?

On-Premise w/ Docker, version 9.1.2

## Description

Trying to update old installation from Sentry 9.1.2 to Sentry 20.10.1. Using the `SENTRY_PYTHON3=1 ./install.sh` command using the sentry/onpremise repository I get the following error during database migrations:

```
11:37:40 [WARNING] sentry.migrations.0028_user_reports: failed to fetch event b6a4aaf4a66f467bb2321bd55f3f9157 for project 9: Invalid date range. Please try a more recent date range.
11:37:40 [WARNING] sentry.migrations.0028_user_reports: failed to fetch event eb043a94a4084fb3ac00f6e4fdff452a for project 9: Invalid date range. Please try a more recent date range.
11:37:40 [WARNING] sentry.migrations.0028_user_reports: failed to fetch event a0505204da8d4a0d8ba31908ae30640a for project 9: Invalid date range. Please try a more recent date range.
11:37:40 [WARNING] sentry.migrations.0028_user_reports: failed to fetch event 93126a80f6df43989c9663bd569dac24 for project 11: Invalid date range. Please try a more recent date range.
11:37:40 [WARNING] sentry.migrations.0028_user_reports: failed to fetch event e9c6810bbb114b11affebef0a8d681cc for project 9: Invalid date range. Please try a more recent date range.
11:37:40 [WARNING] sentry.migrations.0028_user_reports: failed to fetch event 42d2e29996ea43909c8e182cf2cdff49 for project 11: Invalid date range. Please try a more recent date range.
11:37:40 [WARNING] sentry.migrations.0028_user_reports: failed to fetch event afe6ef57b3f6437fba60a0dde4b4cafe for project 11: Invalid date range. Please try a more recent date range.
11:37:40 [WARNING] sentry.migrations.0028_user_reports: failed to fetch event 56ee4109d2a547388a97c2ab3c4b1da7 for project 9: Invalid date range. Please try a more recent date range.
Traceback (most recent call last):
  File "/usr/local/bin/sentry", line 8, in <module>
    sys.exit(main())
  File "/usr/local/lib/python3.6/site-packages/sentry/runner/__init__.py", line 166, in main
    cli(prog_name=get_prog(), obj={}, max_content_width=100)
  File "/usr/local/lib/python3.6/site-packages/click/core.py", line 722, in __call__
    return self.main(*args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/click/core.py", line 697, in main
    rv = self.invoke(ctx)
  File "/usr/local/lib/python3.6/site-packages/click/core.py", line 1066, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/usr/local/lib/python3.6/site-packages/click/core.py", line 895, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/usr/local/lib/python3.6/site-packages/click/core.py", line 535, in invoke
    return callback(*args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/click/decorators.py", line 17, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/sentry/runner/decorators.py", line 30, in inner
    return ctx.invoke(f, *args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/click/core.py", line 535, in invoke
    return callback(*args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/click/decorators.py", line 17, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/sentry/runner/commands/upgrade.py", line 174, in upgrade
    _upgrade(not noinput, traceback, verbosity, not no_repair, with_nodestore)
  File "/usr/local/lib/python3.6/site-packages/sentry/runner/commands/upgrade.py", line 130, in _upgrade
    ignore_ghost_migrations=True,
  File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 131, in call_command
    return command.execute(*args, **defaults)
  File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 204, in handle
    fake_initial=fake_initial,
  File "/usr/local/lib/python3.6/site-packages/django/db/migrations/executor.py", line 115, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "/usr/local/lib/python3.6/site-packages/django/db/migrations/executor.py", line 145, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "/usr/local/lib/python3.6/site-packages/sentry/new_migrations/monkey/executor.py", line 26, in apply_migration
    state, migration, fake=fake, fake_initial=fake_initial
  File "/usr/local/lib/python3.6/site-packages/django/db/migrations/executor.py", line 244, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/usr/local/lib/python3.6/site-packages/django/db/migrations/migration.py", line 129, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/usr/local/lib/python3.6/site-packages/django/db/migrations/operations/special.py", line 193, in database_forwards
    self.code(from_state.apps, schema_editor)
  File "/usr/local/lib/python3.6/site-packages/sentry/migrations/0028_user_reports.py", line 40, in backfill_user_reports
    report.update(group_id=event.group_id, environment=event.get_environment())
AttributeError: 'UserReport' object has no attribute 'update'
```

## Steps to Reproduce

1. Have pre-existing installation of Sentry 9.1.2
2. `git clone https://github.com/sentry/onpremise.git`
3. `git checkout releases/20.10.1` 
3. `sudo SENTRY_PYTHON3=1 ./install.sh`

### What you expected to happen

A smooth upgrade script run.



Discussion:
This issue has gone three weeks without activity. In another week, I will close it.

But! If you comment or otherwise update it, I will reset the clock, and if you label it `Status: Accepted`, I will leave it alone ... forever!

----

"A weed is but an unloved flower." ― _Ella Wheeler Wilcox_ 🥀
The issue is still relevant. We managed to do the migration with manual clean up of the data in PostgreSQL though.
As someone who might be doing the same migration, what was the manual cleanup You needed to do?
Honestly, I don't remember anymore. The particular update script (https://github.com/getsentry/sentry/blob/master/src/sentry/migrations/0028_user_reports.py) is broken. It is looking into the `UserReport` table for events that have `environment` set to NULL. I think I opened the PostgreSQL command line interface, connected to the DB and deleted all the user reports that satisfied the condition. This caused the update script to skip the particular step and eventually successfully finish. 

Resolving patch:
diff --git a/src/sentry/migrations/0028_user_reports.py b/src/sentry/migrations/0028_user_reports.py
--- a/src/sentry/migrations/0028_user_reports.py
+++ b/src/sentry/migrations/0028_user_reports.py
@@ -35,7 +35,9 @@ def backfill_user_reports(apps, schema_editor):
             continue
 
         if event:
-            report.update(group_id=event.group_id, environment_id=event.get_environment().id)
+            report.group_id = event.group_id
+            report.environment_id = event.get_environment().id
+            report.save(update_fields=["group_id", "environment_id"])
 
 
 class Migration(migrations.Migration):


Test patch: