# Bug OP-8982 - Migration of journable_type "CostObject" -> "Budget" may fail 

Project: OpenProject
Status: closed
URL: https://community.openproject.org/work_packages/OP-8982

## Open points

_No work packages_

## Details

| Attribute | Value |
| --- | --- |
| Date |  |
| Priority | High |
| Module |  |
| Target versions | 12.0.0 |
| Observed in versions |  |
| Sprint | 12.0.0 |
| Backlog bucket |  |
| Bug found in version (deprecated) |  |
| Found in mobile app version |  |
| Follow-up feedback e-mail |  |
| Requires doc change | No |
| Accessibility | No |
| Pull preview |  |
| List |  |
| Labels |  |

## People

| Attribute | Value |
| --- | --- |
| Assignee |  |
| Accountable |  |
| Product manager |  |
| Developers |  |
| Designer |  |
| Ordered by |  |
| Signed off by |  |
| Interest by |  |

## Planning

| Attribute | Value |
| --- | --- |
| Relevance |  |

## Costs

| Attribute | Value |
| --- | --- |
| Votes |  |
| Work |  |
| Labor costs |  |

## Description

### **Environment**:

Your OpenProject Version: &lt;VERSION&gt; / Cloud Edition

Operating System / Browser / OpenProject language:

### **Logs**

```bash
PG::UniqueViolation: ERROR:  duplicate key value violates unique constraint "index_journals_on_journable_type_and_journable_id_and_version"
DETAIL:  Key (journable_type, journable_id, version)=(Budget, 29, 1) already exists.
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-6.1.4/lib/active_record/connection_adapters/postgresql_adapter.rb:672:in `exec_params'
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-6.1.4/lib/active_record/connection_adapters/postgresql_adapter.rb:672:in `block (2 levels) in exec_no_cache'
/app/vendor/bundle/ruby/2.7.0/gems/activesupport-6.1.4/lib/active_support/dependencies/interlock.rb:48:in `block in permit_concurrent_loads'
/app/vendor/bundle/ruby/2.7.0/gems/activesupport-6.1.4/lib/active_support/concurrency/share_lock.rb:187:in `yield_shares'
/app/vendor/bundle/ruby/2.7.0/gems/activesupport-6.1.4/lib/active_support/dependencies/interlock.rb:47:in `permit_concurrent_loads'
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-6.1.4/lib/active_record/connection_adapters/postgresql_adapter.rb:671:in `block in exec_no_cache'
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-6.1.4/lib/active_record/connection_adapters/abstract_adapter.rb:696:in `block (2 levels) in log'
/app/vendor/bundle/ruby/2.7.0/gems/activesupport-6.1.4/lib/active_support/concurrency/load_interlock_aware_monitor.rb:26:in `block (2 levels) in synchronize'
/app/vendor/bundle/ruby/2.7.0/gems/activesupport-6.1.4/lib/active_support/concurrency/load_interlock_aware_monitor.rb:25:in `handle_interrupt'
/app/vendor/bundle/ruby/2.7.0/gems/activesupport-6.1.4/lib/active_support/concurrency/load_interlock_aware_monitor.rb:25:in `block in synchronize'
/app/vendor/bundle/ruby/2.7.0/gems/activesupport-6.1.4/lib/active_support/concurrency/load_interlock_aware_monitor.rb:21:in `handle_interrupt'
/app/vendor/bundle/ruby/2.7.0/gems/activesupport-6.1.4/lib/active_support/concurrency/load_interlock_aware_monitor.rb:21:in `synchronize'
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-6.1.4/lib/active_record/connection_adapters/abstract_adapter.rb:695:in `block in log'
/app/vendor/bundle/ruby/2.7.0/gems/activesupport-6.1.4/lib/active_support/notifications/instrumenter.rb:24:in `instrument'
/app/vendor/bundle/ruby/2.7.0/gems/sentry-rails-4.6.1/lib/sentry/rails/tracing.rb:42:in `instrument'
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-6.1.4/lib/active_record/connection_adapters/abstract_adapter.rb:687:in `log'
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-6.1.4/lib/active_record/connection_adapters/postgresql_adapter.rb:670:in `exec_no_cache'
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-6.1.4/lib/active_record/connection_adapters/postgresql_adapter.rb:649:in `execute_and_clear'
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-6.1.4/lib/active_record/connection_adapters/postgresql/database_statements.rb:70:in `exec_delete'
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-6.1.4/lib/active_record/connection_adapters/abstract/database_statements.rb:179:in `update'
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-6.1.4/lib/active_record/connection_adapters/abstract/query_cache.rb:22:in `update'
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-6.1.4/lib/active_record/relation.rb:466:in `update_all'
/app/modules/costs/db/migrate/20210726065912_rename_cost_object_type.rb:5:in `up'
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-6.1.4/lib/active_record/migration.rb:870:in `public_send'
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-6.1.4/lib/active_record/migration.rb:870:in `exec_migration'
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-6.1.4/lib/active_record/migration.rb:851:in `block (2 levels) in migrate'
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-6.1.4/lib/active_record/migration.rb:850:in `block in migrate'
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-6.1.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:462:in `with_connection'
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-6.1.4/lib/active_record/migration.rb:849:in `migrate'
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-6.1.4/lib/active_record/migration.rb:1037:in `migrate'
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-6.1.4/lib/active_record/migration.rb:1329:in `block in execute_migration_in_transaction'
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-6.1.4/lib/active_record/migration.rb:1380:in `block in ddl_transaction'
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-6.1.4/lib/active_record/connection_adapters/abstract/database_statements.rb:320:in `block in transaction'
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-6.1.4/lib/active_record/connection_adapters/abstract/transaction.rb:319:in `block in within_new_transaction'
/app/vendor/bundle/ruby/2.7.0/gems/activesupport-6.1.4/lib/active_support/concurrency/load_interlock_aware_monitor.rb:26:in `block (2 levels) in synchronize'
/app/vendor/bundle/ruby/2.7.0/gems/activesupport-6.1.4/lib/active_support/concurrency/load_interlock_aware_monitor.rb:25:in `handle_interrupt'
/app/vendor/bundle/ruby/2.7.0/gems/activesupport-6.1.4/lib/active_support/concurrency/load_interlock_aware_monitor.rb:25:in `block in synchronize'
/app/vendor/bundle/ruby/2.7.0/gems/activesupport-6.1.4/lib/active_support/concurrency/load_interlock_aware_monitor.rb:21:in `handle_interrupt'
/app/vendor/bundle/ruby/2.7.0/gems/activesupport-6.1.4/lib/active_support/concurrency/load_interlock_aware_monitor.rb:21:in `synchronize'
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-6.1.4/lib/active_record/connection_adapters/abstract/transaction.rb:317:in `within_new_transaction'
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-6.1.4/lib/active_record/connection_adapters/abstract/database_statements.rb:320:in `transaction'
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-6.1.4/lib/active_record/transactions.rb:209:in `transaction'
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-6.1.4/lib/active_record/migration.rb:1380:in `ddl_transaction'
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-6.1.4/lib/active_record/migration.rb:1328:in `execute_migration_in_transaction'
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-6.1.4/lib/active_record/migration.rb:1302:in `each'
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-6.1.4/lib/active_record/migration.rb:1302:in `migrate_without_lock'
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-6.1.4/lib/active_record/migration.rb:1251:in `block in migrate'
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-6.1.4/lib/active_record/migration.rb:1401:in `block in with_advisory_lock'
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-6.1.4/lib/active_record/migration.rb:1416:in `block in with_advisory_lock_connection'
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-6.1.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:462:in `with_connection'
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-6.1.4/lib/active_record/migration.rb:1416:in `with_advisory_lock_connection'
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-6.1.4/lib/active_record/migration.rb:1397:in `with_advisory_lock'
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-6.1.4/lib/active_record/migration.rb:1251:in `migrate'
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-6.1.4/lib/active_record/migration.rb:1086:in `up'
/app/vendor/bundle/ruby/2.7.0/gems/activerecord-6.1.4/lib/active_record/migration.rb:1061:in `migrate'
/app/vendor/bundle/ruby/2.7.0/bundler/gems/apartment-7638a3351166/lib/apartment/migrator.rb:18:in `block in migrate'
/app/vendor/bundle/ruby/2.7.0/bundler/gems/apartment-7638a3351166/lib/apartment/adapters/abstract_adapter.rb:85:in `switch'
/app/vendor/bundle/ruby/2.7.0/bundler/gems/apartment-7638a3351166/lib/apartment/migrator.rb:10:in `migrate'
/app/vendor/bundle/ruby/2.7.0/bundler/gems/apartment-7638a3351166/lib/tasks/apartment.rake:36:in `block (3 levels) in <top (required)>'
/app/vendor/bundle/ruby/2.7.0/bundler/gems/apartment-7638a3351166/lib/tasks/apartment.rake:124:in `block in each_tenant'
/app/vendor/bundle/ruby/2.7.0/gems/parallel-1.20.1/lib/parallel.rb:509:in `call_with_index'
/app/vendor/bundle/ruby/2.7.0/gems/parallel-1.20.1/lib/parallel.rb:343:in `block in work_direct'
/app/vendor/bundle/ruby/2.7.0/gems/parallel-1.20.1/lib/parallel.rb:518:in `with_instrumentation'
/app/vendor/bundle/ruby/2.7.0/gems/parallel-1.20.1/lib/parallel.rb:342:in `work_direct'
/app/vendor/bundle/ruby/2.7.0/gems/parallel-1.20.1/lib/parallel.rb:279:in `map'
/app/vendor/bundle/ruby/2.7.0/gems/parallel-1.20.1/l�ib/parallel.rb:232:in `each'
/app/vendor/bundle/ruby/2.7.0/bundler/gems/apartment-7638a3351166/lib/tasks/apartment.rake:123:in `each_tenant'
/app/vendor/bundle/ruby/2.7.0/bundler/gems/apartment-7638a3351166/lib/tasks/apartment.rake:33:in `block (2 levels) in <top (required)>'
/app/vendor/bundle/ruby/2.7.0/bundler/gems/apartment-7638a3351166/lib/apartment/tasks/enhancements.rb:44:in `block in enhance_after_task'
/app/vendor/bundle/ruby/2.7.0/gems/rake-13.0.6/exe/rake:27:in `<top (required)>'
/usr/local/bundle/gems/bundler-2.1.4/lib/bundler/cli/exec.rb:63:in `load'
/usr/local/bundle/gems/bundler-2.1.4/lib/bundler/cli/exec.rb:63:in `kernel_load'
/usr/local/bundle/gems/bundler-2.1.4/lib/bundler/cli/exec.rb:28:in `run'
/usr/local/bundle/gems/bundler-2.1.4/lib/bundler/cli.rb:476:in `exec'
/usr/local/bundle/gems/bundler-2.1.4/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
/usr/local/bundle/gems/bundler-2.1.4/lib/bundler/vendor/thor/lib/thor/invocation.rb:127:in `invoke_command'
/usr/local/bundle/gems/bundler-2.1.4/lib/bundler/vendor/thor/lib/thor.rb:399:in `dispatch'
/usr/local/bundle/gems/bundler-2.1.4/lib/bundler/cli.rb:30:in `dispatch'
/usr/local/bundle/gems/bundler-2.1.4/lib/bundler/vendor/thor/lib/thor/base.rb:476:in `start'
/usr/local/bundle/gems/bundler-2.1.4/lib/bundler/cli.rb:24:in `start'
/usr/local/bundle/gems/bundler-2.1.4/exe/bundle:46:in `block in <top (required)>'
/usr/local/bundle/gems/bundler-2.1.4/lib/bundler/friendly_errors.rb:123:in `with_friendly_errors'
/usr/local/bundle/gems/bundler-2.1.4/exe/bundle:34:in `<top (required)>'
/usr/local/bundle/bin/bundle:23:in `load'
/usr/local/bundle/bin/bundle:23:in `<main>'
```

### **Steps to reproduce:**

1.  Not clear how the data with a conflicting journal for both the triplet of &quot;CostObject&quot;, id (e.g. 5) and version (e.g. 1) as well as &quot;Budget&quot;, id (e.g. 5) and version (e.g. 1) was created. But if both exist, the migration fails.

### **Actual Behavior**

Migration fails

### **Expected Behavior**

Migration runs through. Assuming both journals are valid, the migration should increase the version of the already existing &quot;Budget&quot;, id, version journals by the amount of journals with &quot;CostObject&quot;, id.
