self-hosted icon indicating copy to clipboard operation
self-hosted copied to clipboard

Restore backup: psycopg2.errors.ForeignKeyViolation: Key (api_grant_id)=(1) is not present in table "sentry_apigrant"

Open tobiasmuehl opened this issue 3 years ago • 4 comments

Self-Hosted Version

22.7.0

CPU Architecture

x86_64

Docker Version

20.10.17

Docker Compose Version

2.6.0

Steps to Reproduce

I forgot what I actually did in Sentry once it was installed... Roughly speaking, it was:

  • github integration
  • internal integration
  • added 2 members
  1. Install sentry
  2. change "Open Membership" setting to false in admin panel
  3. create backup file as indicated in docs: docker-compose run --rm -T -e SENTRY_LOG_LEVEL=CRITICAL web export > sentry/backup.json
  4. docker compose down
  5. docker volume rm $(docker volume ls -q)
  6. ./install.sh
  7. Restore backup docker-compose run --rm -T web import /etc/sentry/backup.json

Expected Result

successful restore

Actual Result

/usr/local/lib/python3.8/site-packages/sentry/runner/initializer.py:555: DeprecatedSettingWarning: The SENTRY_URL_PREFIX setting is deprecated. Please use SENTRY_OPTIONS['system.url-prefix'] instead.
  warnings.warn(DeprecatedSettingWarning(old, "SENTRY_OPTIONS['%s']" % new))
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/sentry/db/postgres/decorators.py", line 91, in inner
    return func(self, sql, *args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/sentry/db/postgres/base.py", line 86, in execute
    return self.cursor.execute(sql, clean_bad_params(params))
psycopg2.errors.ForeignKeyViolation: insert or update on table "sentry_sentryappinstallation" violates foreign key constraint "sentry_sentryappinst_api_grant_id_a30cf323_fk_sentry_ap"
DETAIL:  Key (api_grant_id)=(1) is not present in table "sentry_apigrant".


During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/usr/local/lib/python3.8/site-packages/sentry/db/postgres/decorators.py", line 77, in inner
    raise_the_exception(self.db, e)
  File "/usr/local/lib/python3.8/site-packages/sentry/db/postgres/decorators.py", line 75, in inner
    return func(self, *args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/sentry/db/postgres/decorators.py", line 18, in inner
    return func(self, *args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/sentry/db/postgres/decorators.py", line 95, in inner
    raise exc_info[0](msg).with_traceback(exc_info[2])
  File "/usr/local/lib/python3.8/site-packages/sentry/db/postgres/decorators.py", line 91, in inner
    return func(self, sql, *args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/sentry/db/postgres/base.py", line 86, in execute
    return self.cursor.execute(sql, clean_bad_params(params))
psycopg2.errors.ForeignKeyViolation: ForeignKeyViolation('insert or update on table "sentry_sentryappinstallation" violates foreign key constraint "sentry_sentryappinst_api_grant_id_a30cf323_fk_sentry_ap"\nDETAIL:  Key (api
_grant_id)=(1) is not present in table "sentry_apigrant".\n')
SQL: INSERT INTO "sentry_sentryappinstallation" ("id", "date_deleted", "sentry_app_id", "organization_id", "api_grant_id", "api_token_id", "uuid", "status", "date_added", "date_updated") VALUES (%s, %s, %s, %s, %s, %s, %s,
%s, %s, %s)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/local/bin/sentry", line 8, in <module>
    sys.exit(main())
  File "/usr/local/lib/python3.8/site-packages/sentry/runner/__init__.py", line 188, in main
    func(**kwargs)
  File "/usr/local/lib/python3.8/site-packages/click/core.py", line 1128, in __call__
    return self.main(*args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/click/core.py", line 1659, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/usr/local/lib/python3.8/site-packages/click/core.py", line 1395, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/usr/local/lib/python3.8/site-packages/click/core.py", line 754, in invoke
    return __callback(*args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/click/decorators.py", line 26, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/sentry/runner/decorators.py", line 29, in inner
    return ctx.invoke(f, *args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/click/core.py", line 754, in invoke
    return __callback(*args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/sentry/runner/commands/backup.py", line 21, in import_
    obj.save()
  File "/usr/local/lib/python3.8/site-packages/django/core/serializers/base.py", line 223, in save
    models.Model.save_base(self.object, using=using, raw=True, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/django/db/models/base.py", line 780, in save_base
    updated = self._save_table(
  File "/usr/local/lib/python3.8/site-packages/django/db/models/base.py", line 873, in _save_table
    result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
  File "/usr/local/lib/python3.8/site-packages/django/db/models/base.py", line 910, in _do_insert
    return manager._insert([self], fields=fields, return_id=update_pk,
  File "/usr/local/lib/python3.8/site-packages/django/db/models/manager.py", line 82, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/django/db/models/query.py", line 1186, in _insert
    return query.get_compiler(using=using).execute_sql(return_id)
  File "/usr/local/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1377, in execute_sql
    cursor.execute(sql, params)
  File "/usr/local/lib/python3.8/site-packages/sentry_sdk/integrations/django/__init__.py", line 556, in execute
    return real_execute(self, sql, params)
  File "/usr/local/lib/python3.8/site-packages/django/db/backends/utils.py", line 67, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/usr/local/lib/python3.8/site-packages/django/db/backends/utils.py", line 76, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/usr/local/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/usr/local/lib/python3.8/site-packages/django/db/utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/usr/local/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/usr/local/lib/python3.8/site-packages/sentry/db/postgres/decorators.py", line 77, in inner
    raise_the_exception(self.db, e)
  File "/usr/local/lib/python3.8/site-packages/sentry/db/postgres/decorators.py", line 75, in inner
    return func(self, *args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/sentry/db/postgres/decorators.py", line 18, in inner
    return func(self, *args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/sentry/db/postgres/decorators.py", line 95, in inner
    raise exc_info[0](msg).with_traceback(exc_info[2])
  File "/usr/local/lib/python3.8/site-packages/sentry/db/postgres/decorators.py", line 91, in inner
    return func(self, sql, *args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/sentry/db/postgres/base.py", line 86, in execute
    return self.cursor.execute(sql, clean_bad_params(params))
django.db.utils.IntegrityError: ForeignKeyViolation('insert or update on table "sentry_sentryappinstallation" violates foreign key constraint "sentry_sentryappinst_api_grant_id_a30cf323_fk_sentry_ap"\nDETAIL:  Key (api_gran
t_id)=(1) is not present in table "sentry_apigrant".\n')
SQL: INSERT INTO "sentry_sentryappinstallation" ("id", "date_deleted", "sentry_app_id", "organization_id", "api_grant_id", "api_token_id", "uuid", "status", "date_added", "date_updated") VALUES (%s, %s, %s, %s, %s, %s, %s,
%s, %s, %s)

tobiasmuehl avatar Jul 27 '22 09:07 tobiasmuehl

Could this maybe be the internal integration?

Did you upgrade from an older version or is this a fresh installation?

emmatyping avatar Jul 27 '22 16:07 emmatyping

Could be the internal integration. I ran install.sh, which I think caused new docker images to be pulled, which might've been an unexpected upgrade.

tobiasmuehl avatar Jul 28 '22 02:07 tobiasmuehl

I ran install.sh, which I think caused new docker images to be pulled, which might've been an unexpected upgrade.

So just to confirm, you are upgrading then?

Do you have a copy of the install log? Should be in the root of the self-hosted checkout. It seems that maybe a migration wasn't run or failed.

emmatyping avatar Jul 28 '22 16:07 emmatyping

Checked the install logs, all looks normal, migrations have run. I assume there must've been a version upgrade that caused the restore to fail (it's mentioned in the docs that it should be restored on the same version....). I already installed again from scratch and re-setup, so there's no need to actually restore this backup.

tobiasmuehl avatar Jul 29 '22 06:07 tobiasmuehl

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: Backlog or Status: In Progress, I will leave it alone ... forever!


"A weed is but an unloved flower." ― Ella Wheeler Wilcox 🥀

github-actions[bot] avatar Aug 20 '22 00:08 github-actions[bot]