# Upgrade fails while migrating

**URL:** https://meta.discourse.org/t/upgrade-fails-while-migrating/121894
**Category:** Self-hosting
**Created:** [July 2, 2019, 3:17pm UTC](https://meta.discourse.org/t/upgrade-fails-while-migrating/121894 "2019-07-02T15:17:35Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![klopsknoedel](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/klopsknoedel/32/194126_2.png) [@klopsknoedel](https://meta.discourse.org/u/klopsknoedel)
#### Post date: [July 2, 2019, 3:17pm UTC](https://meta.discourse.org/t/upgrade-fails-while-migrating/121894/1 "2019-07-02T15:17:36Z")

</div>

Hi,

we are trying to update a very old installation of Discourse (v1.2.4) to the latest v2.3.1. Our first test-migrations using v2.2.0 done surprisingly well and the migration-process finished without any issues.  
But now we try to run that in the final stages against v2.3.1 and get the following error:

```
PG::UniqueViolation: ERROR: duplicate key value violates unique constraint "idx_unique_rows"
DETAIL: Key (action_type, user_id, target_topic_id, target_post_id, acting_user_id)=(15, 1140990, 9346002, 9431164, 419835) already exists.
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rack-mini-profiler-1.0.2/lib/patches/db/pg.rb:110:in `async_exec'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rack-mini-profiler-1.0.2/lib/patches/db/pg.rb:110:in `async_exec'
(eval):24:in `async_exec'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/mini_sql-0.2.2/lib/mini_sql/postgres/connection.rb:118:in `run'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/mini_sql-0.2.2/lib/mini_sql/postgres/connection.rb:90:in `exec'
/var/www/discourse/plugins/discourse-solved/plugin.rb:64:in `block in activate!'
/var/www/discourse/lib/plugin/instance.rb:328:in `block in notify_after_initialize'
/var/www/discourse/lib/plugin/instance.rb:326:in `each'
/var/www/discourse/lib/plugin/instance.rb:326:in `notify_after_initialize'
/var/www/discourse/config/application.rb:292:in `each'
/var/www/discourse/config/application.rb:292:in `block in <class:Application>'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.3/lib/active_support/lazy_load_hooks.rb:69:in `block in execute_hook'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.3/lib/active_support/lazy_load_hooks.rb:62:in `with_execution_control'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.3/lib/active_support/lazy_load_hooks.rb:67:in `execute_hook'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.3/lib/active_support/lazy_load_hooks.rb:52:in `block in run_load_hooks'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.3/lib/active_support/lazy_load_hooks.rb:51:in `each'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.3/lib/active_support/lazy_load_hooks.rb:51:in `run_load_hooks'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/railties-5.2.3/lib/rails/application/finisher.rb:75:in `block in <module:Finisher>'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/railties-5.2.3/lib/rails/initializable.rb:32:in `instance_exec'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/railties-5.2.3/lib/rails/initializable.rb:32:in `run'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/railties-5.2.3/lib/rails/initializable.rb:61:in `block in run_initializers'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/railties-5.2.3/lib/rails/initializable.rb:60:in `run_initializers'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/railties-5.2.3/lib/rails/application.rb:361:in `initialize!'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/railties-5.2.3/lib/rails/railtie.rb:190:in `public_send'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/railties-5.2.3/lib/rails/railtie.rb:190:in `method_missing'
/var/www/discourse/config/environment.rb:7:in `<top (required)>'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.3/lib/active_support/dependencies.rb:291:in `require'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.3/lib/active_support/dependencies.rb:291:in `block in require'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.3/lib/active_support/dependencies.rb:257:in `load_dependency'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.3/lib/active_support/dependencies.rb:291:in `require'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/railties-5.2.3/lib/rails/application.rb:337:in `require_environment!'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/railties-5.2.3/lib/rails/application.rb:520:in `block in run_tasks_blocks'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rake-12.3.2/exe/rake:27:in `<top (required)>'
/var/www/discourse/bin/bundle:5:in `load'
/var/www/discourse/bin/bundle:5:in `<main>'
Tasks: TOP => db:migrate => db:load_config => environment
(See full trace by running task with --trace)

```

This seems to be caused by “20120816050526\_add\_unique\_constraint\_to\_user\_actions.rb” and if I take a look in the database-table “schema\_migrations” it seems that this has already been applied. As this migration seems to be added in 2012.

Do you have any ideas why “bundle exec rake db:migrate” is running already applied migrations or what we are doing wrong?  
I know the source - version is pretty old but as said we already got successful results.

Kind Regards

---

<div class="post-metadata">

### Author: ![klopsknoedel](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/klopsknoedel/32/194126_2.png) [@klopsknoedel](https://meta.discourse.org/u/klopsknoedel)
#### Post date: [July 2, 2019, 6:11pm UTC](https://meta.discourse.org/t/upgrade-fails-while-migrating/121894/2 "2019-07-02T18:11:03Z")

</div>

I’ve misinterpreted this error. So sorry for the confusion. Obviously it has nothing to do with the mentioned migration-file.  
Anyway, there seems to be an existing index for an entry in `user_actions`. So I checked that with:

```plaintext
SELECT *
FROM user_actions
WHERE action_type=15
    AND user_id=1140990
    AND target_topic_id=9346002
    AND target_post_id=9431164
    AND acting_user_id=419835;

```

This query gives me 0 Results. So there must be an index without a corresponding dataset.

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [July 2, 2019, 6:20pm UTC](https://meta.discourse.org/t/upgrade-fails-while-migrating/121894/3 "2019-07-02T18:20:47Z")

</div>

When I’ve had similar problems I’ve deleted the thing that it was trying to duplicate. That’s likely not very helpful.

Or maybe make sure that you were in read-only when you make the backup?

---

<div class="post-metadata">

### Author: ![klopsknoedel](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/klopsknoedel/32/194126_2.png) [@klopsknoedel](https://meta.discourse.org/u/klopsknoedel)
#### Post date: [July 3, 2019, 4:26pm UTC](https://meta.discourse.org/t/upgrade-fails-while-migrating/121894/4 "2019-07-03T16:26:17Z")

</div>

> Or maybe make sure that you were in read-only when you make the backup?

Yes maybe that’s the problem. We’ve created the dump while the database was in use. Worked at the first try. I’ll create another one and try again.

---

<div class="post-metadata">

### Author: ![klopsknoedel](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/klopsknoedel/32/194126_2.png) [@klopsknoedel](https://meta.discourse.org/u/klopsknoedel)
#### Post date: [July 11, 2019, 2:22am UTC](https://meta.discourse.org/t/upgrade-fails-while-migrating/121894/5 "2019-07-11T02:22:57Z")

</div>

Hi. It seemed our `user_actions` table got corrupted. I could fix that by running `VACUUM (VERBOSE,ANALYZE,DISABLE_PAGE_SKIPPING) user_actions`. But thanks anyway for your advise!  
There where several entries missing from the visibility map which produced duplicated keys.

---

<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: [December 31, 2022, 1:47am UTC](https://meta.discourse.org/t/upgrade-fails-while-migrating/121894/6 "2022-12-31T01:47:45Z")

</div>



---

<div class="post-metadata">

### Author: ![Stephen](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/stephen/32/95011_2.png) [@Stephen](https://meta.discourse.org/u/Stephen)
#### Post date: [December 31, 2022, 2:24am UTC](https://meta.discourse.org/t/upgrade-fails-while-migrating/121894/7 "2022-12-31T02:24:55Z")

</div>


