# Migration failed upgrading to latest from ~3 weeks ago

**URL:** https://meta.discourse.org/t/migration-failed-upgrading-to-latest-from-3-weeks-ago/211450
**Category:** Self-hosting
**Created:** [December 8, 2021, 6:02am UTC](https://meta.discourse.org/t/migration-failed-upgrading-to-latest-from-3-weeks-ago/211450 "2021-12-08T06:02:36Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![carlokok](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/carlokok/32/115951_2.png) [@carlokok](https://meta.discourse.org/u/carlokok)
#### Post date: [December 8, 2021, 6:02am UTC](https://meta.discourse.org/t/migration-failed-upgrading-to-latest-from-3-weeks-ago/211450/1 "2021-12-08T06:02:36Z")

</div>

1 migrations failed!

```plaintext
Failed to migrate default
#<StandardError: An error has occurred, all later migrations canceled:

PG::UniqueViolation: ERROR: duplicate key value violates unique constraint "index_user_emails_on_email"
DETAIL: Key (lower(email::text))=(talk@remobjects.com<mailto:talk@remobjects.com) already exists.
>
/var/www/discourse/vendor/bundle/ruby/2.7.0/gems/rack-mini-profiler-2.3.3/lib/patches/db/pg.rb:110:in `exec'
/var/www/discourse/vendor/bundle/ruby/2.7.0/gems/rack-mini-profiler-2.3.3/lib/patches/db/pg.rb:110:in `async_exec'
/var/www/discourse/vendor/bundle/ruby/2.7.0/gems/activerecord-6.1.4.1/lib/active_record/connection_adapters/postgresql/database_st

== 20211201221028 MigrateEmailToNormalizedEmail: migrating ====================
-- execute("UPDATE user_emails\nSET normalized_email = REPLACE(REGEXP_REPLACE(email,'([+@].*)',''),'.','') || REGEXP_REPLACE(email, '[^@]*', '')\nWHERE (normalized_email IS NULL OR normalized_email <> (REPLACE(REGEXP_REPLACE(email,'([+@].*)',''),'.','') || REGEXP_REPLACE(email, '[^@]*', '')))\n AND (id >= -2 AND id < 7155)\n")
Migrating secondsite
Docker Manager: FAILED TO UPGRADE
#<RuntimeError: RuntimeError>

```

Does anyone know what I can do to get past this?

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [December 8, 2021, 6:22am UTC](https://meta.discourse.org/t/migration-failed-upgrading-to-latest-from-3-weeks-ago/211450/2 "2021-12-08T06:22:17Z")

</div>

Oh … we have seen this before on major version upgrades of PG. @saj recently worked on something here.

The long story is that on some PG upgrades the DB collation changes underneath you and you due to libc changing stuff. You have no choice but to reindex.

What happened to you is that:

1. You did a major upgrade
2. Your index become somewhat corrupt
3. A second insert of a user for `talk@remobjects.com` was made - you now have a duplicate row despite the index disallowing it.
4. We touch the row and it highlights this issue cause it can not update an unrelated column

Fix is:

- Find the user ids of the 2 users sharing the `talk@remobjects.com` email.
- Either delete or rename the email on one of them
- Proceed as planned
- Do a full db reindex urgently to catch other issues

---

<div class="post-metadata">

### Author: ![carlokok](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/carlokok/32/115951_2.png) [@carlokok](https://meta.discourse.org/u/carlokok)
#### Post date: [December 8, 2021, 6:37am UTC](https://meta.discourse.org/t/migration-failed-upgrading-to-latest-from-3-weeks-ago/211450/3 "2021-12-08T06:37:11Z")

</div>

Your solution did solve it but the “upgrade” already passed (e no more updates); do I need to do anything special to rerun these migrations? I did ofc delete the dupe emails.

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [December 8, 2021, 6:39am UTC](https://meta.discourse.org/t/migration-failed-upgrading-to-latest-from-3-weeks-ago/211450/4 "2021-12-08T06:39:24Z")

</div>

Yes, be sure to run a db reindex

> **[REINDEX](https://www.postgresql.org/docs/13/sql-reindex.html)**
>
> REINDEX REINDEX — rebuild indexes Synopsis REINDEX \[( option \[, ...\] ) \] { INDEX | TABLE | SCHEMA …

Try:

`REINDEX DATABASE VERBOSE`

Critical that you do so to ensure your indexes are all in good shape.

---

<div class="post-metadata">

### Author: ![Jonathan5](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jonathan5/32/197134_2.png) [@Jonathan5](https://meta.discourse.org/u/Jonathan5)
#### Post date: [December 8, 2021, 8:54am UTC](https://meta.discourse.org/t/migration-failed-upgrading-to-latest-from-3-weeks-ago/211450/5 "2021-12-08T08:54:29Z")

</div>

Is there any way to check that a forum is “healthy” from time to time, before running into this sort of problem?

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [January 7, 2022, 8:55am UTC](https://meta.discourse.org/t/migration-failed-upgrading-to-latest-from-3-weeks-ago/211450/6 "2022-01-07T08:55:27Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
